好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java实现建造者模式(Builder Pattern)

一、什么是建筑者模式?

   建造者模式 (builder pattern)使用多个简单的对象一步一步构建一个复杂的对象。

  一个 builder 类会一步一步构造最终的对象,该 builder 类独立于其他对象。

  建造者模式主要解决在软件系统中,有时候面临着"一个复杂对象"的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定。

二、建造者模式的具体实现

结构图

建造者模式中的四个角色:

1、builder:给出一个抽象接口,以规范产品对象的各个组成成分的建造。这个接口规定要实现复杂对象的哪些部分的创建,并不涉及具体的对象部件的创建。
2、concretebuilder:实现builder接口,针对不同的商业逻辑,具体化复杂对象的各部分的创建。 在建造过程完成后,提供产品的实例。
3、director:调用具体建造者来创建复杂对象的各个部分,在指导者中不涉及具体产品的信息,只负责保证对象各部分完整创建或按某种顺序创建。
4、product:要创建的复杂对象。

java 代码实现

1、创建人类实体类

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

package com.designpattern.builderpattern;

 

/**

  * 对象 人

  *

  * @author zhongtao on 2018/9/17

  */

public class human {

 

   private string head;

   private string body;

   private string hand;

   private string foot;

 

   public string gethead() {

     return head;

   }

 

   public void sethead(string head) {

     this .head = head;

   }

 

   public string getbody() {

     return body;

   }

 

   public void setbody(string body) {

     this .body = body;

   }

 

   public string gethand() {

     return hand;

   }

 

   public void sethand(string hand) {

     this .hand = hand;

   }

 

   public string getfoot() {

     return foot;

   }

 

   public void setfoot(string foot) {

     this .foot = foot;

   }

}

2、创建造人的 builder 接口

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

package com.designpattern.builderpattern;

 

/**

  * 造人接口 规定造人的规范 需要头、身体、手、脚

  *

  * @author zhongtao on 2018/9/17

  */

public interface builderhuman {

 

   void buildhead();

 

   void buildbody();

 

   void buildhand();

 

   void buildfoot();

 

   /**

    * 返回创建的对象

    */

   human createhuman();

 

}

3、concretebuilder 创建不同类型的人
tallperson

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

package com.designpattern.builderpattern;

 

/**

  * 高个子的人

  *

  * @author zhongtao on 2018/9/17

  */

public class tallpersonbuilder implements builderhuman {

 

   human human;

 

   public tallpersonbuilder() {

     human = new human();

   }

 

   @override

   public void buildhead() {

     human.sethead( "普通的头脑" );

   }

 

   @override

   public void buildbody() {

     human.setbody( "壮实,高大的身体" );

   }

 

   @override

   public void buildhand() {

     human.sethand( "长手" );

   }

 

   @override

   public void buildfoot() {

     human.setfoot( "长脚" );

   }

 

   @override

   public human createhuman() {

     return human;

   }

}

smarthuman

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

package com.designpattern.builderpattern;

 

/**

  * 聪明的人

  *

  * @author zhongtao on 2018/9/17

  */

public class smarthumanbuilder implements builderhuman {

 

   human human;

 

   public smarthumanbuilder() {

     human = new human();

   }

 

   @override

   public void buildhead() {

     human.sethead( "高智商的头脑" );

   }

 

   @override

   public void buildbody() {

     human.setbody( "健康的身体" );

   }

 

   @override

   public void buildhand() {

     human.sethand( "普通的手" );

   }

 

   @override

   public void buildfoot() {

     human.setfoot( "普通的脚" );

   }

 

   @override

   public human createhuman() {

     return human;

   }

}

4、director 建造者模式的核心 调用具体建造者来创建不同的人

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

package com.designpattern.builderpattern;

 

/**

  * 管理造人的顺序 builderhuman不同,则创建的人不同

  * @author zhongtao on 2018/9/17

  */

public class humandirector {

 

   public human createhumanbydirector(builderhuman builderhuman){

     builderhuman.buildhead();

     builderhuman.buildbody();

     builderhuman.buildhand();

     builderhuman.buildfoot();

 

     return builderhuman.createhuman();

   }

}

5、建造者模式测试

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

package com.designpattern.builderpattern;

 

import org.junit.test;

 

/**

  * 测试

  *

  * @author zhongtao on 2018/9/17

  */

public class builderpatterntest {

 

   /**

    * 测试建造者模式

    */

   @test

   public void test() {

     humandirector humandirector = new humandirector();

     //创建高个子的人

     human humanbydirector = humandirector.createhumanbydirector( new tallpersonbuilder());

     system.out.println(humanbydirector.gethead());

     system.out.println(humanbydirector.getbody());

     system.out.println(humanbydirector.gethand());

     system.out.println(humanbydirector.getfoot());

 

     system.out.println( "------简单的分割线------" );

     //创建聪明的人

     human smarthuman = humandirector.createhumanbydirector( new smarthumanbuilder());

     system.out.println(smarthuman.gethead());

     system.out.println(smarthuman.getbody());

     system.out.println(smarthuman.gethand());

     system.out.println(smarthuman.getfoot());

   }

}

三、建造者模式的优缺点

优点:

建造者独立,易扩展。
便于控制细节风险。

缺点:

产品必须有共同点,范围有限制。
如内部变化复杂,会有很多的建造类。

注意事项:

与工厂模式的区别,建造者模式更加关注与零件装配的顺序。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://www.cnblogs.com/zt19994/p/9680406.html

查看更多关于java实现建造者模式(Builder Pattern)的详细内容...

  阅读:13次