Builder (Builder) Design Pattern

Builder (Builder) Design Pattern

The reason 1.0.0 generated model builder

    In particularly complex when class data, create a class that takes a long time, because some classes require different initialization according to different occasions. Under the class structure is complicated, difficult and initialization, the step of creating encapsulated classes may reduce the workload, to improve stability. This is the reason why the builder mode.

2.0.0 What is the builder pattern?

Construction and separation of objects representing the complex object, such that the same process can build create different representations.

3.0.0 Construction mode of usage scenarios

  • Create complex objects (many parameters, and more difficult to initialize) ---> Create package

4.0.0 Object Model Builder

  • Product Object
  • Abstract builder
  • Specific builder
  • Director

General implementation 5.0.0 builder mode

@Slf4j
public class BuilderPattern {
    class CarDirector {
        private IBuilder builder;
        public CarDirector(IBuilder builder) {this.builder = builder;}
        public Car construct() {
            builder.buildWheel();
            builder.buildBody();
            builder.buildEngine();
            return builder.build();
        }
    }

    interface IBuilder {
        void buildWheel();
        void buildBody();
        void buildEngine();
        Car build();
    }

    class CarBuilder implements IBuilder {
        private Car car = new Car();
        @Override
        public void buildWheel() {car.setWheel("wheel");}
        @Override
        public void buildBody() {car.setWheel("body");}
        @Override
        public void buildEngine() {car.setWheel("engine");}
        @Override
        public Car build() {return car;}
    }

    @Setter
    class Car {
        private String wheel;
        private String body;
        private String engine;
    }
}

Contact the builder pattern and the difference between 6.0.0 and factory mode

  • Factory Mode -> Create to create a distribution facility as well as product lines / products
  • Builder Mode -> product assembly process, taking first screw, installed after the wire ....

  • Factory pattern: the chairman to do a number of products, according to different allocation of sub-products plant products.
  • The builder pattern: product assembly line, line length according to different needs, different arrangements of people assembling a product.
  • Different heights! ! ! The difference between the chairman and long lines

Guess you like

Origin www.cnblogs.com/fengzhida/p/11366739.html