Human: Explain to me these design patterns in Object Oriented Programming:
- Singleton
- Visitor
- Observer
- Factory
Please implement it in Java!
ChatGPT:
I will provide a brief explanation of each design pattern and then provide an example implementation in Java.
- Singleton The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. This is useful when you want to coordinate actions across the system.
javapublic class Singleton {
private static Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
- Visitor The Visitor pattern allows you to add new operations to existing class hierarchies without modifying them. It provides a way to separate an algorithm from the objects it operates on.
java// Element interface
public interface Element {
void accept(Visitor visitor);
}
// Concrete elements
public class ConcreteElementA implements Element {
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
public class ConcreteElementB implements Element {
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
// Visitor interface
public interface Visitor {
void visit(ConcreteElementA element);
void visit(ConcreteElementB element);
}
// Concrete visitor
public class ConcreteVisitor implements Visitor {
public void visit(ConcreteElementA element) {
System.out.println("Visited ConcreteElementA");
}
public void visit(ConcreteElementB element) {
System.out.println("Visited ConcreteElementB");
}
}
- Observer The Observer pattern defines a one-to-many dependency between objects so that when one object (subject) changes state, all its dependents (observers) are notified and updated automatically.
javaimport java.util.ArrayList;
import java.util.List;
// Subject
public class Subject {
private List<Observer> observers = new ArrayList<>();
public void addObserver(Observer observer) {
observers.add(observer);
}
public void removeObserver(Observer observer) {
observers.remove(observer);
}
public void notifyObservers() {
for (Observer observer : observers) {
observer.update();
}
}
}
// Concrete subject
public class ConcreteSubject extends Subject {
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
notifyObservers();
}
}
// Observer interface
public interface Observer {
void update();
}
// Concrete observer
public class ConcreteObserver implements Observer {
private ConcreteSubject subject;
public ConcreteObserver(ConcreteSubject subject) {
this.subject = subject;
subject.addObserver(this);
}
public void update() {
System.out.println("State changed to: " + subject.getState());
}
}
- Factory The Factory pattern provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
java// Product interface
public interface Product {
void doSomething();
}
// Concrete products
public class ConcreteProductA implements Product {
public void doSomething() {
System.out.println("ConcreteProductA");
}
}
public class ConcreteProductB implements Product {
public void doSomething() {
System.out.println("ConcreteProductB");
}
}
// Factory
public abstract class Factory {
public abstract Product createProduct();
}
// Concrete factories
public class ConcreteFactoryA extends Factory {
public Product createProduct() {
return new ConcreteProductA();
}
}
public class ConcreteFactoryB extends Factory {
public Product createProduct() {
return new ConcreteProductB();
}
}
These are basic implementations of the Singleton, Visitor, Observer, and Factory patterns in Java. There are more advanced and flexible ways
Add a comment