DESIGN PATTERNS
Design patterns are typical solutions to commonly occurring problems in software
design. They are like pre-made blueprints that you can customize to solve a recurring
design problem in your code.
an algorithm always defines a clear set of actions that can achieve some goal, a pattern
is a more high-level description of a solution. The code of the same pattern applied to
two different programs may be different.
An analogy to an algorithm is a cooking recipe: both have clear steps to achieve a goal.
On the other hand, a pattern is more like a blueprint: you can see what the result and its
features are, but the exact order of implementation is up to you.
WHY DO WE NEED DESIGN PATTERNS
● Design patterns are a toolkit of tried and tested solutions to common problems in
software design. Even if you never encounter these problems, knowing patterns
is still useful because it teaches you how to solve all sorts of problems using
principles of object-oriented design.
● Design patterns define a common language that you and your teammates can
use to communicate more efficiently. You can say, “Oh, just use a Singleton for
that,” and everyone will understand the idea behind your suggestion. No need to
explain what a singleton is if you know the pattern and its name.
Key Characteristics of Design Patterns
Reusability: Patterns can be applied to different projects and problems, saving time
and effort in solving similar issues.
Standardization: They provide a shared language and understanding among
developers, helping in communication and collaboration.
Efficiency: By using these popular patterns, developers can avoid finding the solution
to same recurring problems, which leads to faster development.
Flexibility: Patterns are abstract solutions/templates that can be adapted to fit various
scenarios and requirements.
Types of Software Design Patterns
There are three types of Design Patterns:
Creational Design Pattern:- Focus on the process of object creation or problems
related to object creation. They help in making a system independent of how its objects
are created, composed and represented.
● Abstract Factory: Creates an instance of several families of classes
● Builder: Separates object construction from its representation
● Factory Method: Creates an instance of several derived classes
● Prototype: A fully initialized instance to be copied or cloned
● Singleton: A class in which only a single instance can exist
Note: The best way to remember Creational Pattern is by remembering ABFPS
(Abraham Became First President of States).
Singleton
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.
Singleton Design Pattern Principles
Single Instance: Singleton ensures that only one instance of the class exists throughout the
application.
Global Access: Provide a global point of access to that instance.
Lazy or Eager Initialization: Support creating the instance either when needed (lazy) or when
the class is loaded (eager).
Thread Safety: Implement mechanisms to prevent multiple threads from creating separate
instances simultaneously.
Private Constructor: Restrict direct instantiation by making the constructor private, forcing the
use of the access point