zoukankan      html  css  js  c++  java
  • 设计模式——构造者模式

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

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

    声明

    引用该文档请注明出处

  • 相关阅读:
    算法
    autoreleasepool和weak
    记录一下锁如何使用
    RunLoop相关知识
    NSTimer内存泄漏的解决方案
    ARC下block循环引用的解决方式
    MGJRouter源码解析及使用方法
    Instruments 中Time Profile的使用
    NSFileManager的基本使用
    Runtime相关知识
  • 原文地址:https://www.cnblogs.com/imaikce/p/10903134.html
Copyright © 2011-2022 走看看