(四)创建者模式(Builder)

最后更新于:2022-04-01 15:48:12

创建者模式主要是为了所谓的高聚合,解耦合,遇到多种配件于一身的情况的时候,放在实体类里面不合适,放在工厂类也不合适,所以就添加了一个创建者类,专门对相应的零件进行组装,如同汽车了N中配件,同时要是各种配件互相联系的话也在这个Builder里面一并做了。 明天准备去爬山,这个原理图就再补上吧: 今天把uml补上了,不过对于uml初学的我就乱乱的先随便画一个吧,等以后慢慢入门了在斟酌里面的错误和细节: ![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-07-12_578455ccaf4da.png) 下面是我自己攒出来的例子(回来的时候着急,把书忘教室了,不知道书上怎么写的,不过明显记得一处错误,构造器也加上了一个void),为了在Builder里面优化一下,用了一下反射,这样就可以直接通过多态来实现对于派生类中的实体类进行初始化并且操作了,简单的实现电脑的组装:通过工厂类实例化Computer的父类指向子类的实例,然后在Builder里面添加一个工厂类的私有变量,通过构造器把要穿件的Computer实例传入Builder,然后对其进行解析,反射得到路径,对其派生类里面的方法进行反射得到相应的配件的集成,转配完成。少说多做: ~~~ package com.designpattern.builder; public interface Mouse { public void add(); } ~~~ ~~~ package com.designpattern.builder; public interface Monitor { public void add(); } ~~~ ~~~ package com.designpattern.builder; public interface Keyboard { public void add(); } ~~~ ~~~ package com.designpattern.builder; public interface Displayer { public void add(); } ~~~ ~~~ package com.designpattern.builder; public interface Factory { public Computer buildComputer(); } ~~~ ~~~ package com.designpattern.builder; public interface Computer { public void add(); } ~~~ ~~~ package com.designpattern.builder; public class AcerMouse implements Mouse { @Override public void add() { System.out.println("add AcerMouse"); } } ~~~ ~~~ package com.designpattern.builder; public class AcerMonitor implements Monitor { @Override public void add() { System.out.println("add AcerMonitor"); } } ~~~ ~~~ package com.designpattern.builder; public class AcerKeyboard implements Keyboard { @Override public void add() { System.out.println("add AcerKeyboard"); } } ~~~ ~~~ package com.designpattern.builder; public class AcerDisplayer implements Displayer { @Override public void add() { System.out.println("add AcerDisplayer"); } } ~~~ ~~~ package com.designpattern.builder; public class AcerFactory implements Factory { @Override public Computer buildComputer() { return new Acer(); } } ~~~ ~~~ package com.designpattern.builder; public class Acer implements Computer { @Override public void add() { System.out.println("builder Acer"); } } ~~~ ~~~ package com.designpattern.builder; import java.lang.reflect.Method; public class Builder { private Factory factory; public Builder(Factory factory) { this.factory = factory; } public void builder() throws Exception { factory.buildComputer().add(); String computer = factory.getClass().toString(); computer = computer.substring(6, computer.length() - 7); // build Mouse Class c = Class.forName(computer + "Mouse"); Mouse mouse = (Mouse) c.newInstance(); Method method = c.getMethod("add", null); method.invoke(mouse, null); // build Keyboard c = Class.forName(computer + "Keyboard"); Keyboard keyboard = (Keyboard) c.newInstance(); method = c.getMethod("add", null); method.invoke(keyboard, null); // build Displayer c = Class.forName(computer + "Displayer"); Displayer displayer = (Displayer) c.newInstance(); method = c.getMethod("add", null); method.invoke(displayer, null); // build Monitor c = Class.forName(computer + "Monitor"); Monitor monitor = (Monitor) c.newInstance(); method = c.getMethod("add", null); method.invoke(monitor, null); System.out.println("build complete"); } } ~~~ ~~~ package com.designpattern.builder; public class Client { public static void main(String[] args) { try { new Builder(new AcerFactory()).builder(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ~~~
';