Design Pattern
Introduction :
A design pattern is a general reusable solution to a recurring problem in software design. It is a template or blueprint that can be applied to different situations to solve common design issues in a flexible and efficient way. Design patterns are not specific to a particular programming language or technology; instead, they are abstract concepts that can be adapted and implemented in various contexts.
Type of Design Pattern:
- Creational Patterns: These patterns are designed for class instantiation. They can be either class-creation patterns or object-creational patterns. Examples include the Singleton, Factory Method, and Abstract Factory patterns.
- Structural Patterns : These patterns are designed with regard to a class’s structure and composition. The main goal of most of these patterns is to increase the functionality of the class(es) involved, without changing much of its composition. Examples include the Adapter, Decorator, and Composite patterns.
- Behavioral Patterns : These patterns define how objects interact and communicate with each other. They focus on the delegation of responsibilities and the flow of control. Examples include the Observer, Strategy, and Command patterns.
In this post, we will go through Creational design pattern for each type.
Singleton Design Pattern :
The Singleton Design Pattern is a Creational pattern, whose objective is to create only one instance of a class and to provide only one global access point to that object.
Get sunny Gupta’s stories in your inbox
Join Medium for free to get updates from this writer.
This pattern is useful when exactly one object is needed to coordinate actions across the system. The Singleton pattern is implemented by defining a class that has only one instance and providing a global point of access to that instance.
Simple example of a Singleton pattern in Python :
class Singleton:
_instance = None # Class variable to store the singleton instance
def __new__(cls):
if cls._instance is None:
cls._instance = super(Singleton, cls).__new__(cls)
# Additional initialization can be done here
return cls._instance
# Example usage:
singleton_instance1 = Singleton()
singleton_instance2 = Singleton()
print(singleton_instance1 is singleton_instance2) # Output: TrueOutput: True. The _instance variable is used to store the single instance of the class.
. The ‘__new__' method is overridden to control the instantiation process. If the ‘_instance' is None, a new instance is created, and it is stored in the _instance variable. If an instance already exists, the existing instance is returned.
Using the Singleton pattern ensures that only one instance of the class is created, and any subsequent attempts to create new instances return the existing one. This can be useful in scenarios where a single point of control or coordination is required, such as managing a configuration object, a connection to a database, or a logging service.
In the next post we are going to Discuss about the Factory Method, and Abstract Factory patterns in Creational design pattern….
https://towardsdev.com/design-pattern-fc1d98ae9760
Thank you .






