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

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

Liskov Substitution Principle

Liskov Substitution Principle References Liskov Substitution Principle (Wikipedia) LSP article (Uncle Bob) Design by Contract (Wikipedia) Introduction Previously, the core mechanism of OCP (Open/Closed Principle) was to use abstraction and polymorphism. By using inheritance, you could create derived classes from an abstract base class. So, what design rules govern this kind of special inheritance? What are the characteristics of the best inheritance hierarchies? What causes hierarchies that do not conform to OCP? ...

October 11, 2020

Open Closed Principle

Open-Closed Principle Classes, modules, and functions should be open for extension but closed for modification. - MEYER Bertrand(1988) Object-Oriented Software Construction Allow extension, but do not allow modification. In other words, a class should be extensible without modification. How to Implement the Principle There are two methods. Both use generalization, but differ in their goals, techniques, and results. 1. Meyer’s Open-Closed Principle Method Implement using inheritance. Explanation By using inheritance, the parent class can be implemented through inheritance, so it is open for extension. Since the parent class is accessed through the child class, the parent class can be hidden and thus closed for modification. ...

October 11, 2020

Prototype Pattern

What is the Prototype Pattern? The Prototype Pattern is a creational design pattern. It enables the creation of new objects by copying an existing object, rather than creating them from scratch via their class constructors. This helps decouple code from specific classes. Prototype: Common vs Software Usage When I first encountered the Prototype Pattern, I was confused. The word “prototype” in daily use has a slightly different meaning, so let’s clarify that first. ...

October 11, 2020