设计模式——构造者模式

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

/**
 * 指挥者
 * @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设计模式案例

声明

引用该文档请注明出处

猜你喜欢

转载自www.cnblogs.com/imaikce/p/10903134.html