Java design pattern summary

Java design pattern memorandum

  1. Iterator

The loop can be done independently of the data. Whether it's a List, an array, a Set, or whatever, it's a similar call.

  1. Adapter

When you want to use a function implemented in an existing class in another class, perform type conversion so that the function can be used in the type you want to use.

  1. Template Method

By using inheritance, you can standardize the general logic while expressing small differences in subclasses.

  1. Factory Method

By implementing the main program using the common Factory interface and Product interface, the behavior of the generated product can be changed simply by changing the specific type of factory (implemented in the interface).

  1. Singleton

You can limit the class instance to only one.

  1. Prototype

By registering the instance of each class in the organizer (Prototype role) in advance, it is possible to copy and create the necessary instance from the instance registered in the organizer.

--Eliminates the need to write new Something in the program itself when creating an instance. --Many classes can be managed together. --You can create an instance that is difficult to create by copying.

  1. Builder

It's similar to the Template Method, but it uses delegation rather than inheritance, and uses the methods of the class that represent the subtle differences to build the general logic. As a result, the logic can be shared

  1. Abstract Factory

Use when you want to create something (for example, html file). The interface of the method that creates the parts that make up the thing you want to make is fixed, and you call the method as many times as you like with any argument to create what you want to make. By leaving the implementation of the method that creates the parts that make up what you want to make to the implementation class, you can express the difference and make it resistant to changes. For example, in creating html. When you click the link part, you can express whether to transition in the same tab or open the link in a new tab by changing the implementation class.

  1. Bridge

You can separate the function class and the implementation class (small differences). That is, the logic is assembled in the function class using a common interface for the implementation class. By doing this, when adding a function, the function class should be added, and if the behavior (implementation) is to be changed, the implementation class should be changed.

  1. Strategy

Only the algorithm can be switched by delegation. This makes it possible to easily switch the output calculation method for input. For example, "fast but inaccurate algorithm" and "slow but accurate algorithm" can be used properly depending on the situation.

  1. Composite

Make it possible to identify the container (plural) and the contents (singular). For example, in the implementation of HashMap, the contents (singular) basically correspond to each hash value, but when a collision occurs, it corresponds by putting a container (plural) in the hash value.

  1. Decorator

If the class implements the same interface, you can add functions as many times as you like by delegation. The image is for the person who put up the shield

  1. Added flame neutralization barrier
  2. Added water neutralization barrier
  3. Added a barrier that recovers after a certain period of time

When thinking about, the image is that when you add it, you can add it as it is to the previous state, and eventually a person who has acquired all the functions will be born. This pattern is used in InputStreamReader, BufferReader, FileReader, etc. of java.io.

  1. Visitor

Separate data structure and processing. When a class that performs processing (what to do depends on the implementation) is given to the class that expresses the data structure, the method that performs the processing is called by delegation and the data is processed.

14. Chain of Responsibility

Turn the process around by delegation. The role of Handler (processing role) performs only the processing that can be done by oneself, and entrusts the processing to the next class if it cannot be kept in the hands of oneself. It is also possible to change the class that processes during program operation. As a disadvantage, processing may be slow.

  1. Facade

To reduce the number of interfaces and express one function, one method needs to be called. The process of calling other specific classes is aggregated in the Facade role.

  1. Mediator

Combine the state change logic into one class. By using delegation, the processing of the target class is called according to the state change.

  1. Observer

Effective for describing processing according to state changes. Register the role of each Observer (observer) in the role of Subject, and call the target method of Observer through delegation when the state in Subject changes.

  1. Memento

It is used when you want to save the state of the instance. The Originator role creates a Memento role when he wants to save his state, and returns to the state previously saved in Memento at the discretion of the Caretaker role. At this time, the role of Memento widens the interface for the role of Originator (such as making the package the same) and narrows the interface for the role of Caretaker so that the encapsulation is not destroyed. (Reduce the methods that can be called.)

  1. State

Manage state by class. By overloading, it is possible to describe processing based on the state without using conditional branching such as if statement.

  1. Flyweight

Share your instance and don't waste new. When an instance is needed, it is created from the Factory class, and in the Factory class, if the instance already exists, that instance is used as it is. This saves memory and new time (constructor processing time).

  1. Proxy

When the Subject role is a class that performs heavy processing, a Proxy role is prepared as a substitute for it. The Proxy role and the Subject role share an interface, and the Proxy role performs processing as much as possible so that the Subject role is not used until the last minute. Like an HTTP proxy, the image is that in response to a request from a client, information is fetched to the server only when the cache does not exist.

  1. Command

Manage processing instructions as one instance. Command history management becomes possible. Specifically, prepare a class that describes detailed instructions and a class that saves and manages those instructions in java.util.Stack. Both use the same interface.

  1. Interpreter

Use the Java language to create a simple language that suits your purpose. You can write regular expressions and batch processing languages.

Recommended Posts

Java design pattern summary
Java design pattern
[Design pattern] Java core library
Java beginner design pattern (Factory Method pattern)
Java knowledge summary
Design pattern ~ Builder ~
Java Generics Summary
Design pattern ~ Visitor ~
java callback pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern (1): AbstractFactory
[Java] Singleton pattern
Design pattern ~ Command ~
Design pattern ~ Iterator ~
Java Design Patterns
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Java 8 documentation summary
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Java 11 document summary
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
Java pattern memo
Java 12 new feature summary
C ++ design pattern (TemplateMethod pattern)
My DAO pattern (Java)
Design pattern ~ Abstract Factory ~
[Summary] Java environment preparation
effective java 3rd summary
Java 13 new feature summary
Java static [Personal summary]
Thread safe summary ~ Java ~
Java Primitive Specialization Summary
Java development link summary
Personal summary about Java
Java 10 new feature summary
Design pattern ~ Template Method ~
Builder pattern (Effective Java)
Java 14 new feature summary
Summary of Java support 2018
Java Lambda Command Pattern
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -
Java reserved word summary
Java8 Stream Rough Summary
Java8 Lambda Expression & Stream Design Pattern Rethinking --Null Object Pattern -
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
Design pattern ~ Chain of Responsibility ~
What is Java Assertion? Summary.
[Java11] Stream Summary -Advantages of Stream-
[Java] Summary of regular expressions
[Java] Summary of operators (operator)