Study GoF design patterns

Introduction

[Introduction to design patterns learned in Java language](https://www.amazon.co.jp/%E5%A2%97%E8%A3%9C%E6%94%B9%E8%A8%82%E7%89 % 88Java% E8% A8% 80% E8% AA% 9E% E3% 81% A7% E5% AD% A6% E3% 81% B6% E3% 83% 87% E3% 82% B6% E3% 82% A4 % E3% 83% B3% E3% 83% 91% E3% 82% BF% E3% 83% BC% E3% 83% B3% E5% 85% A5% E9% 96% 80-% E7% B5% 90% I read E5% 9F% 8E-% E6% B5% A9 / dp / 4797327030), but the content did not come to my mind when I read it as it was, so it was a knack for learning or studying I tried to write an article about the summarized results.

Learning tips

List of 23 GoF design patterns to be used in Java (with summary comments for each pattern) ↑ I have referred to this site considerably. Especially useful are the 4 categories by level. For me, who was wondering "I don't know where to start" and "Do you really use this pattern?", The priority of learning has made me much better. Here is a table of learning priorities x pattern objectives.

If you have a good sense of programming, you can use it naturally without being told The name of the pattern itself is famous and is used here and there It's hard to come up with on your own, but once you learn it, programming is surprisingly open. Things that are difficult to understand and use
Generate -Builder: Subdivided initialization procedure -Factory Method: Dynamically select subclass
・ Singleton: 1 instance guaranteed
・ Abstract Factory: Factory of the factory
-Prototype: Pass a copy
Construction ・ Adapter: Inheritance/Wrapper by delegation
-Decorator: Delegated and intentional hook
・ Facade: Procedure for using multiple classes
・ Proxy: Sneak hook -Bridge: Hierarchical separation of extension and implementation
・Composite:再帰ツリーConstruction
・ Flyweight: Factory with cash
Behavior -Template Method: Child embodies processing fragment
・ Strategy: Algorithm switching
・ Mediator: Star-shaped interaction
-Observer: Event listener
・ Iterator: Processes side by side in order
-State: State object
-Command: Task queue and stack
-Memento: State getters and setters
・ Interpreter: Execution of original language
・ Chain of Responsibility: Help ship network
・ Visitor: Convenient scanner with structure

I cleaned this up from the left side. It was very easy to learn because the pattern that came into my head quickly and was familiar to me somewhere → the pattern was complicated and unfamiliar. Also, if you are aware of which of the "purposes of the pattern" it corresponds to (generation / structure / behavior), it is good because you will not lose track of what you are doing now.

Summary of each pattern

・ In short, what kind of pattern is this? ・ When is this pattern used? What are the benefits? I tried to summarize each pattern with the two axes.

If you have a good sense of programming, you can use it naturally without being told

To put it simply? When do you use it? What are the benefits?
Builder Separation of processing flow and specific processing Connection between use case layer and infrastructure layer (dependency inversion)
Adapter Wrapper class Anti-corruption layer
Decorator Function customization by identifying core and additional functions Infinite variations can be created depending on the combination of additional functions
Facade Providing API as a window Concealment of processing
Template Method Inheritance Concealment of processing
Strategy Algorithm separation You can switch algorithms
Mediator Centralized management of multi-class cooperation Organize interactions between classes (traffic maintenance)

The name of the pattern itself is famous and is used here and there

To put it simply? When do you use it? What are the benefits?
Factory Method Define instantiation and usage methods on the abstract class side Separation of framework and implementation
Singleton Guaranteed 1 instance Implementation that considers side effects on the premise of one instance
Proxy Take over heavy processing You will be able to do things like lazy road
Observer Notify and process class state changes Asynchronous processing (Pub-Sub) will be able to
Iterator Sequential processing of aggregate elements Separation of sequential processing/Stylized

It's hard to come up with on your own, but once you learn it, programming is surprisingly open.

To put it simply? When do you use it? What are the benefits?
Bridge Hierarchical separation of extensions and implementations transfer/You can think about the meaning of inheritance separately for each layer (basic ↔︎ extension)/Abstract ↔︎ implementation)
Composite Recursive tree structure by identifying the container and contents Realization of recursive structure (tree structure)
State Express the state in a class and switch (state transition) to use Conditional branching by if statement or switch statement becomes unnecessary
Command Express the instruction as an object and execute()To be able to run with Flexible management of tasks by utilizing queues and stacks/Can be executed
Memento Saving and restoring state Access (acquire) state without breaking the encapsulation/Can be updated)
Interpreter Parsing your own language Parsing by dropping your own language (defined by BNF) into a class/Can do
Chain of Responsibility Build a chain of processors Flexible processing can be expressed by rearranging the chain
Focus on your work (don't worry about what other elements of the chain are responsible for)
Visitor Work by walking across data structures Processing can be separated from the data structure

Things that are difficult to understand and use

To put it simply? When do you use it? What are the benefits?
Abstract Factory Define methods for instantiating and using the entire structure on the abstract class side Separation of framework and implementation
Prototype Clone the registered instance()Copy and pass Hide instantiation and free from class name (new clause) constraints
Flyweight Pool the instance once created and reuse it Memory usage can be reduced

in conclusion

Studying design patterns will result in memorization of 23 patterns just by doing it, but you can create a good learning pace by creating chunks by level or purpose. Also, because design patterns are the basis of all object-oriented programming, the same concept may appear in different practices in different terms (for example, the Adapter pattern is what DDD calls an anti-corruption layer). Will appear). If you study in relation to what you already know, you can think of it as a relationship within one big framework, not just a memorization of individual matters, so you can establish it efficiently and in a form that is easy to draw out as knowledge. I think.

Recommended Posts

Study GoF design patterns
Java Design Patterns
Introduction to design patterns (introduction)
Introduction to Design Patterns (Builder)
Read design patterns in Ruby
Introduction to Design Patterns (Composite)
Introduction to design patterns (Flyweight)
Why design patterns are needed
[Java] Summary of design patterns
Introduction to Design Patterns (Strategy)
Introduction to Design Patterns (Abstract Factory)
Important design patterns to improve maintainability