Github source address

An overview of 23 design patterns

  • Java Language Design – An overview of 23 design patterns

Creation pattern

  • Factory Method Pattern
  • Abstract Factory Pattern
  • Builder Mode
  • Prototype mode
  • Singleton

Structural mode

  • Facade Pattern
  • Adapter mode (Adapter)
  • Proxy mode
  • Composite mode
  • Flyweight Mode
  • Decorator pattern
  • Bridge mode (Bridge)

Behavioral pattern

  • Mediator Mode
  • Observer Model
  • Command mode
  • Iterator pattern (Iterator)
  • Template Method
  • Strategy Pattern
  • State mode
  • Memento Mode
  • Interpreter mode
  • Chain of Responsibility model
  • Visitor Pattern

Model is introduced

define

Separating the construction of a complex object from its representation allows the same construction process to create different representations.

Usage scenarios

  1. The same method, different execution sequence, produce different event results;
  2. Multiple parts, or parts, can be assembled into an object but produce different results;
  3. The Builder pattern is appropriate when the product class is complex, or when the order of calls in the product class results in different performance;

A common way to use this is when a class constructor needs to pass in many arguments. Creating an instance of the class would make the code very unreadable and would introduce errors.

Before using Builder mode

public class Computer {
    private String cpu;
    private String screen;
    private String memory;
    private String mainBoard;

    public Computer(String cpu, String screen, String memory, String mainBoard) {
        this.cpu = cpu;
        this.screen = screen;
        this.memory = memory;
        this.mainBoard = mainBoard; }}Copy the code

After using Builder mode


public class NewComputer {
    private String cpu;
    private String screen;
    private String memory;
    private String mainBoard;

    public static final class Builder {
        private NewComputer mNewComputer;

        public Builder(a) {
            mNewComputer = new NewComputer();
        }

        public Builder cpu(String val) {
            mNewComputer.cpu = val;
            return this;
        }

        public Builder screen(String val) {
            mNewComputer.screen = val;
            return this;
        }

        public Builder memory(String val) {
            mNewComputer.memory = val;
            return this;
        }

        public Builder mainBoard(String val) {
            mNewComputer.mainBoard = val;
            return this;
        }

        public NewComputer create(a) {
            returnmNewComputer; }}}Copy the code
class Test {
    public static void main(String[] args) {

        // Non-Builder mode
        Computer computer = new Computer("cpu"."screen"."memory"."mainboard");
        
        / / Builder pattern
        NewComputer newComputer = new NewComputer.Builder()
                .cpu("cpu")
                .screen("screen")
                .memory("memory")
                .mainBoard("mainBoard") .create(); }}Copy the code

The above example code just passes in four parameters. If the parameters are dozens or more, the advantages of Builder mode become more obvious, with more flexibility in passing parameters and more readability of the code.

Code Address:

  • Github:github.com/leifu1107/a…