常用设计模式总结--生成器模式

Builder ,生成器模式。

Builder 模式将一个对象的创建与表示分离,从而使得相同的创建过程可以创建出不同的表示对象。

仍以汽车工厂举例,一个汽车集团下面有许多工厂,每个工厂生产出不同的车型。站在集团的层面,其实集团只需要知道工厂造轮子,造引擎,而并不关心轮子的具体尺寸,引擎的具体马力,它只需要知道工厂最终返回的给它的是一辆完整的汽车,就一切 ok

类图

 

常用设计模式总结--生成器模式_第1张图片

在这个类图中,省略了 Wheel,Engine,Vehicle DasAuto 为新引入的一个类,包含着一个对具体 builder 父类的引用。而 VehicleBuilder 中加入一个 getVehicle ()的方法,用来返回创建的 Vehicle

 

代码

对于汽车的零件,参考抽象工厂的日志

 

1 DasAuto

package zl.study.designpattern.builder; /** * DasAuto能够提供汽车和摩托车,具体的车型则要看是 * 哪个工厂提供。 * * @author peter * */ public class DasAuto { public Vehicle createNormalCar(VehicleBuilder builder){ builder.buildVehicle(); builder.buildEngine(); builder.buildWheel(); return builder.getVehicle(); } public Vehicle createRaceCar(VehicleBuilder builder ){ builder.buildVehicle(); builder.buildEngine(); builder.buildWheel(); builder.buildWheel(); return builder.getVehicle(); } }

 

2 VehicleBuilder

 

package zl.study.designpattern.builder; public interface VehicleBuilder { public void buildVehicle(); public void buildEngine(); public void buildWheel(); public Vehicle getVehicle(); }

 

3 NormalVehicleBuilder

package zl.study.designpattern.builder; public class NormalVehicleBuilder implements VehicleBuilder{ private Vehicle vehicle; @Override public void buildEngine() { Engine engine = new NormalEngine(); vehicle.setEngine(engine); } @Override public void buildVehicle() { if( null == vehicle) vehicle = new NormalVehicle(); } @Override public void buildWheel() { Wheel wheel1 = new NormalWheel(); Wheel wheel2 = new NormalWheel(); Wheel wheel3 = new NormalWheel(); Wheel wheel4 = new NormalWheel(); vehicle.addWheel(wheel1); vehicle.addWheel(wheel2); vehicle.addWheel(wheel3); vehicle.addWheel(wheel4); } @Override public Vehicle getVehicle() { return this.vehicle; } }

 

4 RaceVehicleBuilder

package zl.study.designpattern.builder; public class RaceVehicleBuilder implements VehicleBuilder{ private Vehicle vehicle; @Override public void buildEngine() { Engine engine = new RaceEngine(); vehicle.setEngine(engine); } @Override public void buildVehicle() { if( null == vehicle){ vehicle = new RaceVehicle(); } } @Override public void buildWheel() { Wheel wheel1 = new RaceWheel(); Wheel wheel2 = new RaceWheel(); Wheel wheel3 = new RaceWheel(); Wheel wheel4 = new RaceWheel(); vehicle.addWheel(wheel1); vehicle.addWheel(wheel2); vehicle.addWheel(wheel3); vehicle.addWheel(wheel4); } @Override public Vehicle getVehicle() { // TODO Auto-generated method stub return null; } }

 

5 测试程序

 

package zl.study.designpattern.builder.test;

 

import zl.study.designpattern.builder.DasAuto;
import zl.study.designpattern.builder.NormalVehicleBuilder;
import zl.study.designpattern.builder.Vehicle;
import zl.study.designpattern.builder.VehicleBuilder;

public class BuilderTest {

 public static void main(String args[]){
  VehicleBuilder builder = new NormalVehicleBuilder();
  
  DasAuto dasAuto = new DasAuto();
  
  Vehicle normalVehicle = dasAuto.createNormalCar(builder);
  
  System.out.println(normalVehicle);
  
  
 }
}

 

 

你可能感兴趣的:(设计模式,String,null,Class,interface,引擎)