# The 23 Design Patterns by GoF ## Links - [The 23 well-known GoF design pattern](https://buihuycuong.medium.com/the-23-gang-of-four-design-patterns-974ae8d1a957) - [Design Pattern 系列文](https://waynecheng.coderbridge.io/2021/01/09/DesignPattern-Preface/) - [Design Pattern 鐵人賽](https://ithelp.ithome.com.tw/articles/10216587) 以下圖片來自 [大話設計模式](https://github.com/3masterplus/book/blob/master/%E5%A4%A7%E8%AF%9D%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F(%E5%B8%A6%E7%9B%AE%E5%BD%95%E5%AE%8C%E6%95%B4%E7%89%88).pdf)。 ## UML Class Diagram ![image](https://hackmd.io/_uploads/SyZg6VGG1l.png) ### Dependency (Use-a) - Briefly interacts, use its function and maybe only once - Do not hold reference ### Association (Has-a) - Mostly used - Association Qualifier (Hash Table, IRS + Tax ID <-> Person) - Hold a reference of each other, interact in a given period of time ### Aggregation (Owns-a, Your things -> You) - Recursive case: with base node - If one of them is missing, another might be unaffected - Actively Instantiate (Compared to Association) ### Composition (Is-part-of-a, Hand -> You) - Strong and seldomly used - If one of them is missing, then the other one will be missing as well - Need to be responsible for life cycle of its component ### Generalization (Is-a, Animal <- Cat) - Inheritance relationship ## Design Process from J Lee Encapsulation, Abstraction, Delegation ![image](https://hackmd.io/_uploads/Hyt9Pwx-Je.png) - Encapsulation: Encapsulate what varies - Abstraction: Program to an interface, not to implementation - Delegation: Depend on Abstraction, not on concrete class ## Introduction The Gang of Four (GoF) design patterns refer to the 23 design patterns introduced in the book "Design Patterns: Elements of Reusable Object-Oriented Software" by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. These patterns are categorized into three main groups based on their purpose: ## Categories 1. Creational Patterns: These patterns deal with the process of object creation, trying to make the creation process more flexible and dynamic. * Abstract Factory: Provides an interface for creating families of related or dependent objects without specifying their concrete classes. ![image](https://hackmd.io/_uploads/S1gZumzf1g.png) ![image](https://hackmd.io/_uploads/r1bf_mzGke.png) * Builder: Separates the construction of a complex object from its representation, allowing the same construction process to create different representations. ![image](https://hackmd.io/_uploads/r1W_D7fG1l.png) ![image](https://hackmd.io/_uploads/BJOKDmzGJl.png) * Factory Method: Defines an interface for creating objects, but lets subclasses alter the type of objects that will be created. ![image](https://hackmd.io/_uploads/H13QEQMf1x.png) ![image](https://hackmd.io/_uploads/HycVVmzfkg.png) * Prototype: Creates new objects by copying an existing object (the prototype). ![image](https://hackmd.io/_uploads/BkfaVQGGJg.png) ![image](https://hackmd.io/_uploads/r1paV7zfJl.png) * Singleton: Ensures that a class has only one instance and provides a global point of access to it. ![image](https://hackmd.io/_uploads/HJHp3QGM1l.png) ![image](https://hackmd.io/_uploads/SJnThQGMkx.png) 2. Structural Patterns: These patterns are concerned with the composition of classes or objects to form larger structures, focusing on how to ensure relationships between entities are maintained. * Adapter: Allows incompatible interfaces to work together by converting one interface to another that a client expects. ![image](https://hackmd.io/_uploads/Sy_iOmGfkl.png) ![image](https://hackmd.io/_uploads/SyEnuXfMyl.png) * Bridge: Decouples an abstraction from its implementation so that the two can vary independently. ![image](https://hackmd.io/_uploads/r1yHamzf1e.png) ![image](https://hackmd.io/_uploads/By6rTQfzke.png) * Composite: Composes objects into tree-like structures to represent part-whole hierarchies. ![image](https://hackmd.io/_uploads/H1SmYmGG1l.png) ![image](https://hackmd.io/_uploads/r1XVtmfMkg.png) * Decorator: Attaches additional responsibilities to an object dynamically, without affecting other objects of the same class. ![image](https://hackmd.io/_uploads/HyU1GXMfkl.png) ![image](https://hackmd.io/_uploads/SyLWf7zM1x.png) * Facade: Provides a simplified interface to a complex subsystem of classes. ![image](https://hackmd.io/_uploads/rJoGvXGz1x.png) ![image](https://hackmd.io/_uploads/BJPQvmfz1x.png) * Flyweight: Uses sharing to support large numbers of fine-grained objects efficiently. ![image](https://hackmd.io/_uploads/rkVlkVGM1g.png) ![image](https://hackmd.io/_uploads/Sk6gkNzMkx.png) * Proxy: Provides a surrogate or placeholder for another object to control access to it. ![image](https://hackmd.io/_uploads/r1HHzmMzyg.png) ![image](https://hackmd.io/_uploads/rJ1IfQzMyl.png) 3. Behavioral Patterns: These patterns are concerned with communication between objects, focusing on the interaction and responsibilities between them. * Chain of Responsibility: Allows a request to be passed along a chain of handlers until one of them handles it. ![image](https://hackmd.io/_uploads/SJHWCXzM1g.png) ![image](https://hackmd.io/_uploads/HJgzAmffyg.png) * Command: Encapsulates a request as an object, allowing parameterization of clients with queues, requests, and operations. ![image](https://hackmd.io/_uploads/BJKhTQGGkl.png) ![image](https://hackmd.io/_uploads/ryfaTXfzyg.png) * Interpreter: Implements a specialized language and interprets sentences of that language. ![image](https://hackmd.io/_uploads/rJNE1Eff1x.png) ![image](https://hackmd.io/_uploads/By-B1EfM1l.png) * Iterator: Provides a way to access elements of an aggregate object sequentially without exposing its underlying representation. ![image](https://hackmd.io/_uploads/HkRknXGz1l.png) ![image](https://hackmd.io/_uploads/ryCe27fMkg.png) * Mediator: Defines an object that controls how a set of objects interact, centralizing communication. ![image](https://hackmd.io/_uploads/ByLD07ffye.png) ![image](https://hackmd.io/_uploads/H1wdRmGfJe.png) * Memento: Captures the internal state of an object without exposing its implementation, allowing it to be restored later. ![image](https://hackmd.io/_uploads/rk7WFQfGyx.png) ![image](https://hackmd.io/_uploads/SJRbK7Gz1x.png) * Observer: Defines a one-to-many dependency, so when one object changes state, all its dependents are notified and updated automatically. ![image](https://hackmd.io/_uploads/BJYTD7zMkl.png) ![image](https://hackmd.io/_uploads/HJSy_QMGkg.png) * State: Allows an object to alter its behavior when its internal state changes, making it appear as if the object changed its class. ![image](https://hackmd.io/_uploads/Bk6QuXfM1x.png) ![image](https://hackmd.io/_uploads/Sy5V_XGzkx.png) * Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. ![image](https://hackmd.io/_uploads/rJIygXfGJe.png) ![image](https://hackmd.io/_uploads/r1QXfmGz1g.png) * Template Method: Defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm. ![image](https://hackmd.io/_uploads/BJ76I7Mzyl.png) ![image](https://hackmd.io/_uploads/H16a87MzJe.png) * Visitor: Allows you to define new operations on elements of an object structure without changing the elements themselves. ![image](https://hackmd.io/_uploads/HyvF1EMzyg.png) ![image](https://hackmd.io/_uploads/B1_skVGfJg.png) ## Summary of GoF 23 Patterns * Creational Patterns: Deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. * Structural Patterns: Deal with the composition of classes and objects, focusing on how to make larger structures more flexible. * Behavioral Patterns: Deal with the interaction between objects, focusing on how responsibilities are distributed and how objects collaborate. These patterns provide well-established solutions for common design problems, making it easier to create reusable, maintainable, and scalable software systems. # SOLID * Single Responsibility Principle (SRP): A class should have only one reason to change, meaning it should only have one job or responsibility. ![image](https://hackmd.io/_uploads/SJCql7zG1e.png) * Open/Closed Principle (OCP): Software entities should be open for extension but closed for modification, allowing behavior changes without altering existing code. ![image](https://hackmd.io/_uploads/SkDaemMMyl.png) * Liskov Substitution Principle (LSP): Objects of a superclass should be replaceable with objects of a subclass without altering the correctness of the program. ![image](https://hackmd.io/_uploads/B17kbQfG1e.png) * Interface Segregation Principle (ISP): Clients should not be forced to depend on interfaces they do not use, favoring smaller, specific interfaces over large, general-purpose ones. * Dependency Inversion Principle (DIP): High-level modules should not depend on low-level modules; both should depend on abstractions to promote loose coupling. ![image](https://hackmd.io/_uploads/SJP0g7zGJg.png) # Others * Law of Demeter ![image](https://hackmd.io/_uploads/HJalw7Gfye.png) * Composition/Aggregate Reuse Principle (CARP) ![image](https://hackmd.io/_uploads/BJLgaQMGJe.png)