Prompt Title: Design Patterns in Java

Created 1 year ago
Votes 0
Views 66 times
0
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.

  1. 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.
java
public class Singleton { private static Singleton instance; private Singleton() { } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
  1. 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"); } }
  1. 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.
java
import 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()); } }
  1. 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

avatar
Elvern Neylmav
Shared 1 prompt
Created 1 year ago

Leave a Comment

Related Tag Prompts

94
0
392
11
151
0
Docker Pro
1 year ago 2023-01-29 06:08:59 AIPRM
308
0
MySQL Marvel
1 year ago 2023-02-13 00:05:32 AIPRM
260
0
Ansible Expert
1 year ago 2023-02-01 09:23:50 AIPRM
193
0
PostgreSQL Pro
1 year ago 2023-02-07 03:45:19 AIPRM
114
0
270
1
Dockerize me
1 year ago 2023-02-17 08:27:58 Chad Thompson-Smith
52
0
Windows Wizard
1 year ago 2023-01-21 13:43:15 AIPRM
163
0
Linux Legend
1 year ago 2023-01-22 12:08:21 AIPRM
325
0
MacOS Master
1 year ago 2023-01-22 10:29:36 AIPRM