Mediator Pattern

Mediator Pattern Reference https://refactoring.guru/design-patterns/mediator Definition Behavioral design pattern A pattern about how objects communicate within a group. A design pattern that restricts direct communication between objects and forces them to communicate through a mediator object. Situation - Communication Between Components in a Dialog Imagine a dialog for creating and editing a customer’s profile. This dialog contains various form controls, e.g., text fields, checkboxes, buttons. Key Point How should components communicate within a form? ...

October 12, 2020

Memento Pattern

Memento Pattern Reference https://refactoring.guru/design-patterns/memento memento: a souvenir to remember a person or place Definition Behavioral design pattern A pattern that encapsulates the behavior of an object and delegates requests to the object. A design pattern that encapsulates implementation details while allowing you to save or restore the previous state of an object. Also called the Snapshot Pattern. Situation - Implementing Undo Feature in an Editor Program Imagine an editor program. This program allows not only text editing but also text decoration and image insertion. Problem How can we implement the undo feature? To implement the undo feature… Before performing an operation, save the state of all objects and put them in storage. Later, when the user decides to revert, retrieve the state of the objects from storage. Set the app’s current state based on that state. How should we read the object’s state at this time? ...

October 12, 2020

Proxy Pattern

References https://refactoring.guru/design-patterns/proxy https://en.wikipedia.org/wiki/Proxy_pattern proxy: agent, substitute What is the Proxy Pattern? Structural design pattern By placing an agent in front of the real service, the client’s access to the real service can be controlled by the agent. Situation - When do you need to control access to the real service? There is an object that can consume a lot of resources by connecting to the DB. This object is executed occasionally. To address this, you might consider lazy initialization. ...

October 12, 2020

Abstract Factory Pattern

References https://refactoring.guru/design-patterns/abstract-factory What is the Abstract Factory? It is a creational design pattern. It is a pattern that allows you to create a family of related objects without specifying their concrete classes. Original English Abstract Factory is a creational design pattern that 1) lets you produce 2) families of related objects 3) without specifying their concrete classes. let you produce: That is, the client depends on the factory to create products. ...

October 11, 2020

Adapter Pattern

Source https://refactoring.guru/design-patterns/adapter What is the Adapter pattern? Structural pattern A design pattern that allows two incompatible interfaces to collaborate. Situation Goal We want to create a stock market monitoring application. Method Receive stock data in XML format. Analyze it using an Analytics Library and display graphs. Problem However, there is a problem. The Analytics Library accepts data in JSON format for analysis. In other words, the format of the stock data provider and the format of the program that analyzes the data do not match. ...

October 11, 2020

Bridge Pattern

References https://refactoring.guru/design-patterns/bridge https://en.wikipedia.org/wiki/Bridge_pattern Design Patterns: Elements of Reusable Object-Oriented Software (link) https://archive.org/details/designpatternsel00gamm/page/151 What is the Bridge Pattern? Structural design pattern Guru Definition A pattern that separates a large class or a set of highly related classes into two hierarchies. GoF-DesignPattern Definition A pattern that decouples an abstraction from its implementation so that the two can vary independently. Abstraction and Implementation Abstraction and implementation are exactly as Robert Martin described. In the explanation of DIP, Keyboard and Printer correspond to implementation, and Copy can be seen as corresponding to abstraction. That is, Copy is a high-level policy corresponding to the inherent abstract part of the application. Recall that the low-level policy that concretely performs the abstract content of copy was keyboard and printer. The Bridge Pattern goes further, making abstraction an abstract class to allow for the expansion of high-level policies. Analogy - Subtitle (Humans think abstractly) Pick up a spoon and eat rice. Spoon -> It could be a spoon from a restaurant or a spoon from home. Rice could also be rice cooked at a specific time in a restaurant, or leftover rice at home. To satisfy hunger, one performs the act (abstraction) of picking up a spoon and eating rice. ...

October 11, 2020

Builder Pattern

References https://refactoring.guru/design-patterns/builder (Builder pattern explanation) https://projectlombok.org/features/Builder (Lombok) What is Builder? Creational design pattern A design pattern that allows you to construct complex objects step-by-step. Allows you to create different types of objects with the same construction code. Situation You need to create a complex object with many fields and nested objects. Not Recommended Method 1 - Implementation via Class Inheritance Method Description Create a base class Suppose you create a base class called House. This class has a roof, windows, and exterior walls. You make it with specific field values. In other words, you create classes like CabinHouse and BrickHouse. For example, the exterior wall of CabinHouse is wood, and for BrickHouse, it is brick. For extension - create a subclass If you want to make a cabin house with a storage room, you inherit from CabinHouse and add a storage room. Problems You end up with countless subclasses Because you have to create a new subclass for every new parameter. The hierarchy can become deep Inheritance forms a hierarchical structure. If you want to extend not with an annex but with a style (e.g., modern), you have to make it one level deeper. Not Recommended Method 2 - Giant Constructor Method Description Write a constructor that takes all possible parameters. Problems You have to consider unnecessary parameters and put them as arguments As shown on the left of the figure above, even when creating a regular house with only a storage room, you have to consider other options and put null for them. It’s hard to figure out the meaning of each argument When there are many parameters, it’s hard to know what each argument means. Solution - Builder Pattern The Builder pattern moves the object creation code outside the class to be created. It delegates the creation responsibility to individual objects called builders. These builders: ...

October 11, 2020

Dependency Inversion Principle

Previously, we learned about OCP (Open/Closed Principle) and LSP (Liskov Substitution Principle). OCP is the principle of allowing changes but preventing modifications, and LSP is the principle that a base class can be replaced by its subclass. Here, I will talk about the structure that arises from strictly applying these two principles. This structure itself becomes a principle, and its name is the Dependency Inversion Principle. What’s Wrong with Software? As developers, we often drive ourselves into bad design. Why does this happen? The core of the problem is that we haven’t defined what bad design is. ...

October 11, 2020

Factory Method Pattern

References https://refactoring.guru/design-patterns/factory-method What is the Factory Method? Creational design pattern Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. Situation There is an existing logistics app that uses trucks. As the business grows, you want to add ships to support maritime logistics services. However, Truck is tightly coupled to the entire system, making it difficult to add Ship. Solution The main point of the problem is: ...

October 11, 2020

Interface Segregation Principle

Interface Segregation Principle Introduction Terminology client, user: An object, function, or class that uses a specific class or object. What is ISP? ISP stands for Interface Segregation Principle, which means interfaces should be separated. Problem If this principle is not followed, you end up with a fat (or polluted) interface. Such interfaces usually have poor cohesion. In other words, unrelated functions are grouped together in a single interface. Goal According to ISP, these fat interfaces should be split. Clients using these interfaces should not see them as a single class, but rather as abstract base classes with cohesive interfaces. ...

October 11, 2020