MadAlgos Blog
Design patterns quick review
Design patterns
Design patterns are common solutions to recurring problems in software design. They help developers write code that is more readable, maintainable and reusable. In this blog post, we will explore three types of design patterns: creational, structural and behavioral.
Creational patterns deal with how objects are created and initialized. They provide a way to abstract the instantiation process and hide the implementation details from the client. Some examples of creational patterns are:
- Singleton: ensures that only one instance of a class exists and provides a global access point to it.
- Factory Method: defines an interface for creating an object, but lets subclasses decide which class to instantiate.
- Abstract Factory: provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Structural patterns deal with how classes and objects are composed and organized. They provide a way to simplify the structure of complex systems and improve their flexibility and extensibility. Some examples of structural patterns are:
- Adapter: allows classes with incompatible interfaces to work together by wrapping its own interface around that of an existing class.
- Bridge: decouples an abstraction from its implementation so that the two can vary independently.
- Composite: allows composing objects into tree structures to represent part-whole hierarchies and lets clients treat individual objects and compositions uniformly.
Behavioral patterns deal with how classes and objects interact and distribute responsibility. They provide a way to define the communication and control flow between different objects and encapsulate their behavior. Some examples of behavioral patterns are:
- Observer: defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- Strategy: defines a family of algorithms, encapsulates each one, and makes them interchangeable. It lets the algorithm vary independently from clients that use it.
- Command: encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
These are just some of the design patterns that can help you design better software solutions.