设计模式之——建造者模式

建造者模式在抽象类的构造上有点像模板方法,类方法是在父类中构造的而非子类继承重写,首先以最常用的类开始:

  1 package Jianzaozhe;

  2 

  3 import java.util.ArrayList;

  4 import java.util.List;

  5 

  6 public class Client {

  7     public static void main(String[] args) {

  8         BenzModel benzModel = new BenzModel();

  9         ArrayList<String>sequence = new ArrayList<>();

 10         sequence.add("engine boom");

 11         sequence.add("start");

 12         sequence.add("stop");

 13         benzModel.setSequence(sequence);

 14         benzModel.run();

 15     }

 16 }

 17 

 18 abstract class CarModel {

 19     private List<String> sequence = new ArrayList<>();

 20 

 21     protected abstract void start();

 22 

 23     protected abstract void stop();

 24 

 25     protected abstract void alarm();

 26 

 27     protected abstract void engineBoom();

 28 

 29     final public void run() {

 30         for (int i = 0; i < this.sequence.size(); i++) {

 31             String actionName = this.sequence.get(i);

 32             if (actionName.equalsIgnoreCase("start")) {

 33                 this.start(); // 开启汽车

 34             } else if (actionName.equalsIgnoreCase("stop")) {

 35                 this.stop(); // 停止汽车

 36             } else if (actionName.equalsIgnoreCase("alarm")) {

 37                 this.alarm(); // 喇叭开始叫了

 38             } else if (actionName.equalsIgnoreCase("engine boom")) { // 如果是engine

 39                                                                         // boom关键字

 40                 this.engineBoom(); // 引擎开始轰鸣

 41             }

 42         }

 43     }

 44 

 45     public List<String> getSequence() {

 46         return sequence;

 47     }

 48 

 49     public void setSequence(List<String> sequence) {

 50         this.sequence = sequence;

 51     }

 52 

 53 }

 54 

 55 class BenzModel extends CarModel {

 56 

 57     @Override

 58     protected void start() {

 59         // TODO Auto-generated method stub

 60         System.out.println("奔驰开始了...");

 61     }

 62 

 63     @Override

 64     protected void stop() {

 65         // TODO Auto-generated method stub

 66         System.out.println("奔驰停止了...");

 67     }

 68 

 69     @Override

 70     protected void alarm() {

 71         // TODO Auto-generated method stub

 72         System.out.println("奔驰按喇叭...");

 73     }

 74 

 75     @Override

 76     protected void engineBoom() {

 77         // TODO Auto-generated method stub

 78         System.out.println("奔驰引擎开启...");

 79     }

 80 

 81 }

 82 

 83 class BMWModel extends CarModel {

 84     @Override

 85     protected void start() {

 86         // TODO Auto-generated method stub

 87         System.out.println("宝马开始了...");

 88     }

 89 

 90     @Override

 91     protected void stop() {

 92         // TODO Auto-generated method stub

 93         System.out.println("宝马停止了...");

 94     }

 95 

 96     @Override

 97     protected void alarm() {

 98         // TODO Auto-generated method stub

 99         System.out.println("宝马按喇叭...");

100     }

101 

102     @Override

103     protected void engineBoom() {

104         // TODO Auto-generated method stub

105         System.out.println("宝马引擎开启...");

106     }

107 }

测试结果:

奔驰引擎开启...
奔驰开始了...
奔驰停止了...

 

2

在Client类中可以选择我们要执行的动作的顺序,但是具体到每一个动作我们都要重新装配一遍,这样太麻烦了,我们可以为每个模型定义一个装配类,由它来定义子类东走执行的顺序:

 1 abstract class CarBuilder{

 2     public abstract void setSequence(List<String> sequence);

 3     public abstract CarModel getCarModel();

 4 }

 5 

 6 class BenzBuilder extends CarBuilder{

 7 

 8     private BenzModel benzModel = new BenzModel();

 9     @Override

10     public void setSequence(List<String> sequence) {

11         // TODO Auto-generated method stub

12         this.benzModel.setSequence(sequence);

13     }

14 

15     @Override

16     public CarModel getCarModel() {

17         // TODO Auto-generated method stub

18         return this.benzModel;

19     }

20     

21 }

22 

23 class BMWBuilder extends CarBuilder{

24     private BMWModel bmwModel = new BMWModel();

25 

26     @Override

27     public void setSequence(List<String> sequence) {

28         // TODO Auto-generated method stub

29         this.bmwModel.setSequence(sequence);

30     }

31 

32     @Override

33     public CarModel getCarModel() {

34         // TODO Auto-generated method stub

35         return this.bmwModel;

36     }

37     

38 }

修改Client

 1 public class Client{

 2     public static void main(String[] args) {

 3         List<String>sequence = new ArrayList<>();

 4         sequence.add("engine boom");

 5         sequence.add("start");

 6         sequence.add("stop");

 7         

 8         BenzBuilder benzBuilder = new BenzBuilder();

 9         benzBuilder.setSequence(sequence);

10         BenzModel benzModel = (BenzModel)benzBuilder.getCarModel();

11         benzModel.run();

12         

13         BMWBuilder bmwBuilder = new BMWBuilder();

14         bmwBuilder.setSequence(sequence);

15         BMWModel bmwModel = (BMWModel)bmwBuilder.getCarModel();

16         bmwModel.run();

17     }

18 }

