题目:某公司要设计一个房屋选购系统,房屋有两种类型:普通型和豪华型。不同房屋体现在面积大小以及卧室、卫生间、车库的数量上。利用生成器模式编制功能类和测试类。
分析:所需要的类有House类,生成器IBuilder接口类,普通房子生成类BuilderImplP,豪华房子生成类BuilderImplS,指挥生产Director类,最后一个测试类Test。
代码如下:
1.House.java
1 package com.Mark.chapter04.生成器.ToUnderstand.chapter04.exercise;
2
3 public class House
4 {
//为了更好理解,就简单定义这些属性
5 public int area;
6 public int sleeping;
7 public int toliet;
8 public int carNum;
9 }
2.IBuilder接口
1 package com.Mark.chapter04.生成器.ToUnderstand.chapter04.exercise;
2
3 public interface IBuilder
4 {
5 //因为是接口,所以方法的实现要交给他不同的实现类,实现不同的方法.
6 public void createArea();
7 public void createSleeping();
8 public void createCarNum();
9 public void createToliet();
10 public House createHouse();
11 }
3.建造普通房子BuilderImplP类
1 package com.Mark.chapter04.生成器.ToUnderstand.chapter04.exercise;
2
3 public class BuilderImplP implements IBuilder
4 {
5 private House house;
6 public BuilderImplP(House house)
7 {
8 this.house=house;
9 }
10 @Override
11 public void createArea()
12 {
13 house.area=1; //这些属性值并未用到,这里只是起到方便理解作用,就假定认为已经用到了这些属性。
14 System.out.println("平民房就一平米哦!");
15 }
16
17 @Override
18 public void createSleeping()
19 {
20 house.sleeping=1;
21 System.out.println("平民房就一个卧室哦!");
22 }
23
24 @Override
25 public void createCarNum()
26 {
27 house.carNum=0;
28 System.out.println("平民房可是没有车库的哦!");
29 }
30
31 @Override
32 public void createToliet()
33 {
34 house.toliet=1;
35 System.out.println("平民房就一个厕所!");
36 }
37
38 @Override
39 public House createHouse()
40 {
41 System.out.println("建造一个平民房!");
42 createArea();
43 createCarNum();
44 createSleeping();
45 createToliet();
46 return house;
47 }
48 }
4.建造豪华房BuildImplS
1 package com.Mark.chapter04.生成器.ToUnderstand.chapter04.exercise;
2
3 public class BuilderImplS implements IBuilder
4 {
5 private House house;
6 public BuilderImplS(House house)
7 {
8 this.house=house;
9 }
10 @Override
11 public void createArea()
12 {
13 house.area=100;
14 System.out.println("土豪房子可是不一样的大100平!");
15 }
16
17 @Override
18 public void createSleeping()
19 {
20 house.sleeping=5;
21 System.out.println("土豪房有5张床!");
22 }
23
24 @Override
25 public void createCarNum()
26 {
27 house.carNum=666;
28 System.out.println("土豪房子车库数不清!");
29 }
30
31 @Override
32 public void createToliet()
33 {
34 house.toliet=666;
35 System.out.println("土豪房子厕所大大的!");
36 }
37
38 @Override
39 public House createHouse()
40 {
41 System.out.println("土豪房子建好啦!");
42 createArea();
43 createCarNum();
44 createSleeping();
45 createToliet();
46 return house;
47 }
48 }
5.指挥建造Director
1 package com.Mark.chapter04.生成器.ToUnderstand.chapter04.exercise;
2
3 public class Director
4 {
5 private IBuilder iBuilder;
6 public Director(IBuilder iBuilder)
7 {
8 this.iBuilder=iBuilder;
9 }
10 public void build()
11 {
12 iBuilder.createHouse();
13 }
14
15 }
6.测试Test
1 package com.Mark.chapter04.生成器.ToUnderstand.chapter04.exercise;
2
3 public class Test
4 {
5 public static void main(String[] args)
6 {
7 House house = new House();
8 // 普通房
9 IBuilder iBuilder1=new BuilderImplP(house);
10 Director director1=new Director(iBuilder1);
11 director1.build();
12
13 System.out.println("");
14 // 土豪房
15 IBuilder iBuilder2=new BuilderImplS(house);
16 Director director2=new Director(iBuilder2);
17 director2.build();
18
19 }
20 }
输出结果:
建造一个平民房!
平民房就一平米哦!
平民房可是没有车库的哦!
平民房就一个卧室哦!
平民房就一个厕所!
土豪房子建好啦!
土豪房子可是不一样的大100平!
土豪房子车库数不清!
土豪房有5张床!
土豪房子厕所大大的!
配合这篇教学可以更加深刻的理解:生成器模式实例——更通俗易懂的去理解生成器模式