首页 > 其他 > 详细

建造者模式5(7)

时间:2019-08-27 14:06:18      阅读:71      评论:0      收藏:0      [点我收藏+]

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

技术分享图片

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();
    }
}

 

建造者模式5(7)

原文:https://www.cnblogs.com/zzlcome/p/11417961.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!