GoF java design pattern rough summary

This article is a personal summary of "Introduction to Design Patterns Learned in the Java Language"

Iterater

Purpose

Access elements in sequence, such as an array.

Contents

hasNext () returns a boolean if it has the following elements: Then check for elements and run next () next () accesses the next element

Adopter

Purpose

Eliminate gaps between APIs

merit

Reusability is improved by creating a part that fits between the two APIs and eliminates the gap. Implement the desired interface (API) without making any adjustments to existing classes

Template Method

Purpose

Determine the processing framework in the superclass and describe the specific processing in the subclass.

merit

Logic can be shared.

Caution

The parent class is defined as an abstract class. If it is interface, the parent class cannot write the process.

Factory Method

Purpose

Define instance creation in the parent class (do not define a specific class name). In the child class, flesh is done.

Singleton

Purpose

Only one instantiate By making the constructor private, you can prevent instantiation outside the class. The instance access method is to return only one instance with getInstance ().

Caution

private static Singleton singleton = null;
if(singleton == null){
    singleton = new Singleton;
}

With the conditional expression, multiple instances may be created depending on the state of the machine when executed by multiple threads. Make it asynchronous or create it in advance.

Prototype

Purpose

Copy and create an instance.

Caution

The clone method is shallow copy. Override and implement deep copy, which copies the contents of the instance. shallow copy just copies the contents of the instance field as it is. The clone method does not initialize, so it needs to be initialized separately.

Builder

Purpose

Create the entire structure in the parent class and build up complicated processing step by step By increasing independence, it is easier to understand the corrections and additions.

Abstract Factory

Purpose

Use abstract factories and abstract parts.

Caution

It's easy to add a concrete factory. Difficult to add new parts (for the entire concrete factory)

Bridge

Purpose

Separate functions and implementations and bridge them. Function: Processing, etc. Implementation ・ ・ ・ Class when used for implementation

When the number of functions is increased, all the functions are added to the implementation class, which makes it easier to increase the functions.

Caution

Since the implementation is often complicated, use it only when the implementation part is changed frequently. For example, how to deal with OS dependence You can change the implementation without modifying the functionality.

Facade

Purpose

Simplify the interface. Hide complicated processing. It serves as a window for complicated processing.

Caution

To make only the package accessible

//public
public class Sample{

}

//Make it accessible only within the package.
class Sample{

}

Strategy

Purpose

The algorithm can be changed. Delegation makes the connection weaker than inheritance, and the algorithm can be easily changed.

Composite

Purpose

Make it a wooden structure. Make the container and contents the same and make it a recursive structure. composite-> "mixture" "complex"

FileDirectory File directories include files and directories. When looking at files in order, equate them with subdirectories or files. image.png

Directory can access Entry recursively. If File is a tree structure, leaves Directory is a branch.

Example

Decorator

Purpose

From the core of the function, cover it from one skin to the other and add it. Functions can be added without adding contents. By using delegation, loose coupling is possible, so dynamic function additions are possible. While maintaining the transparent interface (API), we will add functions by covering with objects.

Demerit

The number of small classes will increase.

(Extra edition) Inheritance and delegation

Inheritance-subclasses can be equated with superclasses-

class Parent {
    ...
    void parentMethod(){
        ...
    }
}
class Child extends Parent {
    ...
    void childMethod(){
        ...
    }
}

Parent obj = new Child(); obj.parentMethod(); An instance of Child can be assigned to a variable of Parent type as it is. You can call methods inherited from Parent. In other words Treats an instance of Child as if it were an instance of Parent. </ font> </ strong> This is an example of considering a subclass as a superclass


vice versa A cast is required to consider a superclass as a subclass

Parent obj = new Child(); ((Child)obj).childMethod();

Delegation-Identify yourself and the delegate

If the interface is transparent using delegation, you can equate yourself with the delegator.

class Rose {
    Violet obj = ...
    void method(){
        obj.method();
    }
}

class Violet {
    void method(){
        ...
    }
}

Rose and Violet have the same method, and Rose delegates to Violet ... but it's not specified that they are common methods.

abstract class Flower {
    abstract void method();
}

interface Flower {
    abstract void method();
}

Can be specified as above Use properly depending on the case.

Visitor

Purpose

Separate data structure and processing. Create a visitor class that visits the data structure and have that class handle it.

image.png

Performs specific processing on the element that holds the data structure. The Visitor pattern enhances the independence of the File class and Directory class as parts. If you program the contents of the process as a method of the File class or Directory class, you will have to modify the File class or Directory class every time you want to add a new "process" and extend the function.

Other

LSP (Liskov Substitution Principle)

If the parent class is true for the relational expression, then the derived small class of the parent class is also true. (Substitution possibility)

The Open-Closed Principle Do not prohibit future expansion unless you have a specific reason to do so. But you can't have to modify an existing class every time you extend it.

In other words, allow existing classes to be extended without modification </ strong> That is.

Highly reusable classes are closed. Strings are closed because they are highly reusable and inefficient when expanded. (Cannot be extended because it is defined in the final class)

Recommended Posts