测试结果:

奔驰引擎开启...
奔驰开始了...
奔驰停止了...
宝马引擎开启...
宝马开始了...
宝马停止了...

 

3

上面利用Builder对getModel和getSequence两个方法进行了封装,对于未知顺序的类我们还是无法改变,这时就要利用导演类Director了:

 1 class Director{

 2     private List<String>sequence = new ArrayList<>();

 3     private BenzBuilder benzBuilder = new BenzBuilder();

 4     private BMWBuilder bmwBuilder = new BMWBuilder();

 5     

 6     public BenzModel getABenzModel(){

 7         this.sequence.clear();

 8         this.sequence.add("start");

 9         this.sequence.add("stop");

10         this.benzBuilder.setSequence(sequence);

11         return (BenzModel)this.benzBuilder.getCarModel();

12     }

13     

14     public BenzModel getBBenzModel(){

15         this.sequence.clear();

16         this.sequence.add("engine boom");

17         this.sequence.add("alarm");

18         this.benzBuilder.setSequence(sequence);

19         return (BenzModel)this.benzBuilder.getCarModel();

20     }

21     

22     public BMWModel getABmwModel(){

23         this.sequence.clear();

24         this.sequence.add("start");

25         this.sequence.add("stop");

26         this.bmwBuilder.setSequence(sequence);

27         return (BMWModel)this.bmwBuilder.getCarModel();

28     }

29     

30     public BMWModel getBBmwModel(){

31         this.sequence.clear();

32         this.sequence.add("engine boom");

33         this.sequence.add("alarm");

34         this.benzBuilder.setSequence(sequence);

35         return (BMWModel)this.bmwBuilder.getCarModel();

36     }

37 }

修改Client

 1 public class Client{

 2     public static void main(String[] args) {

 3         Director director = new Director();

 4         for (int i = 0; i < 5; i++) {

 5             director.getABenzModel().run();

 6         }

 7         for (int i = 0; i < 5; i++) {

 8             director.getBBenzModel().run();

 9         }

10         for (int i = 0; i < 5; i++) {

11             director.getABmwModel().run();

12         }

13         for (int i = 0; i < 5; i++) {

14             director.getBBmwModel().run();

15         }

16     }

17 }

测试结果:

奔驰开始了...
奔驰停止了...
奔驰开始了...
奔驰停止了...
奔驰开始了...
奔驰停止了...
奔驰开始了...
奔驰停止了...
奔驰开始了...
奔驰停止了...
奔驰引擎开启...
奔驰按喇叭...
奔驰引擎开启...
奔驰按喇叭...
奔驰引擎开启...
奔驰按喇叭...
奔驰引擎开启...
奔驰按喇叭...
奔驰引擎开启...
奔驰按喇叭...
宝马开始了...
宝马停止了...
宝马开始了...
宝马停止了...
宝马开始了...
宝马停止了...
宝马开始了...
宝马停止了...
宝马开始了...
宝马停止了...
宝马引擎开启...
宝马按喇叭...
宝马引擎开启...
宝马按喇叭...
宝马引擎开启...
宝马按喇叭...
宝马引擎开启...
宝马按喇叭...
宝马引擎开启...
宝马按喇叭...

 

