设计模式——建造者模式

来源:互联网 发布:qq飞车神影官网的数据 编辑:程序博客网 时间:2024/04/28 07:13

http://blog.csdn.net/dawanganban/article/details/9990405

什么是建造者模式

Builder模式也叫建造者模式或者生成器模式,是由GoF提出的23种设计模式中的一种。Builder模式是一种对象创建型模式之一,用来隐藏复合对象的创建过程,它把复合对象的创建过程加以抽象,通过子类继承和重载的方式,动态地创建具有复合属性的对象。

首先我们建立一个House对象

  1. package com.meritit;  
  2. /** 
  3.  * 房子 
  4.  * @author 李小强 
  5.  * 
  6.  */  
  7. public class House {  
  8.     //地板  
  9.     private String floor;  
  10.     //墙  
  11.     private String wall;  
  12.     //屋顶  
  13.     private String housetop;  
  14.       
  15.     public String getFloor() {  
  16.         return floor;  
  17.     }  
  18.     public void setFloor(String floor) {  
  19.         this.floor = floor;  
  20.     }  
  21.     public String getWall() {  
  22.         return wall;  
  23.     }  
  24.     public void setWall(String wall) {  
  25.         this.wall = wall;  
  26.     }  
  27.     public String getHousetop() {  
  28.         return housetop;  
  29.     }  
  30.     public void setHousetop(String housetop) {  
  31.         this.housetop = housetop;  
  32.     }  
  33.       
  34.       
  35. }  


 

如果按照普通方法,我们去这样建造房子

  1. package com.meritit;  
  2. /** 
  3.  *  
  4.  * @author 李小强 
  5.  * 
  6.  */  
  7. public class MainClass {  
  8.     public static void main(String[] args) {  
  9.         //客户直接造房子  
  10.         House house = new House();  
  11.         house.setFloor("地板");  
  12.         house.setHousetop("屋顶");  
  13.         house.setWall("墙");  
  14.     }  
  15. }  


 

上面的这种方式实际上是让客户自己造房子,这显然不好,下面我们开始用建造者模式实现:

创建一个HouseBuilder接口

  1. package com.meritit;  
  2. /** 
  3.  * 造房子的接口 
  4.  * @author 李小强 
  5.  * 
  6.  */  
  7. public interface HouseBuilder {  
  8.     //修地板  
  9.     public void makeFloor();  
  10.     //修墙  
  11.     public void makeWall();  
  12.     //修屋顶  
  13.     public void makeHousetop();  
  14.     //获得房子  
  15.     public House getHouse();  
  16. }  


用具体的造房子的工程实现

  1. package com.meritit;  
  2. /** 
  3.  * 平房工程队 
  4.  * @author 李小强 
  5.  * 
  6.  */  
  7. public class PingFangBuilder implements HouseBuilder{  
  8.     House house = new House();  
  9.   
  10.     @Override  
  11.     public void makeFloor() {  
  12.         house.setFloor("平房地板");  
  13.           
  14.     }  
  15.   
  16.     @Override  
  17.     public void makeWall() {  
  18.         house.setWall("平房房顶");  
  19.     }  
  20.   
  21.     @Override  
  22.     public void makeHousetop() {  
  23.         house.setHousetop("平房屋顶");  
  24.     }  
  25.   
  26.     @Override  
  27.     public House getHouse() {  
  28.         return house;  
  29.     }  
  30.   
  31. }  


现在这样造房子

  1. package com.meritit;  
  2. /** 
  3.  *  
  4.  * @author 李小强 
  5.  * 
  6.  */  
  7. public class MainClass {  
  8.     public static void main(String[] args) {  
  9.         //由工程队来修房子  
  10.         HouseBuilder builder = new PingFangBuilder();  
  11.         builder.makeFloor();  
  12.         builder.makeHousetop();  
  13.         builder.makeWall();  
  14.         House house = builder.getHouse();  
  15.           
  16.         System.out.println(house.getFloor());  
  17.         System.out.println(house.getHousetop());  
  18.         System.out.println(house.getWall());  
  19.     }  
  20. }  

 

这里好像缺少一个专门建造房子的角色,下面我们这样做:

创建一个实际的建造者去造房子

  1. package com.meritit;  
  2. /** 
  3.  * 实际建造者 
  4.  * @author 李小强 
  5.  * 
  6.  */  
  7. public class HouseDicrector {  
  8.       
  9.     private HouseBuilder builder;  
  10.       
  11.     public HouseDicrector(HouseBuilder builder){  
  12.         this.builder = builder;  
  13.     }  
  14.       
  15.     /** 
  16.      * 建造房子 
  17.      */  
  18.     public void makeHouse(){  
  19.         builder.makeFloor();  
  20.         builder.makeHousetop();  
  21.         builder.makeWall();  
  22.     }  
  23. }  

 

