Middle
What types of design patterns do you know and can you give examples of their use?
sobes.tech AI
Answer from AI
Creational:
- Singleton: Ensures a class has only one instance and provides a global point of access to it.
- Factory Method: Defines an interface for creating an object but allows subclasses to decide which class to instantiate.
- Abstract Factory: Provides an interface for creating families of related or dependent objects without specifying their concrete classes.
- Builder: Separates the construction of a complex object from its representation so that the same construction process can create different representations.
- Prototype: Creates new objects by copying an existing object.
Structural:
- Adapter: Converts the interface of a class into another interface clients expect.
- Bridge: Decouples an abstraction from its implementation so that the two can vary independently.
- Composite: Composes objects into tree structures to represent hierarchies "part-whole". Lets clients treat individual objects and compositions uniformly.
- Decorator: Dynamically adds responsibilities to an object by wrapping it.
- Facade: Provides a unified interface to a set of interfaces in a subsystem.
- Flyweight: Uses sharing to support large numbers of fine-grained objects efficiently.
- Proxy: Provides a surrogate or placeholder for another object to control access to it.
Behavioral:
- Chain of Responsibility: Passes requests along a chain of handlers.
- Command: Encapsulates a request as an object, allowing parameterization of clients with different requests, queuing or logging requests.
- Interpreter: Defines a grammatical representation for a language and provides an interpreter for it.
- Iterator: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
- Mediator: Defines an object that encapsulates how a set of objects interact.
- Memento: Without violating encapsulation, captures and externalizes an object's internal state so that the object can be restored to this state later.
- Observer: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- State: Allows an object to alter its behavior when its internal state changes. The object will appear as if it changed its class.
- Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
- Template Method: Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses.
- Visitor: Represents an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements.
Usage examples:
// Singleton example: Logger
public class Logger {
private static Logger instance;
private Logger() {}
public static Logger getInstance() {
if (instance == null) {
instance = new Logger();
}
return instance;
}
public void log(String message) {
System.out.println("LOG: " + message);
}
}
// Factory Method example: Factory for creating different types of animals
interface Animal {
void makeSound();
}
class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Woof");
}
}
class Cat implements Animal {
@Override
public void makeSound() {
System.out.println("Meow");
}
}
class AnimalFactory {
public Animal createAnimal(String type) {
if ("dog".equalsIgnoreCase(type)) {
return new Dog();
} else if ("cat".equalsIgnoreCase(type)) {
return new Cat();
}
return null;
}
}
// Observer example: Subscribing to state changes
import java.util.ArrayList;
import java.util.List;
interface Observer {
void update(String message);
}
class Subject {
private List<Observer> observers = new ArrayList<>();
private String state;
public void attach(Observer observer) {
observers.add(observer);
}
public void setState(String state) {
this.state = state;
notifyObservers();
}
private void notifyObservers() {
for (Observer observer : observers) {
observer.update(state);
}
}
}
class ConcreteObserver implements Observer {
private String name;
public ConcreteObserver(String name) {
this.name = name;
}
@Override
public void update(String message) {
System.out.println(name + " received update: " + message);
}
}