当前位置: 首页 > news >正文

设计模式——原型模式(Prototype Pattern)

文章目录

  • 一、原型模式定义
  • 二、例子
    • 2.1 利用Cloneable克隆接口实现的。
      • 2.1.1 定义可复制自身的User类
      • 2.1.2 定义原型工厂PrototypeFactory
      • 2.1.3 使用
    • 2.2 JDK源码——ArrayList
  • 三、其他设计模式

一、原型模式定义

类型: 创建型模式
目的: 用于创建重复的对象,同时又能保证性能。

二、例子

2.1 利用Cloneable克隆接口实现的。

2.1.1 定义可复制自身的User类

public class User implements Cloneable {
    protected String id;
    protected String name;

    public User(String id, String name) {
        this.id = id;
        this.name = name;
    }
    
    public Object clone() {
        Object clone = null;
        try {
            clone = super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return clone;
    }
}

2.1.2 定义原型工厂PrototypeFactory

public class PrototypeFactory{

    private static User prototypeUser;

    public static User setPrototypeUser(User prototypeUser){
        prototypeUser = prototypeUser;
    }
    public static User getPrototypeUser(){
        if(prototypeUser == null){
            prototypeUser = new User("0","xxxx");
        }
        return (User) prototypeUser.clone();
    }
}

2.1.3 使用

public static void main(String[] args) throws Exception{

	PrototypeFactory.setPrototypeUser(new User("root","admin"));

	User prototypeUser1 =  PrototypeFactory.getPrototypeUser();
	User prototypeUser2 =  PrototypeFactory.getPrototypeUser();
	User prototypeUser3 =  PrototypeFactory.getPrototypeUser();
}

除了clone,还可以使用反序列化和拷贝工具实现原型模式。

2.2 JDK源码——ArrayList

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
	    public Object clone() {
        try {
            ArrayList<?> v = (ArrayList<?>) super.clone();
            v.elementData = Arrays.copyOf(elementData, size);
            v.modCount = 0;
            return v;
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError(e);
        }
    }
}

三、其他设计模式

创建型模式
结构型模式

  • 1、设计模式——装饰器模式(Decorator Pattern)+ Spring相关源码

行为型模式

  • 1、设计模式——访问者模式(Visitor Pattern)+ Spring相关源码
  • 2、设计模式——中介者模式(Mediator Pattern)+ JDK相关源码
  • 3、设计模式——策略模式(Strategy Pattern)+ Spring相关源码
  • 4、设计模式——状态模式(State Pattern)
  • 5、设计模式——命令模式(Command Pattern)+ Spring相关源码
  • 6、设计模式——观察者模式(Observer Pattern)+ Spring相关源码
  • 7、设计模式——备忘录模式(Memento Pattern)
  • 8、设计模式——模板方法模式(Template Pattern)+ Spring相关源码
  • 9、设计模式——迭代器模式(Iterator Pattern)+ Spring相关源码
  • 10、设计模式——责任链模式(Chain of Responsibility Pattern)+ Spring相关源码
  • 11、设计模式——解释器模式(Interpreter Pattern)+ Spring相关源码

相关文章:

  • 合肥中科深谷嵌入式项目实战——基于ARM语音识别的智能家居系统(二)
  • Leetcode刷题详解—— 图像渲染
  • Vatee万腾的数字化掌舵:Vatee科技引领未来的新高度
  • 【python】Django——templates模板、静态文件、django模板语法、请求和响应
  • python实现炒股自动化,个人账户无门槛量化交易的开始
  • Elasticsearch 外部词库文件更新
  • 依赖倒转原则接口隔离原则迪米特法则合成复用原则
  • MongoDB索引覆盖查询
  • AUTOSAR汽车电子嵌入式编程精讲300篇-面向车载CAN网络的路由和ECU刷写方法
  • openGauss学习笔记-123 openGauss 数据库管理-设置账本数据库-账本数据库概述
  • QT QSplashScreen
  • 4、创建第一个鸿蒙应用
  • [文件读取]lanproxy 文件读取 (CVE-2021-3019)
  • 33.算术运算符
  • K8S知识点(十)
  • arcgis--NoData数据处理
  • 迅为iTOPRK3588开发板系统定制(无法联网)
  • IDEA 使用Reset Current Branch to Here 进行git 版本控制,图文操作
  • uniapp发行web页面在老版本浏览器打开一片空白
  • Interactive Analysis of CNN Robustness
  • 下周或迎外贸“抢出口”高峰,跨境电商敏感货物如何便利化“登机”?
  • 南宁一学校发生伤害案件,警方通报:嫌疑人死亡,2人受伤
  • 淄博一酒店房间内被曝发现摄像头,当地警方已立案调查
  • 黑龙江省政府副秘书长许振宇,拟任正厅级领导
  • 秦洪看盘|缩量回踩,积蓄叩关能量
  • 奥古斯都时代的历史学家李维