建造者模式5(7)

简单理解:指挥者、建造者、建造对象三者之间的关系

package Creating.pratice;

public class BuilderPMain {

    public static void main(String[] args) {
        
        BuilderP b = new BuilderExecutor();
        Director d = new Director(b);//指挥者拥有多个建造者
        ProductRoleP p = d.construct();//建造者的行为
        System.out.println(p.toString());
        
    }

}

class ProductRoleP{
    private String pName;
    private String pType;
    public String getpName() {
        return pName;
    }
    public void setpName(String pName) {
        this.pName = pName;
    }
    public String getpType() {
        return pType;
    }
    public void setpType(String pType) {
        this.pType = pType;
    }
    @Override
    public String toString() {
        return "ProductRoleP [pName=" + pName + ", pType=" + pType + "]";
    }
    
}

abstract class BuilderP{
    protected ProductRoleP p = new ProductRoleP();
    public abstract void builderPname();
    public abstract void builderPtype();
    public ProductRoleP getInstance() {
        return p;
    }
}

class BuilderExecutor extends BuilderP{

    @Override
    public void builderPname() {
        System.out.println("name创建了");
        p.setpName("zhangsan");
        
    }

    @Override
    public void builderPtype() {
        System.out.println("type创建了");
        p.setpType("nan");
    }
    
}

class Director{
    
    private BuilderP b;
    public Director(BuilderP b){
        this.b = b;
    }
    
    public ProductRoleP construct(){
        b.builderPname();
        b.builderPtype();
        return b.getInstance();
    }
}

 

posted @ 2019-08-27 14:01  飞鸟游鱼  阅读(135)  评论(0编辑  收藏  举报