Design Pattern: Prototype 模式

  学习是分享和合作式的!

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

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

您从图书馆的期刊从发现了几篇您感兴趣的文章,由于这是图书馆的书,您不可以直接在书中作记号或写字,所以您将当中您所感兴趣的几个主题影印出来,这下子您就可在影印的文章上画记重点。
Prototype模式的作用有些类似上面的描述,您在父类别中定义一个clone()方法,而在子类别中重新定义它,当客户端对于所产生的物件有兴趣并想加以利用,而您又不想破坏原来的物件,您可以产生一个物件的复本给它。
Prototype具有展示的意味,就像是展览会上的原型车款,当您对某个车款感兴趣时,您可以购买相同款示的车,而不是车展上的车。
在软体设计上的例子会更清楚的说明为何要进行物件复制,假设您要设计一个室内设计软体,软体中有一个展示家具的工具列,您只要点选工具列就可以产生一个家 具复本,例如一张椅子或桌子,您可以拖曳这个复制的物件至设计图中,随时改变它的位置、颜色等等,当您改变设计图中的物件时,工具列上的原型工具列是不会 跟着一起改变的,这个道理是无需解释的。
下面的 UML 类别图表示了上述的简单概念:

Prototype

Prototype模式的重点在于clone(),它负责复制物件本身并传回,但这个clone()本身在实作上存在一些困难,尤其是当物件本身又继承另一个物件时,如何确保复制的物件完整无误,在不同的程式语言中有不同的作法。
在Java中的作法是透过实作一个Cloneable介面,它只是一个声明的介面,并无规定任何实作的方法,您的目的是改写Object的clone ()方法,使其具备有复制物件的功能,这个方面建议您参考:How to avoid traps and correctly override methods from java.lang.Object
用一个简单的例子来实作上图中的结构,这个例子利用了Java语言本身的clone特性:

  • AbstractFurniture.java
   1: public abstract class AbstractFurniture 
   2:                              implements Cloneable {
   3:     public abstract void draw();
   4:     
   5:     // 在Design Pattern上,以下的clone是抽象未实作的
   6:     // 实际上在Java中class都继承自Object
   7:     // 所以在这边我们直接重新定义clone() 
   8:     // 这是为了符合Java现行的clone机制
   9:     protected Object clone() throws CloneNotSupportedException { 
  10:         return super.clone(); 
  11:     }
  12: }
CircleTable与SquareTable继承了AbstractFurniture,并实作clone方法,用于传回本身的复制品:
  • CircleTable.java
   1: import java.awt.*;
   2:  
   3: public class CircleTable extends AbstractFurniture {
   4:     protected Point center;    
   5:     
   6:     public void setCenter(Point center) {
   7:         this.center = center;
   8:     }
   9:     
  10:     protected Object clone () 
  11:                      throws CloneNotSupportedException { 
  12:         Object o = super.clone(); 
  13:         if(this.center != null) {
  14:             ((CircleTable) o).center = (Point) center.clone();
  15:         }
  16:         
  17:         return o; 
  18:     } 
  19:  
  20:     public void draw() { 
  21:         System.out.println("\t圆桌\t中心:(" + center.getX() 
  22:                             + ", " + center.getY()+ ")");
  23:     } 
  24: }
  • SquareTable.java
   1: import java.awt.*;
   2:  
   3: public class SquareTable extends AbstractFurniture {
   4:     protected Rectangle rectangle;    
   5:     
   6:     public void setRectangle(Rectangle rectangle) {
   7:         this.rectangle = rectangle;
   8:     }
   9:     
  10:     protected Object clone () 
  11:                       throws CloneNotSupportedException { 
  12:         Object o = super.clone(); 
  13:         if(this.rectangle != null) { 
  14:             ((SquareTable) o).rectangle = (Rectangle) rectangle.clone();
  15:         }
  16:         
  17:         return o; 
  18:     } 
  19:  
  20:     public void draw() { 
  21:         System.out.print("\t方桌\t位置:(" + rectangle.getX() 
  22:                             + ", " + rectangle.getY()+ ")");
  23:         System.out.println(" / 宽高:(" + 
  24:                          rectangle.getWidth() 
  25:                 + ", " + rectangle.getHeight()+ ")");
  26:     }
  27: }
House是个虚拟的房屋物件,从Prototype复制出来的物件加入至House中:
  • House.java
   1: import java.util.*;
   2:  
   3: public class House { 
   4:     private Vector vector;
   5:  
   6:     public House() { 
   7:         vector = new Vector(); 
   8:     }
   9:  
  10:     public void addFurniture(AbstractFurniture furniture) { 
  11:         vector.addElement(furniture); 
  12:         
  13:         System.out.println("现有家具....");
  14:         
  15:         Enumeration enumeration = vector.elements();
  16:         while(enumeration.hasMoreElements()) { 
  17:              AbstractFurniture f = 
  18:                  (AbstractFurniture) enumeration.nextElement(); 
  19:              f.draw(); 
  20:         } 
  21:         System.out.println(); 
  22:     } 
  23: }
再来是应用程式本身:
  • Application.java
   1: import java.awt.*;
   2:  
   3: public class Application {
   4:     private AbstractFurniture circleTablePrototype;
   5:     
   6:     public void setCircleTablePrototype(
   7:                    AbstractFurniture circleTablePrototype) {
   8:         this.circleTablePrototype = circleTablePrototype;
   9:     }
  10:     
  11:     public void runAppExample() throws Exception {
  12:         House house = new House(); 
  13:         CircleTable circleTable = null;
  14:  
  15:         // 从工具列选择一个家具加入房子中
  16:         circleTable =
  17:             (CircleTable) circleTablePrototype.clone();
  18:         circleTable.setCenter(new Point(10, 10));
  19:         house.addFurniture(circleTable); 
  20:         
  21:         // 从工具列选择一个家具加入房子中
  22:         circleTable = 
  23:             (CircleTable) circleTablePrototype.clone();
  24:         circleTable.setCenter(new Point(20, 30));
  25:         house.addFurniture(circleTable); 
  26:     }
  27:     
  28:     public static void main(String[] args) throws Exception {
  29:         Application application = new Application();
  30:         application.setCircleTablePrototype(
  31:                             new CircleTable());
  32:         application.runAppExample();
  33:     }
  34: }
Java中的clone()方法是继承自Object,AbstractFurniture的子类别则override这个clone()方法,以复制其本身并传回。
下图为Prototype模式的类别结构图:

Prototype

在 Gof 的设计模式书中给出一个原型模式的应用:一个通用的图型编辑器 Framework。在这个 Framework中有一个工具列,您可以在上面选择音乐符号以加入乐谱中,并可以随时调整音乐符号的位置等等。
图型编辑器Framework是通用的,然而它并不知道这些音乐符号的型态,有人或许会想到继承图型编辑器Framework来为每个音乐符号设计一个框 架子类别,但由于音乐符号的种类很多,这会产生相当多的子类别,为了避免这种情况,可以透过Prototype模式来减少子类别的数目,可以设计出以下的 结构:

Prototype

依照这个结构,图型编辑器的Framework可以独立于要套用的特定类别之外,虽然不知道被复制传回的对象型态是什么,但总可以按照 Graphics所定义的介面来操作这些物件。

Edit by Atlas

Time 2013/7/8 14:41

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值