当前位置:   article > 正文

简谈设计模式之原型模式

简谈设计模式之原型模式

原型模式是一种创建型设计模式, 用于创建对象, 而不必指定它们所属的具体类. 它通过复制现有对象 (即原型) 来创建新对象. 原型模式适用于当创建新对象的过程代价较高或复杂时, 通过克隆现有对象来提高性能

原型模式结构
  • 原型接口. 声明一个克隆自身的接口
  • 具体原型. 实现克隆的具体方法
  • 客户端. 通过调用具体原型中的克隆方法来创建新对象
原型模式实现
// 原型接口
interface Prototype {
    Prototype clone();
}

// 具体原型类
class ConcretePrototype implements Prototype {
    private String field;
    
    public ConcretePrototype(String field) {
        this.field = field;
    }
    
    // 实现克隆方法
    @Override
    public Prototype clone() {
        return new ConcretePrototype(this.field);
    }
    
    @Override
    public String toString() {
        return "ConcretePrototype{" + "field = " + field + '\'' + '}';
    }
}

// 客户端
public class Client {
    public static void main(String[] args) {
        // 创建原型对象
        ConcretePrototype prototype = new ConcretePrototype("prototype");
        
        // 克隆原型对象
        ConcretePrototype clone = (ConcretePrototype) prototype.clone();
        
        System.out.println(prototype);
        System.out.println(clone);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

在原型模式中, 克隆可以分为浅拷贝和深拷贝

  • 浅拷贝: 复制对象时, 只复制对象本身的基本数据类型字段和对象的引用, 而不复制引用对象本身
  • 深拷贝: 复制对象时, 不仅复制对象本身的基本数据类型字段和对象的引用, 还复制所有引用对象, 递归地进行复制

下面是深拷贝和浅拷贝的实例

浅拷贝:

import java.util.ArrayList;
import java.util.List;

// 具体原型类
class ConcretePrototype implements Prototype {
    private List<String> list;

    public ConcretePrototype() {
        this.list = new ArrayList<>();
    }

    public void addItem(String item) {
        this.list.add(item);
    }

    @Override
    public Prototype clone() {
        try {
            return (ConcretePrototype) super.clone(); // 浅拷贝
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public String toString() {
        return "ConcretePrototype{" + "list=" + list + '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

深拷贝:

import java.util.ArrayList;
import java.util.List;

// 具体原型类
class ConcretePrototype implements Prototype {
    private List<String> list;

    public ConcretePrototype() {
        this.list = new ArrayList<>();
    }

    public void addItem(String item) {
        this.list.add(item);
    }

    @Override
    public Prototype clone() {
        ConcretePrototype clone = new ConcretePrototype();
        clone.list = new ArrayList<>(this.list); // 深拷贝
        return clone;
    }

    @Override
    public String toString() {
        return "ConcretePrototype{" + "list=" + list + '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/828112
推荐阅读
相关标签
  

闽ICP备14008679号