Software Design Design Patterns
Software Design Design Patterns
awet.fesseha@rca.ac.rw
1/22/2025
1 Introduction
2 Creational patterns
1.1 Singleton Pattern:
1.2 Abstract Factory
1.3 Factory Method
3 2. Structural Patterns
2.1 Adapter
2.2 Bridge
2.3 Composite
4 3. Behavioral design pattern
3.1 Chain of Responsibility
3.2 Visitor
5 Conclusion
1 Creational patterns
2 Structural patterns
3 Behavioral patterns
Ensures that a class has only one instance and provides a global point
of access to it.
Ensure that a class has just a single instance. Why would anyone
want to control how many instances a class has? The most common
reason for this is to control access to some shared resource—for
example, a database or a file.
Provide a global access point to that instance. Remember those
global variables that you (all right, me) used to store some essential
objects? While they’re very handy, they’re also very unsafe since any
code can potentially overwrite the contents of those variables and
crash the app
Pros
You can be sure that a class has only a single instance.
You gain a global access point to that instance.
Useful when exactly one object is needed to coordinate actions across
the system.
cons
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.
Pros
The Factory pattern hides the object creation details, making the
code cleaner and more modular. The client only interacts with an
interface and doesn’t need to know the concrete classes.
Adding new types of objects is easy. You can create a new subclass
and update the factory method without changing existing code,
adhering to the Open/Closed Principle.
cons
Introducing a factory adds additional layers of abstraction, which can
make the code more complex, especially for simple object creation.
For straightforward applications with no complex object creation
requirements, a Factory pattern might be overkill and unnecessarily
increase the codebase.
Structural patterns are design patterns that deal with the composition
and organization of classes and objects. They help you define how
different components of your system interact and relate to each other.
Structural patterns can simplify your code by reducing the number of
classes, hiding the complexity of the internal structure, or providing a
common interface for different implementations.
Some examples of structural patterns are Adapter, Bridge,
Composite, Decorator, Facade, Flyweight, and Proxy.
Bridge is a structural design pattern that lets you split a large class
or a set of closely related classes into two separate
hierarchiesabstraction and implementationwhich can be developed
independently of eachother.
The Bridge pattern attempts to solve this problem by switching from
inheritance to the object composition.
What this means is that you extract one of the dimensions into a
separate class hierarchy, so that the original classes will reference an
object of the new hierarchy, instead of having all of its state and
behaviors within one class.