学习总结录 学习总结录
首页
归档
分类
标签
  • Java基础
  • Java集合
  • MySQL
  • Redis
  • JVM
  • 多线程
  • 计算机网络
  • 操作系统
  • Spring
  • Kafka
  • Elasticsearch
  • Python
  • 面试专题
  • 案例实践
  • 工具使用
  • 项目搭建
  • 服务治理
  • ORM框架
  • 分布式组件
  • MiniSpring
  • 设计模式
  • 算法思想
  • 编码规范
友链
关于
GitHub (opens new window)
首页
归档
分类
标签
  • Java基础
  • Java集合
  • MySQL
  • Redis
  • JVM
  • 多线程
  • 计算机网络
  • 操作系统
  • Spring
  • Kafka
  • Elasticsearch
  • Python
  • 面试专题
  • 案例实践
  • 工具使用
  • 项目搭建
  • 服务治理
  • ORM框架
  • 分布式组件
  • MiniSpring
  • 设计模式
  • 算法思想
  • 编码规范
友链
关于
GitHub (opens new window)
  • 设计模式

    • 设计模式-工厂模式
    • 设计模式-抽象工厂模式
    • 设计模式-单例模式
    • 设计模式-建造者模式
    • 设计模式-原型模式
      • 设计模式-原型模式
      • 浅克隆和深克隆
      • 克隆实现方案
        • 通用方法
        • Clone()方法和Cloneable接口
      • 案例
      • 参考
    • 设计模式-适配器模式
    • 设计模式-桥接模式
    • 设计模式-组合模式
    • 设计模式-外观模式
    • 设计模式-装饰模式
    • 设计模式-享元模式
    • 设计模式-代理模式
    • 设计模式-职责链模式
    • 设计模式-命令模式
    • 设计模式-迭代器模式
    • 设计模式-中介者模式
    • 设计模式-观察者模式
    • 设计模式-状态模式
    • 设计模式-策略模式
  • 算法思想

  • 编码规范

  • 技术思想
  • 设计模式
旭日
2023-03-27
目录

设计模式-原型模式

# 设计模式-原型模式

原型模式:使用原型实例指定待创建对象的类型,并且通过复制这个原型来创建新的对象。

由于在软件系统中经常会遇到需要创建多个相同或者相似对象的情况,因此原型模式在软件开发工具中具有较高的效率。

使用环境:

  • 创建新对象成本较大。
  • 系统要保存对象的状态,而对象的状态变化很少。

优点:

  • 当创建新对象实例较为复杂时,使用原型模式可以简化对象的创建过程。通过复制一个已有实例可以提供新实例的创建效率。
  • 扩展性好,原型模式中提供了抽象原型类,可以针对抽象原型类进行编程。

缺点:

  • 需要为每一个类配备一个克隆方法。
  • 实现深克隆需要编写较为复杂的代码。

# 浅克隆和深克隆

浅拷贝:

当原型对象被复制时,只复制它本身和其中包含的值类型的成员变量,而引用类型的成员变量并没有复制。

  • 值类型:int、double、byte等基本数据类型
  • 引用类型:类、接口、数组

深拷贝:

当原型对象被复制时,除了本身被复制外,对象所包含的所有成员变量也将被复制。

# 克隆实现方案

# 通用方法

抽象类:

public abstract class Prototype {

    /**
     * 通用克隆
     * @return
     */
    @Override
    public abstract Prototype clone();
}

普通类:

public class ConcretePrototype extends Prototype{

    private String str;

    public void setStr(String str) {
        this.str = str;
    }

    public String getStr() {
        return this.str;
    }


    @Override
    public Prototype clone() {
        ConcretePrototype concretePrototype = new ConcretePrototype();
        concretePrototype.setStr(this.str);
        return concretePrototype;
    }
}

# Clone()方法和Cloneable接口

public class ClonePrototype implements Cloneable{

    private String str;

    public void setStr(String str) {
        this.str = str;
    }

    public String getStr() {
        return this.str;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

# 案例

形状抽象类:

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

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

形状类:

public class Rectangle extends Shape {

    Rectangle() {
        this.type = "Rectangle";
    }

    @Override
    void draw() {
        System.out.println("长方形的draw()方法");
    }
}

public class Square extends Shape {

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

    @Override
    void draw() {
        System.out.println("正方形的draw()方法");
    }
    
}

public class Circle extends Shape{

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

    @Override
    public void draw() {
        System.out.println("圈的draw()方法");
    }

}

形状的缓存:

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

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

案例:

public class PrototypePatternDemo {
    public static void main(String[] args) {
        ShapeCache.loadCache();

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

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

        Shape clonedShape3 = ShapeCache.getShape("3");
        System.out.println("Shape : " + clonedShape3.getType());
        clonedShape3.draw();
    }
}


Shape : Circle
圈的draw()方法
Shape : Square
正方形的draw()方法
Shape : Rectangle
长方形的draw()方法

# 参考

设计模式入门实践 (opens new window)

#设计模式
上次更新: 2024/06/29, 15:13:44
设计模式-建造者模式
设计模式-适配器模式

← 设计模式-建造者模式 设计模式-适配器模式→

最近更新
01
基础概念
10-31
02
Pytorch
10-30
03
Numpy
10-30
更多文章>
Theme by Vdoing | Copyright © 2021-2024 旭日 | 蜀ICP备2021000788号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式