所有代码如下:

  1 package Jianzaozhe;

  2 

  3 import java.util.ArrayList;

  4 import java.util.List;

  5 

  6 /*public class Client {

  7     public static void main(String[] args) {

  8         BenzModel benzModel = new BenzModel();

  9         List<String>sequence = new ArrayList<>();

 10         sequence.add("engine boom");

 11         sequence.add("start");

 12         sequence.add("stop");

 13         benzModel.setSequence(sequence);

 14         benzModel.run();

 15     }

 16 }*/

 17 

 18 /*public class Client{

 19     public static void main(String[] args) {

 20         List<String>sequence = new ArrayList<>();

 21         sequence.add("engine boom");

 22         sequence.add("start");

 23         sequence.add("stop");

 24         

 25         BenzBuilder benzBuilder = new BenzBuilder();

 26         benzBuilder.setSequence(sequence);

 27         BenzModel benzModel = (BenzModel)benzBuilder.getCarModel();

 28         benzModel.run();

 29         

 30         BMWBuilder bmwBuilder = new BMWBuilder();

 31         bmwBuilder.setSequence(sequence);

 32         BMWModel bmwModel = (BMWModel)bmwBuilder.getCarModel();

 33         bmwModel.run();

 34     }

 35 }*/

 36 

 37 public class Client{

 38     public static void main(String[] args) {

 39         Director director = new Director();

 40         for (int i = 0; i < 5; i++) {

 41             director.getABenzModel().run();

 42         }

 43         for (int i = 0; i < 5; i++) {

 44             director.getBBenzModel().run();

 45         }

 46         for (int i = 0; i < 5; i++) {

 47             director.getABmwModel().run();

 48         }

 49         for (int i = 0; i < 5; i++) {

 50             director.getBBmwModel().run();

 51         }

 52     }

 53 }

 54 

 55 abstract class CarModel {

 56     private List<String> sequence = new ArrayList<>();

 57 

 58     protected abstract void start();

 59 

 60     protected abstract void stop();

 61 

 62     protected abstract void alarm();

 63 

 64     protected abstract void engineBoom();

 65 

 66     final public void run() {

 67         for (int i = 0; i < this.sequence.size(); i++) {

 68             String actionName = this.sequence.get(i);

 69             if (actionName.equalsIgnoreCase("start")) {

 70                 this.start(); // 开启汽车

 71             } else if (actionName.equalsIgnoreCase("stop")) {

 72                 this.stop(); // 停止汽车

 73             } else if (actionName.equalsIgnoreCase("alarm")) {

 74                 this.alarm(); // 喇叭开始叫了

 75             } else if (actionName.equalsIgnoreCase("engine boom")) { // 如果是engine

 76                                                                         // boom关键字

 77                 this.engineBoom(); // 引擎开始轰鸣

 78             }

 79         }

 80     }

 81 

 82     public List<String> getSequence() {

 83         return sequence;

 84     }

 85 

 86     public void setSequence(List<String> sequence) {

 87         this.sequence = sequence;

 88     }

 89 

 90 }

 91 

 92 class BenzModel extends CarModel {

 93 

 94     @Override

 95     protected void start() {

 96         // TODO Auto-generated method stub

 97         System.out.println("奔驰开始了...");

 98     }

 99 

100     @Override

101     protected void stop() {

102         // TODO Auto-generated method stub

103         System.out.println("奔驰停止了...");

104     }

105 

106     @Override

107     protected void alarm() {

108         // TODO Auto-generated method stub

109         System.out.println("奔驰按喇叭...");

110     }

111 

112     @Override

113     protected void engineBoom() {

114         // TODO Auto-generated method stub

115         System.out.println("奔驰引擎开启...");

116     }

117 

118 }

119 

120 class BMWModel extends CarModel {

121     @Override

122     protected void start() {

123         // TODO Auto-generated method stub

124         System.out.println("宝马开始了...");

125     }

126 

127     @Override

128     protected void stop() {

129         // TODO Auto-generated method stub

130         System.out.println("宝马停止了...");

131     }

132 

133     @Override

134     protected void alarm() {

135         // TODO Auto-generated method stub

136         System.out.println("宝马按喇叭...");

137     }

138 

139     @Override

140     protected void engineBoom() {

141         // TODO Auto-generated method stub

142         System.out.println("宝马引擎开启...");

143     }

144 }

145 

146 abstract class CarBuilder{

147     public abstract void setSequence(List<String> sequence);

148     public abstract CarModel getCarModel();

149 }

150 

151 class BenzBuilder extends CarBuilder{

152 

153     private BenzModel benzModel = new BenzModel();

154     @Override

155     public void setSequence(List<String> sequence) {

156         // TODO Auto-generated method stub

157         this.benzModel.setSequence(sequence);

158     }

159 

160     @Override

161     public CarModel getCarModel() {

162         // TODO Auto-generated method stub

163         return this.benzModel;

164     }

165     

166 }

167 

168 class BMWBuilder extends CarBuilder{

169     private BMWModel bmwModel = new BMWModel();

170 

171     @Override

172     public void setSequence(List<String> sequence) {

173         // TODO Auto-generated method stub

174         this.bmwModel.setSequence(sequence);

175     }

176 

177     @Override

178     public CarModel getCarModel() {

179         // TODO Auto-generated method stub

180         return this.bmwModel;

181     }

182     

183 }

184 

185 class Director{

186     private List<String>sequence = new ArrayList<>();

187     private BenzBuilder benzBuilder = new BenzBuilder();

188     private BMWBuilder bmwBuilder = new BMWBuilder();

189     

190     public BenzModel getABenzModel(){

191         this.sequence.clear();

192         this.sequence.add("start");

193         this.sequence.add("stop");

194         this.benzBuilder.setSequence(sequence);

195         return (BenzModel)this.benzBuilder.getCarModel();

196     }

197     

198     public BenzModel getBBenzModel(){

199         this.sequence.clear();

200         this.sequence.add("engine boom");

201         this.sequence.add("alarm");

202         this.benzBuilder.setSequence(sequence);

203         return (BenzModel)this.benzBuilder.getCarModel();

204     }

205     

206     public BMWModel getABmwModel(){

207         this.sequence.clear();

208         this.sequence.add("start");

209         this.sequence.add("stop");

210         this.bmwBuilder.setSequence(sequence);

211         return (BMWModel)this.bmwBuilder.getCarModel();

212     }

213     

214     public BMWModel getBBmwModel(){

215         this.sequence.clear();

216         this.sequence.add("engine boom");

217         this.sequence.add("alarm");

218         this.benzBuilder.setSequence(sequence);

219         return (BMWModel)this.bmwBuilder.getCarModel();

220     }

221 }
View Code

 

你可能感兴趣的:(建造者模式)