设计模式 (四)创建者模式(Builder)

更新时间:2023-06-09 11:16:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

设计模式初步学习,一个前言+14种模式

设计模式 (四)创建者模式(Builder)

创建者模式主要是为了所谓的高聚合,解耦合,遇到多种配件于一身的情况的时候,放在实体类里面不合适,放在工厂类也不合适,所以就添加了一个创建者类,专门对相应的零件进行组装,如同汽车了N中配件,同时要是各种配件互相联系的话也在这个Builder里面一并做了。

明天准备去爬山,这个原理图就再补上吧:

今天把uml补上了,不过对于uml初学的我就乱乱的先随便画一个吧,等以后慢慢入门了在斟酌里面的错误和细节:

下面是我自己攒出来的例子(回来的时候着急,把书忘教室了,不知道书上怎么写的,不过明显记得一处错误,构造器也加上了一个void),为了在Builder里面优化一下,用了一下反射,这样就可以直接通过多态来实现对于派生类中的实体类进行初始化并且操作了,简单的实现电脑的组装:通过工厂类实例化Computer的父类指向子类的实例,然后在Builder里面添加一个工厂类的私有变量,通过构造器把要穿件的Computer实例传入Builder,然后对其进行解析,反射得到路径,对其派生类里面的方法进行反射得到相应的配件的集成,转配完成。少说多做:

[java] view plaincopy

1. package com.designpattern.builder;

2.

3. public interface Mouse {

4. public void add();

5. }

[java] view plaincopy

1. package com.designpattern.builder;

2.

设计模式初步学习,一个前言+14种模式

3. public interface Monitor {

4. public void add();

5. }

[java] view plaincopy 1. package com.designpattern.builder;

2.

3. public interface Keyboard {

4. public void add();

5. }

[java] view plaincopy 1. package com.designpattern.builder;

2.

3. public interface Displayer {

4. public void add();

5. }

[java] view plaincopy

1. package com.designpattern.builder;

2.

3. public interface Factory {

4. public Computer buildComputer();

5. }

[java] view plaincopy 1. package com.designpattern.builder;

2.

3. public interface Computer {

4. public void add();

5. }

[java] view plaincopy

1. package com.designpattern.builder;

设计模式初步学习,一个前言+14种模式

3. public class AcerMouse implements Mouse {

4.

5. @Override

6. public void add() {

7. System.out.println("add AcerMouse");

8. }

9.

10. }

[java] view plaincopy 1. package com.designpattern.builder;

2.

3. public class AcerMonitor implements Monitor {

4.

5. @Override

6. public void add() {

7. System.out.println("add AcerMonitor");

8. }

9.

10. }

[java] view plaincopy 1. package com.designpattern.builder;

2.

3. public class AcerKeyboard implements Keyboard {

4.

5. @Override

6. public void add() {

7. System.out.println("add AcerKeyboard");

8. }

9.

10. }

[java] view plaincopy

1. package com.designpattern.builder;

2.

3. public class AcerDisplayer implements Displayer {

设计模式初步学习,一个前言+14种模式

5. @Override

6. public void add() {

7. System.out.println("add AcerDisplayer");

8. }

9.

10. }

[java] view plaincopy 1. package com.designpattern.builder;

2.

3. public class AcerFactory implements Factory {

4.

5. @Override

6. public Computer buildComputer() {

7. return new Acer();

8. }

9. }

[java] view plaincopy 1. package com.designpattern.builder;

2.

3. public class Acer implements Computer {

4.

5. @Override

6. public void add() {

7. System.out.println("builder Acer");

8. }

9.

10. }

[java] view plaincopy

1. package com.designpattern.builder;

2.

3. import ng.reflect.Method;

4.

5. public class Builder {

6. private Factory factory;

设计模式初步学习,一个前言+14种模式

8. public Builder(Factory factory) {

9. this.factory = factory;

10. }

11.

12. public void builder() throws Exception {

13. factory.buildComputer().add();

14. String computer = factory.getClass().toString();

15. computer = computer.substring(6, computer.length() - 7); 16.

17. // build Mouse

18. Class c = Class.forName(computer + "Mouse");

19. Mouse mouse = (Mouse) c.newInstance();

20. Method method = c.getMethod("add", null);

21. method.invoke(mouse, null);

22.

23. // build Keyboard

24. c = Class.forName(computer + "Keyboard");

25. Keyboard keyboard = (Keyboard) c.newInstance();

26. method = c.getMethod("add", null);

27. method.invoke(keyboard, null);

28.

29. // build Displayer

30. c = Class.forName(computer + "Displayer");

31. Displayer displayer = (Displayer) c.newInstance();

32. method = c.getMethod("add", null);

33. method.invoke(displayer, null);

34.

35. // build Monitor

36. c = Class.forName(computer + "Monitor");

37. Monitor monitor = (Monitor) c.newInstance();

38. method = c.getMethod("add", null);

39. method.invoke(monitor, null);

40.

41. System.out.println("build complete");

42. }

43. }

[java] view plaincopy

1. package com.designpattern.builder;

2.

3. public class Client {

设计模式初步学习,一个前言+14种模式

4. public static void main(String[] args) {

5. try {

6. new Builder(new AcerFactory()).builder();

7. } catch (Exception e) {

8. // TODO Auto-generated catch block

9. e.printStackTrace();

10. }

11. }

12. }

本文转自csdn:/column/details/first-study-pattern.html

本文来源:https://www.bwwdw.com/article/tsv1.html

Top