Design patterns are the well proved solution of commonly occuring problems in software design.
A design pattern is a general reusable solution to a commonly occurring problem in software design. It is a proven approach that provides a template or guideline for solving a specific design problem while promoting good software design practices such as modularity, reusability, and maintainability. Design patterns capture best practices and design principles that have been developed and refined by experienced software developers over time. They represent solutions to recurring problems and provide a common vocabulary for developers to communicate and share their design ideas.
[!note] Design patterns are not specific to a particular programming language or technology. They are higher-level concepts that can be implemented in various languages and frameworks. By utilizing design patterns, developers can create more robust, modular, and maintainable software systems.Design patterns include creational patterns, which deal with object creation. structural patterns, which focus on object composition and organization. and behavioral patterns, which deal with the communication between objects and classes.
Creational design patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.
Read
Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.
Read the_blog.
Singleton works on the concept of one and only one instance of the object, which results in the global control of a resource. In simple words, the Singleton design pattern ensures that only one instance of the class will be created and that instance will have global access within the application.
When you want to ensure that only one instance of a class exists, for example, a single database object shared by different parts of the program, you should use the Singleton design pattern.
[x] You can be sure that a class has only a single instance.
[x] You gain a global access point to that instance.
[x] The singleton object is initialized only when it’s requested for the first time.
[ ] Violates the Single Responsibility Principle. The pattern solves two problems at the time.
[ ] The Singleton pattern can mask bad design, for instance, when the components of the program know too much about each other.
[ ] The pattern requires special treatment in a multithreaded environment so that multiple threads won’t create a singleton object several times.
[ ] It may be difficult to unit test the client code of the Singleton because many test frameworks rely on inheritance when producing mock objects. Since the constructor of the singleton class is private and overriding static methods is impossible in most languages, you will need to think of a creative way to mock the singleton. Or just don’t write the tests. Or don’t use the Singleton pattern.
Factory Method is a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.The Factory Method is a creational design pattern that provides a solution to create product objects without the need to specify their concrete classes during the creation process.
The Factory Method is used to conserve system resources by reusing existing objects instead of reconstructing them repeatedly.
Read the blog.
Abstract Factory is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.
Read the article.