建造者模式Builder
本文的内容:
一、建造者模式的定义
将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
可以理解为对象的分步构建或者逐步完善。
二、建造者模式的参与者及其角色
1. Builder:
- 为创建一个产品对象的各个部件指定抽象接口。
2. ConcreteBuilder:
- 实现Builder的接口以构造和装配该产品的各个部件。
- 定义并明确它所创建的表示。
- 并提供一个检索产品的接口。
3. Director
- 构造一个使用Builder接口的对象。
4. Product
- 表示被构造的复杂对象。ConcreteBuilder创建该产品的内部表示并定义它的装配过程
- 包含定义组成部件的类,包括将这些部件装配成最终产品的接口。
三、建造者模式的类图
四、建造者模式的示例
下面是一个比较简单粗暴的栗子:
1. Builder:创建对象的抽象接口定义
这定义了一个接口,接口中的方法时对象相关部件的创建方式。
1 public interface WeatherBuilder { 2 3 public void setTemperature(double temperature); 4 public void setHumidity(double humidity); 5 public void setWeatherDescribe(String weatherDescribe); 6 public Weather buildWeather(); 7 }
2. ConcreteBuilder:实现Builder的接口以构造和装配该产品的各个部件。
这是一些具体的实现。实现一:HotWeatherBuilder,代表炎热的天气。
1 public class HotWeatherBuilder implements WeatherBuilder{ 2 3 private Weather weather; 4 public HotWeatherBuilder() { 5 weather = new Weather(); 6 System.out.println("Build HotWeatherBuilder"); 7 } 8 @Override 9 public void setTemperature(double temperature) { 10 this.weather.setTemperature(temperature); 11 } 12 13 @Override 14 public void setHumidity(double humidity) { 15 this.weather.setHumidity(humidity); 16 } 17 18 @Override 19 public void setWeatherDescribe(String weatherDescribe) { 20 this.weather.setWeatherDescribe(weatherDescribe); 21 } 22 23 @Override 24 public Weather buildWeather() { 25 return this.weather; 26 } 27 28 }
实现二:ColdWeatherBuilder代表寒冷天气。
1 public class ColdWeatherBuilder implements WeatherBuilder{ 2 3 private Weather weather; 4 public ColdWeatherBuilder(){ 5 weather = new Weather(); 6 System.out.println("Build ColdWeatherBuilder"); 7 } 8 @Override 9 public void setTemperature(double temperature) { 10 this.weather.setTemperature(temperature); 11 } 12 13 @Override 14 public void setHumidity(double humidity) { 15 this.weather.setHumidity(humidity); 16 } 17 18 @Override 19 public void setWeatherDescribe(String weatherDescribe) { 20 this.weather.setWeatherDescribe(weatherDescribe); 21 } 22 23 @Override 24 public Weather buildWeather() { 25 return this.weather; 26 } 27 28 }
3. Director:构造一个使用Builder接口的对象。
这里用main方法直接干,其实把调用具体的构建过程再做个封装会好点。。。
1 public class BuilderTestApp { 2 //Here we use main-test as a Director 3 public static void main(String[] args) { 4 //use the Builder 5 WeatherBuilder hwb = new HotWeatherBuilder(); 6 hwb.setHumidity(90.0); 7 hwb.setTemperature(35.0); 8 hwb.setWeatherDescribe("It is too hot."); 9 //then get the product 10 Weather hw = hwb.buildWeather(); 11 System.out.println("Humidity : " + hw.getHumidity()); 12 System.out.println("Temperature : " + hw.getTemperature()); 13 System.out.println("WeatherDescribe : " + hw.getWeatherDescribe()); 14 15 WeatherBuilder cwb = new ColdWeatherBuilder(); 16 cwb.setHumidity(30.0); 17 cwb.setTemperature(-5.0); 18 cwb.setWeatherDescribe("It is too cold."); 19 Weather cw = cwb.buildWeather(); 20 System.out.println("Humidity : " + cw.getHumidity()); 21 System.out.println("Temperature : " + cw.getTemperature()); 22 System.out.println("WeatherDescribe : " + cw.getWeatherDescribe()); 23 } 24 }
4. Product:具体的对象,内部有各个组件的装配方式。
1 public class Weather { 2 3 private double temperature; 4 private double humidity; 5 private String weatherDescribe; 6 //all getters and setters 7 public double getTemperature() { 8 return temperature; 9 } 10 public void setTemperature(double temperature) { 11 this.temperature = temperature; 12 } 13 public double getHumidity() { 14 return humidity; 15 } 16 public void setHumidity(double humidity) { 17 this.humidity = humidity; 18 } 19 public String getWeatherDescribe() { 20 return weatherDescribe; 21 } 22 public void setWeatherDescribe(String weatherDescribe) { 23 this.weatherDescribe = weatherDescribe; 24 } 25 }
五、参考
1、参考《Head First设计模式》和GoF《设计模式:可复用面向对象软件的基础》
2、代码可参考【github传送门】、UML类图参考【github传送门】
原文地址:https://www.cnblogs.com/wpbxin/p/9313466.html
时间: 2024-10-16 16:49:30