栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

设计模式之原型模式(Java实现)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

设计模式之原型模式(Java实现)

Prototype(原型) 一、意图

用原型实例指定创建对象的种类,并且通过复制这些原型创建新的对象。

二、结构

原型模式的结构如图7-28所示。

其中:
• Prototype声明一个复制自身的接口。
• CoiicretePrototype实现一个复制自身的操作。
• Client让一个原型复制自身从而创建一个新的对象。

三、适用性

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

四、实现

我们将创建一个抽象类 Shape 和扩展了 Shape 类的实体类。下一步是定义类 ShapeCache,该类把 shape 对象存储在一个 Hashtable 中,并在请求的时候返回它们的克隆。
PrototypePatternDemo 类使用 ShapeCache 类来获取 Shape 对象。

首先创建一个实现了 Cloneable 接口的抽象类。

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;
	   }
}

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

class Rectangle extends Shape {
	   public Rectangle(){
	     type = "Rectangle";
	   }	 
	   @Override
	   public void draw() {
	      System.out.println("矩形被画出来了!");
	   }
}

 class Square extends Shape {
	   public Square(){
	     type = "Square";
	   }	 
	   @Override
	   public void draw() {
	      System.out.println("正方形被画出来了!");
	   }
}

class Circle extends Shape {	 
	   public Circle(){
	     type = "Circle";
	   }	 
	   @Override
	   public void draw() {
	      System.out.println("圆被画出来了!");
	   }
}

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

class ShapeCache {  
	   private static Hashtable shapeMap 
	      = new Hashtable();	 
	   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);
	   }
}

最后 PrototypePatternDemo 使用 ShapeCache 类来获取存储在 Hashtable 中的形状的克隆。

public class Prototype {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ShapeCache.loadCache();	 
	    Shape clonedShape = (Shape) ShapeCache.getShape("1");
	    System.out.println(clonedShape.getType() + "被克隆出来了!");        
	    clonedShape.draw();
	    Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
	    System.out.println(clonedShape2.getType() + "被克隆出来了!");        
	    clonedShape2.draw();
	    Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
	    System.out.println(clonedShape3.getType() + "被克隆出来了!");
	    clonedShape3.draw();
	}

}

运行结果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/956975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号