Concept
Separates a complex build from its representation so that different representations can be created during the same build process.
Class Diagram
Code
public abstract class Computer { private String type; private String cpu; private String ram; private String os; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public String getRam() { return ram; } public void setRam(String ram) { this.ram = ram; } public String getOs() { return os; } public void setOs(String os) { this.os = os; } }
Create two types of computers:
public class T410 extends Computer { private String hardDisk; public T410() { this.setType("ThinkPad T410i"); } public String getHardDisk() { return hardDisk; } public void setHardDisk(String hardDisk) { this.hardDisk = hardDisk; } @Override public String toString() { return "T410 [hardDisk=" + hardDisk + ", getType()=" + getType() + ", getCpu()=" + getCpu() + ", getRam()=" + getRam() + ", getOs()=" + getOs() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + "]"; }}
public class X201 extends Computer { public X201() { this.setType("Thinkpad X201i"); } @Override public String toString() { return "X201 [getType()=" + getType() + ", getCpu()=" + getCpu() + ", getRam()=" + getRam() + ", getOs()=" + getOs() + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + "]"; }}
Add a computerbuilder interface and two implementation classes based on the computer product class to facilitate computer production:
public interface ComputerBuilder { void buildCpu(); void buildRam(); void buildHardDisk(); void BuildOs(); Computer getResult();}
public class T410Builder implements ComputerBuilder { private T410 computer = new T410(); @Override public void buildCpu() { computer.setCpu("i5"); } @Override public void buildRam() { computer.setRam("4G"); } @Override public void buildHardDisk() { computer.setHardDisk("500G"); } @Override public void BuildOs() { computer.setOs("Win7"); } @Override public Computer getResult() { return computer; }}
Public class x201builder implements computerbuilder {private x201 computer = new x201 (); @ override public void buildcpu () {computer. setcpu ("i7") ;}@ override public void buildram () {computer. setram ("8g") ;}@ override public void buildharddisk () {// No haeddisk} @ override public void buildos () {computer. setos ("Win8") ;}@ override public computer getresult () {return computer ;}}
Another director:
public class ComputerDirector { ComputerBuilder builder; public T410 constructT410() { builder = new T410Builder(); builder.buildCpu(); builder.buildHardDisk(); builder.BuildOs(); builder.buildRam(); return (T410) builder.getResult(); } public X201 constructX201() { builder = new X201Builder(); builder.buildCpu(); builder.buildHardDisk(); builder.BuildOs(); builder.buildRam(); return (X201) builder.getResult(); }}
Test:
public class ComputerTest { public static void main(String[] args) { ComputerDirector director = new ComputerDirector(); Computer t410 = director.constructT410(); System.out.println(t410); System.out.println("-------------------------"); Computer x201 = director.constructX201(); System.out.println(x201); }}
Differences from abstract factories
In the builder mode, there is a mentor who manages the builder. the user is in contact with the mentor, and the mentor contacts the builder to get the product. That is, the construction mode can enforce a step-by-step construction process.
The construction mode encapsulates complex internal creation in the Internal. For external callers, they only need to pass in the builder and construction tool. The caller does not need to care about how the internal creation is finished.
I am the dividing line of tiantiao
Source code: http://pan.baidu.com/s/1dD1Qx01
Javabuilder. Zip