java原型模式_Java原型模式

原型模式指在创建重复对象的同时保持性能。 这种类型的设计模式属于创建模式,因为此模式提供了创建对象的最佳方法之一。

这个模式涉及实现一个原型接口,它只创建当前对象的克隆。有时直接创建对象时使用这种模式是昂贵的。例如,在昂贵的数据库操作之后创建对象。因此我们可以缓存对象,在下一个请求时返回其克隆,并在需要时更新数据库,从而减少数据库调用。

实现实例

我们将创建一个抽象类Shape和扩展Shape类的具体类。 在下一步中定义ShapeCache类,在Hashtable中存储形状(Shape)对象,并在请求时返回其克隆。

PrototypPatternDemo这是一个演示类,将使用ShapeCache类来获取一个Shape对象。实现结构图如下所示 -

154918ae14aa89e0f7357440804e1879.png

第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 shapeMap = new Hashtable();

public static Shape getShape(String shapeId) {

Shape cachedShape = shapeMap.get(shapeId);

return (Shape) cachedShape.clone();

}

// for each shape run database query and create shape

// shapeMap.put(shapeKey, shape);

// for example, we are adding three shapes

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中的形状(shape)的克隆。

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

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值