Object Assembling
Adapter
The Adapter pattern is a structural design pattern that allows objects with incompatible interfaces to work together.
// Target interface
interface Target {
void request();
}
// Adaptee (the class with an incompatible interface)
class Adaptee {
void specificRequest() {
System.out.println("Adaptee's specific request");
}
}
// Adapter class that implements the Target interface and delegates requests to the Adaptee
class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
// Client code
public class Main {
public static void main(String[] args) {
// Create an Adaptee object
Adaptee adaptee = new Adaptee();
// Create an Adapter object and pass the Adaptee to it
Target adapter = new Adapter(adaptee);
// Call the request method of the Target interface
adapter.request();
}
}
Bridge
The Bridge pattern is a structural design pattern that decouples an abstraction from its implementation, allowing both to vary independently.
Composite
The Composite pattern is a structural design pattern that allows you to compose objects into tree structures to represent part-whole hierarchies.
Decorator
The Decorator pattern is a structural design pattern that allows behavior to be added to individual objects dynamically, without affecting the behavior of other objects from the same class.
Facade
The Facade pattern is a structural design pattern that provides a simplified interface to a complex subsystem of classes, making it easier to use.
Flyweight
The Flyweight pattern is a structural design pattern that aims to minimize memory usage and improve performance by sharing common state among multiple objects instead of keeping it in each object individually.
Proxy
The Proxy pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it. It allows you to add an additional layer of indirection to manage the interaction between the client and the real subject object.
Last updated