Design Pattern: Builder 模式

简介:

  学习是分享和合作式的!

转载请注明出处:http://blog.csdn.net/wdzxl198/article/details/9248365; 

文章摘自: http://www.riabook.cn/doc/designpattern/; 

您想要建立一个迷宫产生程式,迷宫使用二维阵列来定义,0表示道路,1表示墙,2表示宝物,根据所定义的二维迷宫阵列,您想要程式自动产生各种不同材质的迷宫,例如砖墙迷宫,钻石迷宫等等。
您可以在程式中定义两个角色,一个是指导迷宫建立的Director角色,一个是按照指导者指示建立迷宫的Builder角色,Director根据定义的迷宫阵列来指导Builder,只要更换Builder,就可以完成不同材质的迷宫。
可以使用下面的UML 类别图来表示上述的概念:

Builder

实际上的程式设计如下:

  • MazeDirector.java
   1: public class MazeDirector {
   2:     private int[][] maze;
   3:     private IMazeBuilder mazeBuilder;
   4:  
   5:     public void setMaze(int[][] maze) {
   6:         this.maze = maze;
   7:     }
   8:     
   9:     public void setMazeBuilder(IMazeBuilder mazeBuilder) {
  10:         this.mazeBuilder = mazeBuilder;
  11:     }
  12:     
  13:     public void buildMaze() {
  14:         for(int i = 0; i < maze.length; i++) {
  15:             for(int j = 0; j < maze[i].length; j++) {
  16:                 // 由于mazeBuilder是IMazeBuilder型态
  17:                 // 所以无论Builder实例为何,这边的程式都无需变动
  18:                switch (maze[i][j]) {
  19:                     case 0:
  20:                         mazeBuilder.createRoadBlock();
  21:                         break;
  22:                     case 1:
  23:                         mazeBuilder.createWallBlock();
  24:                         break;
  25:                     case 2:
  26:                         mazeBuilder.createTreasureBlock();
  27:                         break;
  28:                     default:
  29:                         System.out.println("undefined");
  30:                 }
  31:             }
  32:             mazeBuilder.nextRow();    
  33:         }
  34:     }
  35: } 
  • IMazeBuilder.java
   1: public interface IMazeBuilder {
   2:     public void createRoadBlock();
   3:     public void createWallBlock();
   4:     public void createTreasureBlock();
   5:     public void nextRow();
   6: } 
  • SoliderMazeBuilder.java
 
 
   1: public class SolidMazeBuilder implements IMazeBuilder {
   2:     public void createWallBlock() {
   3:         System.out.print("█");
   4:     }
   5:     
   6:     public void createRoadBlock() {
   7:         System.out.print(" ");
   8:     }
   9:     
  10:     public void createTreasureBlock() {
  11:         System.out.print("$ ");
  12:     }
  13:     
  14:     public void nextRow() {
  15:         System.out.println();
  16:     }
  17: }

  • DiamondMazeBuilder.java
   1: public class DiamondMazeBuilder implements IMazeBuilder {
   2:     public void createWallBlock() {
   3:         System.out.print("◇");
   4:     }
   5:     
   6:     public void createRoadBlock() {
   7:         System.out.print(" ");
   8:     }
   9:     
  10:     public void createTreasureBlock() {
  11:         System.out.print("* ");
  12:     }
  13:     
  14:     public void nextRow() {
  15:         System.out.println();
  16:     }    
  17: }  

使用下面的程式来测试一下,它将产生两个迷宫图形:
   1: public class Main {
   2:     public static void main(String[] args) {
   3:         int[][] maze = {{1, 1, 1, 1, 1, 1, 1}, 
   4:                         {1, 0, 0, 0, 0, 2, 1}, 
   5:                         {1, 0, 1, 0, 1, 0, 1}, 
   6:                         {1, 0, 2, 1, 0, 1, 1}, 
   7:                         {1, 1, 0, 1, 0, 1, 1}, 
   8:                         {1, 0, 0, 2, 0, 0, 1}, 
   9:                         {1, 1, 1, 1, 1, 1, 1}};
  10:         
  11:         MazeDirector mazeDirector = new MazeDirector();
  12:         mazeDirector.setMaze(maze);
  13:         
  14:         System.out.println("Build SolidMaze....");
  15:         mazeDirector.setMazeBuilder(new SolidMazeBuilder());
  16:         mazeDirector.buildMaze();
  17:         
  18:         System.out.println("Build DiamondMaze....");
  19:         
  20:         mazeDirector.setMazeBuilder(
  21:                        new DiamondMazeBuilder());
  22:         mazeDirector.buildMaze();
  23:     }
  24: } 

Builder

在迷宫例子中并没有产生或返回产品物件,这视您的需求而定,迷宫例子只是将结果输出至主控台,您也可以设计一个产品物件,或是将结果直接输出为文件。
在 Gof (Design Patterns Elements of Reusable Object-Oriented Software)中有给出了一个不错的例子,以设计文件剖析器为例,该剖析器可以将文件转换为其它的格式,以DOC文件剖析器为例好了,假设希望析剖器可以将DOC文件转换为RTF或是PDF文件,可以如下设计结构:

Builder

简单来说,建筑者模式适用的场合,在于使得您可以依赖抽象的建筑蓝图,而实际建造时可以使用不同的实例,这是其之所以命为Builder的原因

Edit by Atlas

Time 2013/7/4 14:23

目录
相关文章