650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/3F/EB/wKioL1PMzQKxSTJRAAKUiSwCEcE781.jpg "Title =" qq 40721081402.jpg "alt =" wkiol1pmzqkxstjraakuiswcece781.jpg "/>
Builder design mode: in my opinion, the core lies in two classes. Builder class and director class. Abstract everythingParts and assembly. The final product is presented to the customer through the cooperation of the two parts ., Only one product is assembled.
When the company grows bigger, it can not only provide one product. Based on the number of existing features and streamlining, you can quickly produce a variety of products, as long as different construct and product. Construct provides three features, indicating that this is a Lenovo thinkpadt430 laptop. If construct only provides a show function, it may be Lenovo's tablet.
public interface Builder { void call(); void show(); void work(); Product getResult();}
public class Lenovo implements Builder { private Product lenovo; public void call() { // TODO: implement lenovo.setCallFun("lenovo call"); } public void show() { // TODO: implement lenovo.setShowFun("lenovo show"); } public void work() { // TODO: implement lenovo.setWorkFun("lenovo work"); } public Product getResult() { // TODO: implement return this.lenovo; } public Lenovo() { // TODO: implement lenovo = new Product(); }}
public class Product { private String callFun; private String showFun; private String workFun; public String getCallFun() { return callFun; } public void setCallFun(String newCallFun) { callFun = newCallFun; } public String getShowFun() { return showFun; } public void setShowFun(String newShowFun) { showFun = newShowFun; } public String getWorkFun() { return workFun; } public void setWorkFun(String newWorkFun) { workFun = newWorkFun; }}
public class Director { private Builder builder; public Director(Builder builder) { // TODO: implement this.builder= builder; } public void construct() { // TODO: implement builder.call(); builder.show(); builder.work(); }}
public class CallBuilder {private static Builder builder;private static Director director;private static Product product;public static void main(String[] args) {builder = new Lenovo();director = new Director(builder);director.construct();product = builder.getResult();System.out.println(product.getCallFun());System.out.println(product.getShowFun());System.out.println(product.getWorkFun());}}
Builder Design Mode