?
工厂类模式提供的是创建单个类的模式,而建造者模式则是将各种产品集中起来进行管理,用来创建复合对象,所谓复合对象就是指某个类具有不同的属性,其实建造者模式就是前面抽象工厂模式和最后的Test结合起来得到的。我们看一下代码:
还和前面一样,一个Sender接口,两个实现类MailSender和SmsSender。最后,建造者类如下:
[java]?view plaincopy其次,创建实现类:
[java]?view plaincopy测试类:
[java]?view plaincopy从这点看出,建造者模式将很多功能集成到一个类里,这个类可以创造出比较复杂的东西。所以与工程模式的区别就是:工厂模式关注的是创建单个产品,而建造者模式则关注创建符合对象,多个部分。因此,是选择工厂模式还是建造者模式,依实际情况而定。
?
==================================================================================
?
Builder: 主要用来简化一个复杂的对象的创建。这个模式也可以用来实现一个 Fluent Interface。 java.lang.StringBuilder#append() java.lang.StringBuffer#append() java.sql.PreparedStatement javax.swing.GroupLayout.Group#addComponent() abstract class Glass{ } class AmericanGlass extends Glass{ public String toString(){ return "\"American Glass\" "; } } class JapaneseGlass extends Glass{ public String toString(){ return "\"Japanese Glass\" "; } } /** * 定义部件wheel的抽象类Wheel * 和两个具体类AmericanWheel、JapaneseWheel */ abstract class Wheel{ } class AmericanWheel extends Wheel{ public String toString(){ return "\"American Wheel\" "; } } class JapaneseWheel extends Wheel{ public String toString(){ return "\"Japanese Wheel\" "; } } /** * 定义部件engine的抽象类Engine * 和两个具体类ChineseEngine、FranceEngine */ abstract class Engine{ } class ChineseEngine extends Engine{ public String toString(){ return "\"Chinese Engine\" "; } } class FranceEngine extends Engine{ public String toString(){ return "\"France Engine\" "; } } /** * 定义产品类Car */ class Car{ Glass glass; Wheel wheel; Engine engine; } /** * 定义抽象建造器接口Builder */ interface CarBuilder{ public void buildGlass(); public void buildWheel(); public void buildEngine(); public Car getProduct(); } /** * 具体建造器类CarABuilder * CarA=AmericanGlass+JapaneseWheel+ChineseEngine */ class CarABuilder implements CarBuilder{ private Car product=null; public CarABuilder(){ product=new Car(); } /** * 将建造部件的工作封装在getProduct()操作中,主要是为了向客户隐藏实现细节 * 这样,具体建造类同时又起到了一个director的作用 */ @Override public void buildEngine() { // TODO Auto-generated method stub product.engine=new ChineseEngine(); } @Override public void buildGlass() { // TODO Auto-generated method stub product.glass=new AmericanGlass(); } @Override public void buildWheel() { // TODO Auto-generated method stub product.wheel=new JapaneseWheel(); } @Override public Car getProduct() { // TODO Auto-generated method stub buildGlass(); buildWheel(); buildEngine(); return product; } } /** * 具体建造器类CarABuilder * CarB=JapaneseGlass+AmericanWheel+FranceEngine */ class CarBBuilder implements CarBuilder{ private Car product; public CarBBuilder(){ product=new Car(); } /** * 将建造部件的工作封装在getProduct()操作中,主要是为了向客户隐藏实现细节 * 这样,具体建造类同时又起到了一个director的作用 */ @Override public void buildEngine() { // TODO Auto-generated method stub product.engine=new FranceEngine(); } @Override public void buildGlass() { // TODO Auto-generated method stub product.glass=new JapaneseGlass(); } @Override public void buildWheel() { // TODO Auto-generated method stub product.wheel=new AmericanWheel(); } @Override public Car getProduct() { // TODO Auto-generated method stub buildGlass(); buildWheel(); buildEngine(); return product; } } class Director{ private CarBuilder builder; public Director(CarBuilder builder) { this.builder = builder; } public Car construct() { return builder.getProduct(); } } /** * 客户端代码,使用Director创建两种不同型别的CarA和CarB */ public class test{ public static void main(String[] args){ Car carA, CarBuilder builderA = new CarABuilder(); CarBuilder builderB = new CarBBuilder(); Director director; director = new Director(builderA); carA=director.construct(); director = new Director(builderB); carB=director.construct(); System.out.println("Car A is made by:"+carA.glass+carA.wheel+carA.engine); System.out.println("Car B is made by:"+carB.glass+carB.wheel+carB.engine); } }?