现在Main方法中就可以这样造房子了:


 

  1. package com.meritit;  
  2.   
  3. import javax.naming.directory.DirContext;  
  4.   
  5. /** 
  6.  *  
  7.  * @author 李小强 
  8.  * 
  9.  */  
  10. public class MainClass {  
  11.     public static void main(String[] args) {  
  12.         //设计房子  
  13.         HouseBuilder builder = new PingFangBuilder();  
  14.         //创造工程队  
  15.         HouseDicrector dicrector = new HouseDicrector(builder);  
  16.         //开始造  
  17.         dicrector.makeHouse();  
  18.         //得到房子  
  19.         House house = builder.getHouse();  
  20.           
  21.         System.out.println(house.getFloor());  
  22.         System.out.println(house.getHousetop());  
  23.         System.out.println(house.getWall());  
  24.     }  
  25. }  


  这就是建造者设计模式,如果现在你想建造公寓只需要先设计一个公寓就行。

设计公寓

  1. package com.meritit;  
  2. /** 
  3.  * 设计公寓 
  4.  * @author 李小强 
  5.  * 
  6.  */  
  7. public class GongyuBuilder implements HouseBuilder{  
  8.     House house = new House();  
  9.     @Override  
  10.     public void makeFloor() {  
  11.          house.setFloor("公寓地板");  
  12.     }  
  13.   
  14.     @Override  
  15.     public void makeWall() {  
  16.         house.setWall("公寓墙");  
  17.     }  
  18.   
  19.     @Override  
  20.     public void makeHousetop() {  
  21.         house.setHousetop("公寓房顶");  
  22.     }  
  23.   
  24.     @Override  
  25.     public House getHouse() {  
  26.         return house;  
  27.     }  
  28.   
  29. }  


在Main方法中只需将Pingfang平房改为Gongyu

  1. package com.meritit;  
  2.   
  3. import javax.naming.directory.DirContext;  
  4.   
  5. /** 
  6.  *  
  7.  * @author 李小强 
  8.  * 
  9.  */  
  10. public class MainClass {  
  11.     public static void main(String[] args) {  
  12.         //设计房子  
  13.         HouseBuilder builder = new GongyuBuilder();  
  14.         //创造工程队  
  15.         HouseDicrector dicrector = new HouseDicrector(builder);  
  16.         //开始造  
  17.         dicrector.makeHouse();  
  18.         //得到房子  
  19.         House house = builder.getHouse();  
  20.           
  21.           
  22.         System.out.println(house.getFloor());  
  23.         System.out.println(house.getHousetop());  
  24.         System.out.println(house.getWall());  
  25.     }  
  26. }  

 

我们还可以再改一下

  1. package com.meritit;  
  2. /** 
  3.  * 实际建造者 
  4.  * @author 李小强 
  5.  * 
  6.  */  
  7. public class HouseDicrector {  
  8.     /** 
  9.      * 建造房子 
  10.      */  
  11.     public void makeHouse(HouseBuilder builder){  
  12.         builder.makeFloor();  
  13.         builder.makeHousetop();  
  14.         builder.makeWall();  
  15.     }  
  16. }  


 

这样在Main方法中可以这样做:

  1. package com.meritit;  
  2.   
  3. import javax.naming.directory.DirContext;  
  4.   
  5. /** 
  6.  *  
  7.  * @author 李小强 
  8.  * 
  9.  */  
  10. public class MainClass {  
  11.     public static void main(String[] args) {  
  12.         //设计房子  
  13.         HouseBuilder builder = new GongyuBuilder();  
  14.         //创造工程队  
  15.         HouseDicrector dicrector = new HouseDicrector();  
  16.         //开始造  
  17.         dicrector.makeHouse(builder);  
  18.         //得到房子  
  19.         House house = builder.getHouse();  
  20.           
  21.           
  22.         System.out.println(house.getFloor());  
  23.         System.out.println(house.getHousetop());  
  24.         System.out.println(house.getWall());  
  25.     }  
  26. }  


 

建造者模式的结构如图:

 

 

建造者模式的应用场景:

-对象的创建:Builder模式是为对象的创建而设计的模式

- 创建的是一个复合对象:被创建的对象为一个具有复合属性的复合对象

- 关注对象创建的各部分的创建过程:不同的工厂(这里指builder生成器)对产品属性有不同的创建方法

 

源代码下载:http://download.csdn.net/detail/lxq_xsyu/5956163

0 0