当前位置:网站首页>Builder pattern

Builder pattern

2020-11-08 15:22:00 Irving the procedural ape

Builder pattern application scenarios :

It is mainly used to build some complex objects , Here's a complex object like : When building buildings , We need to lay the foundation first , Building framework , And then cover it from bottom to top . Usually , In the construction of such complex structures , It's hard to do it all at once . We need to build parts of this object first , And then put them together in stages . So now there's the builder pattern : The builder pattern is made up of Builder( builder ) and Director( commander ) form . Please see the following UML chart :                    

                                    

 

 

Director class 

package com.yuan.builder.demo1;

// command : The core , Responsible for directing the construction of a project , How the project builds , It's up to it
public class Director {

// Instruct the workers to give the order of execution
public Product build(Builder builder) {
builder.buildA();
builder.buildC();
builder.buildD();
builder.buildB();
return builder.getProduct();
}

}

 

Builder class

 

package com.yuan.builder.demo1;
// The abstract builder
public abstract class Builder {

abstract void buildA();// The foundation
abstract void buildB();// Steel Engineering
abstract void buildC();// Laying wires
abstract void buildD();// Paint

// to be finished : Got a specific product
abstract Product getProduct();
}

Worker class
package com.yuan.builder.demo1;

/**
* The concrete builder : Worker
*/
public class Worker extends Builder {
private Product product;

public Worker() {
this.product = new Product();
}

@Override
void buildA() {
product.setBuildA(" The foundation ");
System.out.println(" The foundation ");
}

@Override
void buildB() {
product.setBuildB(" Steel Engineering ");
System.out.println(" Steel Engineering ");
}

@Override
void buildC() {
pro.........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