Java设计模式——创建型模式:原型模式(克隆模式)

模式定义

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

这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。

模式介绍

  • 动机:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。
  • 优点:1、性能提高。 2、逃避构造函数的约束。
  • 缺点: 1、配备克隆方法需要对类的功能进行通盘考虑,这对于全新的类不是很难,但对于已有的类不一定很容易,特别当一个类引用不支持串行化的间接对象,或者引用含有循环结构的时候。 2、必须实现 Cloneable 接口。3.clone 方法位于类的内部,当对已有类进行改造的时候,需要修改代码,违背了开闭原则。

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

代码实现

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

PrototypePatternDemo 类使用 ShapeCache 类来获取 Shape 对象。

在这里插入图片描述

//抽象Shape
public abstract class Shape implements Cloneable{
    
    

    private String id;
    protected String type;

    abstract void draw();

    public String getId() {
    
    
        return id;
    }

    public void setId(String id) {
    
    
        this.id = id;
    }

    public String getType() {
    
    
        return type;
    }

    public void setType(String type) {
    
    
        this.type = type;
    }

    public Object clone(){
    
    
        Object clone=null;
        try {
    
    
            clone=super.clone();
        }catch (CloneNotSupportedException  e){
    
    
            e.printStackTrace();
        }
        return clone;
    }

}

//具体shape
public class Square extends Shape {
    
    

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

    @Override
    public void draw() {
    
    
        System.out.println("Inside Square::draw() method.");
    }
}

public class Rectangle extends Shape {
    
    

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

    @Override
    public void draw() {
    
    
        System.out.println("Inside Rectangle::draw() method.");
    }
}

public class Circle extends Shape {
    
    

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

    @Override
    public void draw() {
    
    
        System.out.println("Inside Circle::draw() method.");
    }
}

//ShapeCache缓存
public class ShapeCache {
    
    

    private static Hashtable<String, Shape> shapeMap
            = new Hashtable<>();

    public static Shape getShape(String shapeId){
    
    
        Shape cachedShape=shapeMap.get(shapeId);
        return (Shape)cachedShape.clone();
    }

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

//Client
public class Client {
    
    

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

}

上述代码将具体shape都继承了抽象Shape类,在ShapeCache使用loadCache实例化具体Shape类,并存入到ShapeMap中,在需要使用时,从客户端访问ShapeCache然后加载相应的具体Shape类。

猜你喜欢

转载自blog.csdn.net/qq_35531985/article/details/109150939