Home >Web Front-end >JS Tutorial >Singleton or Observable? Wrong Choice Could Cost You Your Promotion!
Design patterns are fundamental in creating well-structured and easy-to-maintain software. Among them, Singleton and Observable are often used in scenarios that require global state control and communication between different parts of the system. In this article, we will explore how these two standards work, when to use them, their differences, and provide practical examples of how to implement them.
The Singleton Pattern is a creational design pattern that ensures that a class has only one instance and provides a global point of access to that instance. This pattern is useful when you need a single object across the entire application, such as global settings, database connection, or log management.
The main feature of Singleton is that it restricts the class instance to a single object, ensuring that all instance requests return the same object. To achieve this, the pattern normally uses a static method that creates the instance only when it is requested for the first time, guaranteeing the unique creation and use of the object throughout the system.
In the diagram below, the sequence shows how the Singleton instance is created and accessed, ensuring it is unique.
The Singleton sequence diagram illustrates the interaction flow between the client and the Singleton class. The process begins with the client calling the static getInstance() method to obtain the Singleton instance. If the instance has not yet been created, Singleton creates a new instance and returns it. When the client calls the getInstance() method again, the same instance is returned, ensuring that there is only one instance of the Singleton object during the entire program execution.
class Singleton { private static instance: Singleton; private constructor() { } // Método para acessar a instância única public static getInstance(): Singleton { if (!Singleton.instance) { Singleton.instance = new Singleton(); } return Singleton.instance; } public showMessage(): string { return "Esta é a única instância!"; } } // Uso do Singleton const instance1 = Singleton.getInstance(); console.log(instance1.showMessage()); // "Esta é a única instância!" const instance2 = Singleton.getInstance(); console.log(instance1 === instance2); // true
The Observable Pattern is a behavioral design pattern that defines a one-to-many dependency between objects. In other words, when the state of an object (the "subject") changes, all its dependents (the "observers") are automatically notified. This pattern is widely used in systems where events and state changes need to be propagated between different components, such as graphical interfaces or monitoring systems.
The Observable pattern allows objects to "observe" changes in the state of an object and react to those changes. The pattern is based on three main components:
The Observable sequence diagram demonstrates how the pattern works with multiple observers. The Subject (or observed object) notifies all registered Observers when a state change occurs. Each Observer reacts to the notification, taking necessary actions based on the information received. The notification process is propagated so that all observers are updated simultaneously, keeping them synchronized with the state of the Subject. This pattern is useful when there are multiple components or parts of the system that need to be informed about changes in the state of an object.
class Singleton { private static instance: Singleton; private constructor() { } // Método para acessar a instância única public static getInstance(): Singleton { if (!Singleton.instance) { Singleton.instance = new Singleton(); } return Singleton.instance; } public showMessage(): string { return "Esta é a única instância!"; } } // Uso do Singleton const instance1 = Singleton.getInstance(); console.log(instance1.showMessage()); // "Esta é a única instância!" const instance2 = Singleton.getInstance(); console.log(instance1 === instance2); // true
Although both patterns serve to manage objects in a controlled way, their purposes and behaviors are very different:
Feature |
|
Observable |
|||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Objective |
Ensure that a class has only one instance. | Notify multiple objects about state changes. | |||||||||||||||
Instance | Only one instance is created and shared. | Several objects can be observers of a single subject. | |||||||||||||||
Main use | Exclusive resource management. | Notification of events and state changes. | |||||||||||||||
Example of use | Log management or global configuration. | Updating graphical interfaces or propagating events. |
The Singleton and Observable
patterns are fundamental in several situations in software development.The above is the detailed content of Singleton or Observable? Wrong Choice Could Cost You Your Promotion!. For more information, please follow other related articles on the PHP Chinese website!