设计模式之原型模式

原型模式(Prototype)

用于创建重复的对象,同时又能保证性能。将一个对象作为原型,通过对其进行复制而克隆出多个和原型类似的新实例

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

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

注意事项:

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

使用场景

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

具体实现

  1. 创建一个实现Clonable接口的抽象类

    //Shape.java:
    public abstract class Shape implements Cloneable {
    
        private String id;
        protected String type;
        
        abstract void draw();
    
        public String getType(){
           return type;
        }
    
        //id的get/set方法
    
        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:
    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();
        }
    
        // 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. 使用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());        
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值