欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

设计模式——构造者模式

程序员文章站 2022-08-17 15:37:19
构造器模式_组装复杂实例(逐步构造出一个复杂的实例 附录 github.com/maikec/patt… 个人GitHub设计模式案例 声明 引用该文档请注明出处 ......

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

设计模式——构造者模式

 

设计模式——构造者模式

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

声明

引用该文档请注明出处