and as well as , , , which have many great System design courses
2. Factory Method Pattern
The Factory Method pattern defines an interface for creating an object, but lets subclasses alter the type of objects that will be created.
It provides an interface for creating instances of a class, with its subclasses deciding which class to instantiate.
And, here is the sample code for this pattern:
public interface Product {
void create();
}
public class ConcreteProduct implements Product {
@Override
public void create() {
// Implementation
}
}
public interface Creator {
Product factoryMethod();
}
public class ConcreteCreator implements Creator {
@Override
public Product factoryMethod() {
return new ConcreteProduct();
}
}
Impact
This design pattern promotes loose coupling by allowing the client code to interact with the objects through interfaces, making it easier to extend and maintain.
Here is a UML diagram of Factory method pattern:
4. Strategy Pattern
The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. It allows a client to choose the appropriate algorithm at runtime.
Here is the code example of this pattern:
public interface PaymentStrategy {
void pay(int amount);
}
public class CreditCardPayment implements PaymentStrategy {
@Override
public void pay(int amount) {
// Implementation
}
}
public class PayPalPayment implements PaymentStrategy {
@Override
public void pay(int amount) {
// Implementation
}
}
public class ShoppingCart {
private PaymentStrategy paymentStrategy;
public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
this.paymentStrategy = paymentStrategy;
}
public void checkout(int amount) {
paymentStrategy.pay(amount);
}
}
Impact
This design pattern allows algorithms to vary independently from clients using them, promoting flexibility and ease of algorithm replacement.
Here is the UML diagram of Strategy design pattern in Java:
6. Adapter Pattern
The Adapter pattern allows incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces, making them compatible without changing their code.
Here is a code example of Adapter pattern in Java:
public interface Target {
void request();
}
public class Adaptee {
public void specificRequest() {
// Implementation
}
}
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
this.adaptee = adaptee;
}
@Override
public void request() {
adaptee.specificRequest();
}
}
Impact
This pattern facilitates the integration of existing systems by allowing them to work together despite incompatible interfaces.
Here is the UML diagram of Adapter pattern in Java:
8. Composite Pattern
The Composite pattern composes objects into tree structures to represent part-whole hierarchies. It allows clients to treat individual objects and compositions of objects uniformly.
Here is a code example for implementing Composite pattern in Java:
public interface Component {
void operation();
}
public class Leaf implements Component {
@Override
public void operation() {
// Implementation
}
}
public class Composite implements Component {
private List<Component> components = new ArrayList<>();
public void addComponent(Component component) {
components.add(component);
}
@Override
public void operation() {
for (Component component : components) {
component.operation();
}
}
}
**Impact
**This pattern greatly simplifies the client code by allowing it to treat individual objects and compositions uniformly.
Here is the UML diagram of Composite pattern in Java:
10. State Pattern
The State pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
Here is the code example of state design pattern in Java:
public interface State {
void handle();
}
public class ConcreteStateA implements State {
@Override
public void handle() {
// Implementation for state A
}
}
public class ConcreteStateB implements State {
@Override
public void handle() {
// Implementation for state B
}
}
public class Context {
private State currentState;
public void setState(State state) {
currentState = state;
}
public void request() {
currentState.handle();
}
}
Impact
The state design pattern Simplifies complex object behavior by allowing it to change its internal state dynamically.
Here is the UML diagram of State design pattern in Java:
Explore practical implementations of design patterns in Java.
Prepare for object-oriented programming interviews with real-world scenarios.
A rich library of design pattern examples and explanations.
image_credit --- twitter

SOCIAL SHARE CARD GENERATOR