网站设计模式 专题
您的位置:web > 网站设计模式专题 > 原型模式
原型模式
作者:--    发布时间:2019-11-20

原型模式(prototype pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。

介绍

意图:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

主要解决:在运行期建立和删除原型。

何时使用: 1、当一个系统应该独立于它的产品创建,构成和表示时。 2、当要实例化的类是在运行时刻指定时,例如,通过动态装载。 3、为了避免创建一个与产品类层次平行的工厂类层次时。 4、当一个类的实例只能有几个不同状态组合中的一种时。建立相应数目的原型并克隆它们可能比每次用合适的状态手工实例化该类更方便一些。

如何解决:利用已有的一个原型对象,快速地生成和原型对象一样的实例。

关键代码: 1、实现克隆操作,在 java 继承 cloneable,重写 clone(),在 .net 中可以使用 object 类的 memberwiseclone() 方法来实现对象的浅拷贝或通过序列化的方式来实现深拷贝。 2、原型模式同样用于隔离类对象的使用者和具体类型(易变类)之间的耦合关系,它同样要求这些"易变类"拥有稳定的接口。

应用实例: 1、细胞分裂。 2、java 中的 object clone() 方法。

优点: 1、性能提高。 2、逃避构造函数的约束。

缺点: 1、配备克隆方法需要对类的功能进行通盘考虑,这对于全新的类不是很难,但对于已有的类不一定很容易,特别当一个类引用不支持串行化的间接对象,或者引用含有循环结构的时候。 2、必须实现 cloneable 接口。 3、逃避构造函数的约束。

使用场景: 1、资源优化场景。 2、类初始化需要消化非常多的资源,这个资源包括数据、硬件资源等。 3、性能和安全要求的场景。 4、通过 new 产生一个对象需要非常繁琐的数据准备或访问权限,则可以使用原型模式。 5、一个对象多个修改者的场景。 6、一个对象需要提供给其他对象访问,而且各个调用者可能都需要修改其值时,可以考虑使用原型模式拷贝多个对象供调用者使用。 7、在实际项目中,原型模式很少单独出现,一般是和工厂方法模式一起出现,通过 clone 的方法创建一个对象,然后由工厂方法提供给调用者。原型模式已经与 java 融为浑然一体,大家可以随手拿来使用。

注意事项:与通过对一个类进行实例化来构造新对象不同的是,原型模式是通过拷贝一个现有对象生成新对象的。浅拷贝实现 cloneable,重写,深拷贝是通过实现 serializable 读取二进制流。

实现

我们将创建一个抽象类 shape 和扩展了 shape 类的实体类。下一步是定义类 shapecache,该类把 shape 对象存储在一个 hashtable 中,并在请求的时候返回它们的克隆。

prototyppatterndemo,我们的演示类使用 shapecache 类来获取 shape 对象。

原型模式的 uml 图

步骤 1

创建一个实现了 clonable 接口的抽象类。

shape.java

public abstract class shape implements cloneable {
   
   private string id;
   protected string type;
   
   abstract void draw();
   
   public string gettype(){
      return type;
   }
   
   public string getid() {
      return id;
   }
   
   public void setid(string id) {
      this.id = id;
   }
   
   public object clone() {
      object clone = null;
      try {
         clone = super.clone();
      } catch (clonenotsupportedexception e) {
         e.printstacktrace();
      }
      return clone;
   }
}

步骤 2

创建扩展了上面抽象类的实体类。

rectangle.java

public class rectangle extends shape {

   public rectangle(){
     type = "rectangle";
   }

   @override
   public void draw() {
      system.out.println("inside rectangle::draw() method.");
   }
}

square.java

public class square extends shape {

   public square(){
     type = "square";
   }

   @override
   public void draw() {
      system.out.println("inside square::draw() method.");
   }
}

circle.java

public class circle extends shape {

   public circle(){
     type = "circle";
   }

   @override
   public void draw() {
      system.out.println("inside circle::draw() method.");
   }
}

步骤 3

创建一个类,从数据库获取实体类,并把它们存储在一个 hashtable 中。

shapecache.java

import java.util.hashtable;

public class shapecache {
   
   private static hashtable<string, shape> shapemap 
      = new hashtable<string, shape>();

   public static shape getshape(string shapeid) {
      shape cachedshape = shapemap.get(shapeid);
      return (shape) cachedshape.clone();
   }

   // 对每种形状都运行数据库查询,并创建该形状
   // shapemap.put(shapekey, shape);
   // 例如,我们要添加三种形状
   public static void loadcache() {
      circle circle = new circle();
      circle.setid("1");
      shapemap.put(circle.getid(),circle);

      square square = new square();
      square.setid("2");
      shapemap.put(square.getid(),square);

      rectangle rectangle = new rectangle();
      rectangle.setid("3");
      shapemap.put(rectangle.getid(),rectangle);
   }
}

步骤 4

prototypepatterndemo 使用 shapecache 类来获取存储在 hashtable 中的形状的克隆。

prototypepatterndemo.java

public class prototypepatterndemo {
   public static void main(string[] args) {
      shapecache.loadcache();

      shape clonedshape = (shape) shapecache.getshape("1");
      system.out.println("shape : " + clonedshape.gettype());       

      shape clonedshape2 = (shape) shapecache.getshape("2");
      system.out.println("shape : " + clonedshape2.gettype());        

      shape clonedshape3 = (shape) shapecache.getshape("3");
      system.out.println("shape : " + clonedshape3.gettype());        
   }
}

步骤 5

验证输出。

shape : circle
shape : square
shape : rectangle
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册