{"action":"opened","issue":{"url":"https://api.github.com/repos/Huoxubeiyin/Design_mode/issues/9","repository_url":"https://api.github.com/repos/Huoxubeiyin/Design_mode","labels_url":"https://api.github.com/repos/Huoxubeiyin/Design_mode/issues/9/labels{/name}","comments_url":"https://api.github.com/repos/Huoxubeiyin/Design_mode/issues/9/comments","events_url":"https://api.github.com/repos/Huoxubeiyin/Design_mode/issues/9/events","html_url":"https://github.com/Huoxubeiyin/Design_mode/issues/9","id":325978650,"number":9,"title":"设计模式之Builder模式","user":{"login":"Huoxubeiyin","id":17829981,"avatar_url":"https://avatars3.githubusercontent.com/u/17829981?v=4","gravatar_id":"","url":"https://api.github.com/users/Huoxubeiyin","html_url":"https://github.com/Huoxubeiyin","followers_url":"https://api.github.com/users/Huoxubeiyin/followers","following_url":"https://api.github.com/users/Huoxubeiyin/following{/other_user}","gists_url":"https://api.github.com/users/Huoxubeiyin/gists{/gist_id}","starred_url":"https://api.github.com/users/Huoxubeiyin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Huoxubeiyin/subscriptions","organizations_url":"https://api.github.com/users/Huoxubeiyin/orgs","repos_url":"https://api.github.com/users/Huoxubeiyin/repos","events_url":"https://api.github.com/users/Huoxubeiyin/events{/privacy}","received_events_url":"https://api.github.com/users/Huoxubeiyin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-05-24T06:18:31Z","updated_at":"2018-05-24T06:18:31Z","closed_at":null,"author_association":"OWNER","body":"### Builder模式\r\n\r\n1. 对外部隐藏内部实现细节\r\n2. 分离部件和组装过程\r\n3. 自由扩展部件和构造过程\r\n4. 降低解耦，分工明确\r\n\r\n### 适应场景\r\n\r\n1. 相同的方法，不同的执行顺序，产生不同的事件结果\r\n2. 多个部件和零件，都可以装配到一个对象中，但产生的运行结果又不相同时\r\n3. 产品类非常复杂，或者产品类中的调用顺序不同产生不同的作用\r\n4. 当初始化一个对象特别复杂，如参数多，且很多参数具有默认值时\r\n\r\n### UML类图\r\n\r\n![](https://user-gold-cdn.xitu.io/2017/10/17/019970181f7739849ec93011c75e675d)\r\n##### 说明：\r\n1. 抽象类Builder由ConcreteBuilder子类来实现\r\n2. Director类含有一个Builder的实例对象，通过construct方法指导Builder控制Product的流程\r\n3. ConcreteBuilder实现具体的组装工作（new Product），组装流程由Director控制\r\n\r\n### 代码实例实现\r\n\r\n计算机的组装过程较为复杂，且组装过程也不是固定的，我们暂且将计算机的组装过程简化为构建主机，设置显示器，设置操作系统三个步骤，通过Director和具体的Builder来构建计算机对象\r\n\r\n* 计算机抽象类，即Product角色\r\n\r\n        public abstract class Computer {\r\n    \r\n        protected String mBoard;\r\n        protected String mDisplay;\r\n        protected String mOS;\r\n    \r\n            public Computer() {\r\n            }\r\n        \r\n            //设置cpu\r\n            public void setBoard(String board){\r\n                mBoard = board;\r\n            }\r\n        \r\n            //设置显示\r\n            public void setDisplay(String display){\r\n                mDisplay = display;\r\n            }\r\n        \r\n            //设置操作系统\r\n            public abstract void setOS();\r\n        \r\n            @Override\r\n            public String toString() {\r\n                return \"Computer { \" +\r\n                        \"mBoard='\" + mBoard + '\\'' +\r\n                        \", mDisplay='\" + mDisplay + '\\'' +\r\n                        \", mOS='\" + mOS + '\\'' +\r\n                        '}';\r\n            }\r\n        }\r\n    \r\n* 具体的Computer类,即macboook\r\n    \r\n        public class Macbook extends Computer{\r\n    \r\n            public Macbook() {\r\n            }\r\n        \r\n            @Override\r\n            public void setOS() {\r\n                mOS = \"Mac OS X 10.10\";\r\n            }\r\n    \r\n        }\r\n\r\n* 抽象Builder类,只提供抽象方法，也不提供组装方法，有Director实现组装\r\n\r\n        public abstract class Builder {\r\n            //设置主机\r\n            public abstract void buildBoard(String board);\r\n        \r\n            //设置显示器\r\n            public abstract void buildDisplay(String display);\r\n        \r\n            //设置操作系统\r\n            public abstract void buildOS();\r\n        \r\n            //创建Computer\r\n            public abstract Computer create();\r\n        }\r\n\r\n* MAcbookBuilder，具体的builder类\r\n        \r\n        public class MacbookBuilder extends Builder {\r\n        \r\n          //实例化，父类容器容纳子类对象\r\n            private Computer mComputer = new Macbook();\r\n        \r\n            @Override\r\n            public void buildBoard(String board) {\r\n                mComputer.setBoard(board);\r\n            }\r\n        \r\n            @Override\r\n            public void buildDisplay(String display) {\r\n                mComputer.setDisplay(display);\r\n            }\r\n        \r\n            @Override\r\n            public void buildOS() {\r\n                mComputer.setOS();\r\n            }\r\n        \r\n            @Override\r\n            public Computer create() {\r\n                return mComputer;\r\n            }\r\n        }\r\n\r\n* Director类，负责构造Computer，通过传入builder对象，负责具体的组装工作\r\n \r\n        public class Director {\r\n            Builder mBuilder = null;\r\n        \r\n            public Director(Builder builder) {\r\n                this.mBuilder = builder;\r\n            }\r\n        \r\n            public void construct(String board, String display){\r\n                mBuilder.buildBoard(board);\r\n                mBuilder.buildDisplay(display);\r\n                mBuilder.buildOS();\r\n            }\r\n        }\r\n\r\n* 测试Main\r\n\r\n        public class Main {\r\n        \r\n            public static void main(String[] args) {\r\n            \r\n            //Director，通过传入builder对象，可实现builder的自动组装任务\r\n            Director pcDirector = new Director(macbookBuilder);\r\n    \r\n            //封装构建过程，传入具体的零件信息：4核CPU，内存2GB，MAC操作系统\r\n            pcDirector.construct(\"英特尔主板 酷睿I7CPU\", \"Retina 显示器\");\r\n        \r\n                //构建计算机，输出相关信息\r\n                System.out.println(\"Computer Info : \" + macbookBuilder.create().toString());\r\n        \r\n            }\r\n        }\r\n        \r\n    \r\n* 运行结果\r\n\r\n        Computer Info : Computer { mBoard='英特尔主板 酷睿I7CPU', mDisplay='Retina 显示器', mOS='Mac OS X 10.10'}\r\n\r\n    \r\n#### 总结\r\n\r\n1. 抽象出数据原型Computer，其内包含基本的数据组成\r\n2. 具体的数据原型MacBook，通过继承Computer而来\r\n3. Builder类实现具体的装配工作，其内部必须要有数据原型对象（原料）用来装配，但是具体装配的前后顺序不是自己能控制的，只是拥有装配的能力\r\n4. Director指挥Builder实现具体的装配工作，其内部封装了构建复杂的产品对象过程，对外部隐藏构建的细节，控制装配的流程\r\n5. Builde的派生类Macbookbuilder，用于构建具体的装配对象Macbook（Computer的派生类类）\r\n6. Director通过指挥Builder，一起将一个复杂的对象构建和表示过程分离，使同样的构建过程可以创建不同的对象\r\n \r\n#### 优点\r\n\r\n*  良好的封装性，使用建造者模式可以使客户端不必要知道产品内部组成的细节\r\n*  建造者独立，容易扩展\r\n\r\n#### 缺点\r\n\r\n* 会产生多余的Builder对象以及Director对象，消耗内存\r\n\r\n#### 深度拓展\r\n[Android源码分析之Builder模式](https://juejin.im/post/59ccbf186fb9a00a562ea439)\r\n"}}
{"actor":{"display_login":"Huoxubeiyin"}}