[JAVA] Various design patterns

It is a memorandum of design patterns explained in as simple words as possible so that even a confused person can understand it. I'm studying design patterns, so I'll add them when I notice.

Strategy pattern

A pattern that allows you to classify and exchange parts where the algorithm changes

For example, it can be simplified when the behavior is switched by a switch statement or when the amount of code is large. As an image, in the game, the [caller] is a difficult / easy mode selection screen, and Argo A and Argo B are difficult / easy processing, respectively.

図2.png

Template method pattern

Boil the common parts to make a template (type). I think I posted it before.

Make the common part an abstract class, and create different instances for different parts and change their behavior. Expressed in yakitori, when there is a peach with a taste and a peach with a sauce, the common chicken [peach] and grilling action is made into an abstract class. An implementation pattern that separates the'salt'or'sauce' or seasoning process when it is finally finished. It's not a big design pattern, but I feel that it is always used in general object-oriented programming.

Singleton pattern

Probably the first famous one to hear. Limit object instantiation to one and not create more than one. There are various implementation methods. (Even if it seems that there are multiple, it includes those that refer to the same place in the end)

Singleton class
-Member variables
+Instance acquisition
-processing

The most basic implementation method //直接Qiitaのエディタに書いただけなので後日確認します。。。

public class Singleton {
public:
//Only instance created by static at class initialization
static Singleton& getInstance(){
static Singleton myself;
return myself;
}
    //Private so that it cannot be generated from the outside
    private:
    Singleton(){} 
}

Recommended Posts

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