I want to address the following points in this project:
- What are design patterns and why are they important?
- In-depth theory and example use cases.
- Real-life working examples (C++, Java, Python, JavaScript)
- 📕 Design Patterns: Elements of Reusable Object-Oriented Software
- 📕 Software Design Patterns: Wikipedia
- 📕 Gang of Four - Cheatsheet
-
- Abstract Factory: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
- Builder: Separate the construction of a complex object from its representation so that the same construction process can create different representations.
- Factory Method: Define an interface for creating an object, but let subclasses decide which class to instantiate.
- Prototype: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
- Singleton: Ensure a class only has one instance, and provide a global point of access to it.
-
- Adapter (or Wrapper): Convert the interface of a class into another interface clients expect.
- Bridge (or Handle/Body): Decouple an abstraction from its implementation so that the two can vary independently.
- Composite: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
- Decorator (or Wrapper): Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
- Facade: Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
- Flyweight: Use sharing to support large numbers of fine-grained objects efficiently.
- Proxy (or Surrogate): Provide a surrogate or placeholder for another object to control access to it.
-
- Chain of Responsibility: Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
- Command: Encapsulate the request as an object, thereby letitng you parameterize clients with different requests, queue or log requests, and support undoable operations.
- Interpreter: Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
- Iterator: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
- Mediator: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
- Memento: Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.
- Observer: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
- State: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
- Strategy: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
- Template Method: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
- Visitor: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
Purpose | Design Pattern | Aspect(s) That Can Vary |
---|---|---|
Creational | Abstract Factory | Families of product objects. |
Builder | How a composite object gets created. | |
Factory Method | Subclass of object that is instantiated. | |
Prototype | Class of object that is instantiated. | |
Singleton | The sole instance of a class. | |
Structural | Adapter | Interface to an object. |
Bridge | Implementation of an object. | |
Composite | Structure and composition of an object. | |
Decorator | Responsibilities of an object without subclassing. | |
Facade | Interface to a subsystem. | |
Flyweight | Storage costs of objects. | |
Proxy | How an object is accessed; its location. | |
Behavioral | Chain of responsibility | Object that can fulfill a request. |
Command | When and how a request is fulfilled. | |
Interpreter | Grammar and interpretation of a language. | |
Iterator | How an aggregate's elements are accessed, traversed. | |
Mediator | How and which object interact with each other. | |
Memento | What private information is stored outside an object, and when. | |
Observer | Number of objects that depend on another object, how the dependent objects stay up to date. | |
State | States of an object. | |
Strategy | An algorithm. | |
Template Method | Steps of an algorithm. | |
Visitor | Operations that can be applied to object(s) without changing their class(es). |
- | - | - | Purpose | - |
---|---|---|---|---|
- | - | Creational | Structural | Behavioral |
Scope | Class | Factory Method | Adapter (class) | Interpreter, Template Method |
- | Object | Abstract Factory, Builder, Prototype, Singleton | Adapter (object), Bridge, Composite, Decorator, Facade, Flyweight, Proxy | Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Visitor |
-
Study patterns' intents and how they interrelate.
-
Examine causes of redesign.
-
Consider what should be variable in your design.
-
Creating an object by specifying a class explicitly. This commits you to a particular implementation instead of a particular interface. To avoid it, create objects indirectly.
- Design patterns: Abstract Factory, Factory Method, Prototype.
-
Dependence on specific operations. When you specify a particular operation, you commit to one way of satisfying a request. By avoiding hard-coded requests, you can make it easier to change the way a request gets satisfied both at compile-time and at run-time.
- Design patterns: Chain of Responsibility, Command.
-
Dependence on hardware and software platform. Software that depends on a particular platform will be harder to port to other platforms. It may even be difficult to keep it up to date on its native platform. It's important therefore to design your system to limit its platform dependencies.
- Design patterns: Abstract Factory, Bridge.
-
Dependence on object representations or implementations. Clients that know how an object is represented, stored, located, or implemented might need to be changed when the object changes. Hiding this information from clients keeps changes from cascading.
- Design patterns: Abstract Factory, Bridge, Memento, Proxy.
-
Algorithmic dependencies. Algorithms are often extended, optimized, and replaced during development and reuse. Objects that depend on an algorithm will have to change when the algorithm changes. Therefore algorithms that are likely to change should be isolated.
- Design patterns: Builder, Iterator, Strategy, Template Method, Visitor.
-
Tight coupling. Classes that are tightly coupled are hard to reuse in isolation, since they depend on each other. Tight coupling leads to monolithic systems, where you can't change or remove a class without understanding and changing many other classes. The system becomes a dense mass that's hard to learn, port, and maintain. Loose coupling increases portabiliy and makes that a system can be learned, ported, modified, and extended more easily.
- Design patterns: Abstract Factory, Bridge, Chain of Responsibility, Command, Facade, Mediator, Observer.
-
Extending functionality by subclassing. Every new class has a fixed implementation overhead (initialization, finalization, etc.). Defining a subclass also requires an in-depth understanding of the parent class. Subclassing can lead to an explosion of classes, because you might have to introduce many new subclasses for even a simple extension. Object composition in general and delegation in particular provide flexible alternatives to inheritance for combining behavior. On the other hand, heavy use of object composition can make designs harder to understand. Many design patterns produce designs in which you can introduce customized functionality just by defining one subclass and composing its instances with existing ones.
- Design patterns: Bridge, Chain of Responsibility, Composite, Decorator, Observer, Strategy.
-
Inability to alter classes conveniently. Sometimes you have to modify a class that can't be modified conveniently.
- Design patterns: Adapter, Decorator, Visitor.
✔️ Completed all Object-Oriented Design Patterns from GoF
✔️ Javascript examples
👉 Go examples
👉 Refactor Javascript examples using Typescript
👉 Java examples
👉 C++ examples