java设计模式之五(原型模式)

什么是原型模式?

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

当我们程序中有几个相似但又不太一样的对象时,就可以使用原型模式。简单一些说的话,就是通过深拷贝的方法用原型实例指定创建对象的种类

Java 自带的原型模式基于内存二进制流的复制,在性能上比直接 new 一个对象更加优良。

图示

我们可以举个例子,比如现在有三个形状对象,圆形(Circle),正方形(Square),长方形(Rectangle)。现在我们想要通过原型模式创建这三个对象,那就需要有一个原型实例 :形状(Shape),让它实现java的接口类Cloneable,并且重写克隆方法clone()。
在这里插入图片描述
然后我们定义一个类 ShapeCache,该类把 shape 对象存储在一个 Hashtable 中,并在请求的时候返回它们的克隆。最后通过PrototypePatternDemo 类使用 ShapeCache 类来获取 Shape 对象。

实现

步骤1

//定义原型类,实现java接口
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 Object clone(){
        Object clone = null;
        try{
            clone = super.clone();
        } catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        return clone;
    }

}

步骤2
实现具体对象 圆形(Circle),正方形(Square),长方形(Rectangle)

public class Circle extends Shape{

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

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

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

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

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

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

步骤3
通过调用原型对象克隆方法,创建指定对象

import java.util.Hashtable;

public class ShapeCache {
    private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>();

    public static Shape getShape(String shapeId){
        Shape shapeCache = shapeMap.get(shapeId);
        return (Shape)shapeCache.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);

    }
}

步骤4
main()方法检查结果

public class PrototypePattenDemo {
    public static void main(String[] args) {

        ShapeCache.loadCache();

        Shape cloneShape1 = (Shape)ShapeCache.getShape("1");
        System.out.println("Shape: "+ cloneShape1.getType());
        System.out.println("Shape: "+ cloneShape1.getId());
        cloneShape1.draw();
        System.out.println("------------------------------");

        Shape cloneShape2 = (Shape)ShapeCache.getShape("2");
        System.out.println("Shape: "+ cloneShape2.getType());
        System.out.println("Shape: "+ cloneShape2.getId());
        cloneShape2.draw();
        System.out.println("------------------------------");

        Shape cloneShape3 = (Shape)ShapeCache.getShape("3");
        System.out.println("Shape: "+ cloneShape3.getType());
        System.out.println("Shape: "+ cloneShape3.getId());
        cloneShape3.draw();
        System.out.println("------------------------------");

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木小同

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值