The Factory Pattern is a creational design pattern used in object-oriented programming (OOP) to create objects without specifying the exact class of object that will be created. It provides a way to encapsulate the instantiation logic within a method or class, allowing the client code to use the factory method to create objects without knowing the specific implementation details.
The Singleton Pattern, on the other hand, is a creational design pattern that ensures a class has only one instance and provides a global point of access to that instance.
Here’s how you can implement the Singleton Pattern using an object-oriented programming language:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public class Singleton { private static Singleton instance; // Private constructor to prevent instantiation from outside private Singleton() {} // Static method to get the singleton instance public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } // Example method public void someMethod() { System.out.println("Some method is called."); } public static void main(String[] args) { // Example usage Singleton obj1 = Singleton.getInstance(); Singleton obj2 = Singleton.getInstance(); // Both references point to the same object System.out.println(obj1 == obj2); // Output: true obj1.someMethod(); // Output: Some method is called. } } |
Q,no 3
Q.no 4
Q,no 5
The Observer Pattern is a behavioral design pattern where an object (known as the subject) maintains a list of its dependents (observers) and notifies them of any state changes, usually by calling one of their methods.
Let’s consider an example of an online newspaper (the subject) and its subscribers (the observers). Whenever a new article is published, the newspaper notifies all its subscribers about the new article so they can read it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import java.util.ArrayList; import java.util.List; // Subject interface interface Subject { void registerObserver(Observer observer); void removeObserver(Observer observer); void notifyObservers(); } // Concrete subject class Newspaper implements Subject { private List<Observer> observers = new ArrayList<>(); private String latestArticle; public void setLatestArticle(String latestArticle) { this.latestArticle = latestArticle; notifyObservers(); } @Override public void registerObserver(Observer observer) { observers.add(observer); } @Override public void removeObserver(Observer observer) { observers.remove(observer); } @Override public void notifyObservers() { for (Observer observer : observers) { observer.update(latestArticle); } } } // Observer interface interface Observer { void update(String latestArticle); } // Concrete observer class Subscriber implements Observer { private String name; public Subscriber(String name) { this.name = name; } @Override public void update(String latestArticle) { System.out.println(name + ": New article published - " + latestArticle); } } // Main class public class ObserverPatternExample { public static void main(String[] args) { // Create subject (newspaper) Newspaper newspaper = new Newspaper(); // Create observers (subscribers) Observer subscriber1 = new Subscriber("John"); Observer subscriber2 = new Subscriber("Alice"); // Register observers to the subject newspaper.registerObserver(subscriber1); newspaper.registerObserver(subscriber2); // Publish a new article newspaper.setLatestArticle("How to implement Observer Pattern"); // Output: // John: New article published - How to implement Observer Pattern // Alice: New article published - How to implement Observer Pattern } } |