JAVA设计模式之【建造者模式】

建造者模式    建造者模式为客户端返回的不是一个简单的产品,而是一个由多个部件组成的复杂产品    角色        Builder抽象建造者            buildPartX            getResult        ConcreteBuilder具体建造者        Product产品角色        Director指挥者    复杂对象        指包含多个成员属性的对象,它们称为部件或零件        比如汽车,包含方向盘、发动机、轮胎等

1.角色类

package Builder;

/**
 * Created by e550 on 2016/10/1.
 */
public class Actor {
    private String type; // 角色
    private String sex;  // 性别
    private String face; // 脸型
    private String costume; // 服装
    private String hairstyle; // 发型
    public void setType(String type) {
        this.type = type;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setFace(String face) {
        this.face = face;
    }

    public void setCostume(String costume) {
        this.costume = costume;
    }

    public void setHairstyle(String hairstyle) {
        this.hairstyle = hairstyle;
    }
    public String getType() {
        return this.type;
    }
    public String getSex() {
        return this.sex;
    }
    public String getFace() {
        return this.face;
    }

    public String getCostume() {
        return this.costume;
    }

    public String getHairstyle() {
        return this.hairstyle;
    }

}

2.角色构造抽象类

package Builder;

/**
 * Created by jim on 2016/10/3.
 */
abstract class ActorBuilder {
    protected Actor actor = new Actor();
    public abstract void buildType();
    public abstract void buildSex();
    public abstract void buildFace();
    public abstract void buildCostume();
    public abstract void buildHairstyle();
    public Actor createActor() {
        return actor;
    }
}

3.角色构造具体类,英雄角色

package Builder;

/**
 * Created by e550 on 2016/10/3.
 */
public class HeroBuilder extends ActorBuilder{
    public void buildType() {
        actor.setType("英雄");
    }

    public void buildSex() {
        actor.setSex("男");
    }

    public void buildFace() {
        actor.setFace("英俊");
    }

    public void buildCostume() {
        actor.setCostume("盔甲");
    }

    public void buildHairstyle() {
        actor.setHairstyle("飘逸");
    }
}

天使角色

package Builder;

/**
 * Created by e550 on 2016/10/3.
 */
public class AngelBuilder extends ActorBuilder{
    public void buildType() {
        actor.setType("天使");
    }

    public void buildSex() {
        actor.setSex("女");
    }

    public void buildFace() {
        actor.setFace("漂亮");
    }

    public void buildCostume() {
        actor.setCostume("白裙");
    }

    public void buildHairstyle() {
        actor.setHairstyle("披肩长发");
    }
}

恶魔角色

package Builder;

/**
 * Created by e550 on 2016/10/3.
 */
public class DevilBuilder extends ActorBuilder{
    public void buildType() {
        actor.setType("恶魔");
    }

    public void buildSex() {
        actor.setSex("妖");
    }

    public void buildFace() {
        actor.setFace("丑陋");
    }

    public void buildCostume() {
        actor.setCostume("黑衣");
    }

    public void buildHairstyle() {
        actor.setHairstyle("光头");
    }
}

4.角色控制器

package Builder;

/**
 * Created by e550 on 2016/10/3.
 */
public class ActorController {
    public Actor construct(ActorBuilder ab) {
        Actor actor;
        ab.buildType();
        ab.buildSex();
        ab.buildFace();
        ab.buildCostume();
        ab.buildHairstyle();
        actor = ab.createActor();
        return actor;
    }
}

5.客户端执行

package Builder;

/**
 * Created by e550 on 2016/10/3.
 */
public class Client {
    public static void main(String args[]) {
        ActorBuilder ab;
        ab = new AngelBuilder();

        ActorController ac = new ActorController();

        Actor actor;
        actor = ac.construct(ab);

        System.out.println("类型:" + actor.getType());
        System.out.println("性别:" + actor.getSex());
        System.out.println("面容:" + actor.getFace());
        System.out.println("服装:" + actor.getCostume());
        System.out.println("发型:" + actor.getHairstyle());
    }
}

执行结果
类型:天使
性别:女
面容:漂亮
服装:白裙
发型:披肩长发


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/5929161.html,如需转载请自行联系原作者

上一篇:C++设计模式12--建造者模式----创建复杂对象的工程师


下一篇:[设计模式]建造者模式