0% found this document useful (0 votes)
10 views10 pages

Overview of Software Design Patterns

Design patterns

Uploaded by

itsmy3209
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

Overview of Software Design Patterns

Design patterns

Uploaded by

itsmy3209
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Design patterns presentation

Design patterns are formalized solutions to common problems in software


design, helping developers create flexible, maintainable, and efficient code
structures[1][3]. These patterns establish best practices across projects and
make teamwork easier, since developers share a common vocabulary for
design concepts[8][9].

### What Are Design Patterns?

Design patterns are high-level strategies or templates that provide


repeatable solutions for recurring software design issues, without dictating
exact code[4][5]. They are not finished designs but are blueprints that can
be customized for specific situations[7]. Patterns make code easier to read,
debug, and enhance by promoting clear separation of concerns, reusability,
and standardization[3][8][5].

### Types of Design Patterns

Design patterns are generally categorized as:

| Pattern Type | Purpose/Example | Example


Pattern | Example Description |

|-------------------|--------------------------------------------------------|--------------------|---------
--------------------------------------------------------------------------|

| Creational | Object creation, instantiation | Singleton


| Ensures a class has only one instance and provides global access[1][3]. |

| Structural | Composition of classes/objects | Adapter


| Converts interface of a class into another interface clients expect[1][7].|

| Behavioral | Communication between objects, responsibilities |


Observer | Defines one-to-many dependency, notifying all dependents
on state change[1]. |

### Classic Examples of Design Patterns


#### 1. Singleton Pattern (Creational)

Ensures only one instance of a class exists in a system, often used for
managing shared resources like configurations or logging[1][3].

- **Example:** Database connection manager. Rather than multiple


connections scattered throughout an app, a singleton ensures there's only
one shared connection.

#### 2. Adapter Pattern (Structural)

Allows classes with incompatible interfaces to work together by providing a


wrapper with the expected interface[7].

- **Example:** Using legacy code with new interfaces. You could wrap old
code so it can interface with new modules without rewriting everything.

#### 3. Observer Pattern (Behavioral)

Enables a publisher to notify multiple subscribers/reactors about any state


changes, commonly used for event handling or UI updates[1][3].

- **Example:** In a graphical UI, when the model changes, all registered


views automatically update themselves.

### Benefits of Design Patterns

- **Reusable solutions** make problem-solving faster[8].

- **Standardized vocabulary** streamlines communication between


developers[9].

- **Efficiency** and **flexibility** in code structure, saving time and


promoting robust designs[5][8].

### Example in Pseudocode: Singleton

```python

class Singleton:
_instance = None

def __new__(cls):

if cls._instance is None:

cls._instance = super().__new__(cls)

return cls._instance

```

This ensures any call to create `Singleton` objects results in the same
instance.

### Summary Table of Key Patterns

| Category | Pattern | Brief Description |

|-------------|--------------|----------------------------------------------------|

| Creational | Singleton | One shared instance globally[1][3]. |

| Structural | Adapter | Converts interface for compatibility[7]. |

| Behavioral | Observer | Notification for dependent objects[3][1]. |

Design patterns act as reliable, proven guides for building robust and
maintainable software ssystem.

Here are detailed Java code examples for each of the main design patterns
discussed earlier: Singleton (Creational), Adapter (Structural), and Observer
(Behavioral)[1][2][3][4][5][6].

### Singleton Pattern Example in Java

The Singleton pattern ensures only one instance of a class is created and
provides a global access point[1][7][8][9].
```java

class Database {

// Holds the single instance

private static Database dbObject;

// Private constructor prevents instantiation from other classes

private Database() {}

// Returns the single instance, creating it if necessary

public static Database getInstance() {

if (dbObject == null) {

dbObject = new Database();

return dbObject;

public void getConnection() {

[Link]("You are now connected to the database.");

public class Main {

public static void main(String[] args) {

Database db1 = [Link]();

[Link]();

}
}

```

This ensures you always get the same `Database` instance throughout your
application[1][9].

***

### Adapter Pattern Example in Java

The Adapter pattern allows objects with incompatible interfaces to


collaborate by wrapping one interface into another[2][4][10].

```java

// Target Interface

interface ModernPrinter {

void print(String message);

// Adaptee (legacy code)

class LegacyPrinter {

public void printLegacy(String text) {

[Link]("Legacy Printer: " + text);

// Adapter: implements the target interface and adapts legacy to modern

class PrinterAdapter implements ModernPrinter {


private LegacyPrinter legacyPrinter;

public PrinterAdapter(LegacyPrinter legacyPrinter) {

[Link] = legacyPrinter;

@Override

public void print(String message) {

[Link](message);

public class Main {

public static void main(String[] args) {

LegacyPrinter legacyPrinter = new LegacyPrinter();

ModernPrinter modernPrinter = new PrinterAdapter(legacyPrinter);

[Link]("Hello World!");

```

The adapter lets modern code use old implementations seamlessly[2][4][10].

***

### Observer Pattern Example in Java


The Observer pattern lets multiple observers listen for and react to changes
in another object's state[3][5][6].

```java

import [Link];

import [Link];

// Subject class

class Subject {

private List<Observer> observers = new ArrayList<>();

private int state;

public int getState() { return state; }

public void setState(int state) {

[Link] = state;

notifyAllObservers();

public void attach(Observer observer) {

[Link](observer);

public void notifyAllObservers() {

for (Observer obs : observers) {

[Link]();

}
// Observer abstract class

abstract class Observer {

protected Subject subject;

public abstract void update();

// Concrete Observer

class HexObserver extends Observer {

public HexObserver(Subject subject) {

[Link] = subject;

[Link](this);

public void update() {

[Link]("Hex String: " +


[Link]([Link]()));

// Usage

public class Main {

public static void main(String[] args) {

Subject subject = new Subject();

new HexObserver(subject);

[Link](15); // Outputs: Hex String: f

[Link](32); // Outputs: Hex String: 20

}
}

```

Observers automatically get updates from the subject when its state
changes, making it easy to sync dependent objects[3][5][6].

***

Each of these examples demonstrates a classic design pattern in Java and


how they help solve practical programming problems with reusable,
structured solutions[1][2][3][4][5][6].

Citations:

[1] Example: Java Singleton Class Syntax [Link]


programming/singleton

[2] Adapter Design Pattern in Java


[Link]
java

[3] Observer Pattern Tutorial with Java Examples


[Link]

[4] Adapter Software Pattern Java Examples


[Link]

[5] Observer Design Pattern in Java - Tutorial


[Link]

[6] Observer in Java / Design Patterns [Link]


patterns/observer/java/example

[7] Java Singleton Design Pattern Best Practices with Examples


[Link]
pattern-best-practices-examples

[8] java - example for Singleton pattern


[Link]
[9] Singleton Method Design Pattern in Java
[Link]

[10] Adapter Design Pattern in Java [Link]


design/adapter-design-pattern-in-java/

You might also like