设计模式——构造模式

构造器模式_组装复杂实例(逐步构造出一个复杂的实例)

/**
 * 指挥者
 * @author maikec
 * @date 2019/5/11
 */
public class Director {
    private final AbstractBuilder builder;
    public Director(AbstractBuilder builder){
        this.builder = builder;
    }
    public void builderMessage(){
        builder.createHead();
        builder.createBody();
        builder.signature();
    }
}

/**
 * 抽象构造器
 * @author maikec
 * @date 2019/5/11
 */
public abstract class AbstractBuilder {
    protected Message message;
    protected Head head;
    protected Body body;
    protected Author author;
    /**
     * 创建消息头部
     * @return
     */
    protected abstract Head createHead();

    /**
     * 创建消息体
     * @return
     */
    protected abstract Body createBody();

    /**
     * 署名
     * @return
     */
    protected abstract Author signature();

    public Message getMessage(){
        return message;
    }
}

/**
 * @author maikec
 * @date 2019/5/11
 */
public class Message {
    private Head head;
    private Body body;
    private Author author;
    public Message(){}
    public Message(Head head,Body body,Author author){
        this.head = head;
        this.body = body;
        this.author = author;
    }

    @Override
    public String toString() {
         super.toString();
         if (head !=null || body != null || author != null){
             StringBuffer sb = new StringBuffer( "[" );
             if (head != null){
                 sb.append( head.toString() );
             }
             if (body != null){
                 sb.append( body.toString() );
             }
             if (author != null){
                 sb.append( author.toString() );
             }
             sb.append( "]" );
             return sb.toString();
         }
         return null;
    }
}

/**
 * @author maikec
 * @date 2019/5/11
 */
public class Head {
    private String name;
    public Head(){}
    public Head(String name){
        this.name = name;
    }
    @Override
    public String toString() {
        super.toString();
        return name == null ? "Head" : name;
    }
}

/**
 * @author maikec
 * @date 2019/5/11
 */
public class Body {
    private String name;
    public Body(){}
    public Body(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
         super.toString();
         return name == null ? "Body" : name;
    }
}

/**
 * @author maikec
 * @date 2019/5/11
 */
public class Author {
    private String name;
    public Author(){}
    public Author(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
         super.toString();
         return name == null ? "Author" : name;
    }
}

/**
 * Email构造器
 * @author maikec
 * @date 2019/5/11
 */
public class EmailBuilder extends AbstractBuilder {
    @Override
    protected Head createHead() {
         head = new Head("Email Head");
         return head;
    }

    @Override
    protected Body createBody() {
        body = new Body("Email Body");
        return body;
    }

    @Override
    protected Author signature() {
        author =  new Author("maikec");
        return author;
    }

    @Override
    public Message getMessage() {
        return new Message( head,body,author );
    }
}
复制代码

附录

github.com/maikec/patt… 个人GitHub设计模式案例

声明

引用该文档请注明出处

转载于:https://juejin.im/post/5ce40d15e51d4556dc29357c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值