首页 > 其他分享 >创建型设计模式-原型 Prototype

创建型设计模式-原型 Prototype

时间:2023-09-06 12:36:58浏览次数:30  
标签:设计模式 Yay clone component been circularReference p1 原型 Prototype

简介

原型模式支持按照一个对象为模板,创建出另一个一模一样的对象。

简单说就是把 A 对象的属性,都赋值到 B 上,注意必须是深拷贝,即 clone 后的 AB 关联的对象是不同的对象。

角色

  • 抽象原型类

    定义 clone 方法

  • 具体实现类

    实现 clone 方法

类图

类图

代码

class Prototype
{
    public $primitive;
    public $component;
    public $circularReference;

    public function __clone()
    {
        $this->component = clone $this->component;

        $this->circularReference = clone $this->circularReference;
        $this->circularReference->prototype = $this;
    }
}

class ComponentWithBackReference
{
    public $prototype;

    public function __construct(Prototype $prototype)
    {
        $this->prototype = $prototype;
    }
}

function clientCode()
{
    $p1 = new Prototype();
    $p1->primitive = 245;
    $p1->component = new \DateTime();
    $p1->circularReference = new ComponentWithBackReference($p1);

    $p2 = clone $p1;
    if ($p1->primitive === $p2->primitive) {
        echo "Primitive field values have been carried over to a clone. Yay!\n";
    } else {
        echo "Primitive field values have not been copied. Booo!\n";
    }
    if ($p1->component === $p2->component) {
        echo "Simple component has not been cloned. Booo!\n";
    } else {
        echo "Simple component has been cloned. Yay!\n";
    }

    if ($p1->circularReference === $p2->circularReference) {
        echo "Component with back reference has not been cloned. Booo!\n";
    } else {
        echo "Component with back reference has been cloned. Yay!\n";
    }

    if ($p1->circularReference->prototype === $p2->circularReference->prototype) {
        echo "Component with back reference is linked to original object. Booo!\n";
    } else {
        echo "Component with back reference is linked to the clone. Yay!\n";
    }
}

clientCode();

output:

Primitive field values have been carried over to a clone. Yay!
Simple component has been cloned. Yay!
Component with back reference has been cloned. Yay!
Component with back reference is linked to the clone. Yay!

本文由mdnice多平台发布

标签:设计模式,Yay,clone,component,been,circularReference,p1,原型,Prototype
From: https://www.cnblogs.com/caipi/p/17681982.html

相关文章

  • 创建型设计模式-单例 Singleton
    简介全局共用一个对象,好处是对象只实例化一次,减少资源占用角色单例类返回本类的唯一一个实例代码classSingleton{privatestatic$instances=[];protectedfunction__construct(){}protectedfunction__clone(){}publicfunction__wa......
  • 学习笔记-设计模式-创建型模式-工厂模式
    工厂模式工厂模式是一种创建者设计模式,细分之下可以分成三类简单工厂模式,工厂方法模式和抽象工厂模式。简单工厂模式最简单的工厂模式,它采用静态方法的方式来决定应该应该生产什么商品。publicclassStoreFactory{publicstaticICommoditygetCommodityService(Integ......
  • 【23种设计模式】桥接模式(七)
    前言【桥接模式】是【结构型】设计模式的第二个模式,也有叫【桥模式】的,英文名称:BridgePattern。大家第一次看到这个名称会想到什么呢?我第一次看到这个模式根据名称猜肯定是连接什么东西的。因为桥在我们现实生活中经常是连接着A地和B地,再往后来发展,桥引申为一种纽带,比如:丝绸之......
  • 单例设计模式
    单例设计模式基本介绍类的单例设计模式,就是采取一定的方式保证在整个的软件系统中,对某个类只能有一个对象实例存在,且类提供一个静态方法,用以获取该对象。例如Hibernate的SessionFactory,它是sql会话工厂,这个对象一定是很重的(创建需要加载很多资源和时间),一般情况下,只需要一个Se......
  • 设计模式- 单例模式
    单例模式是一种创建型设计模式,可以保证一个类型有且只有一个实例存在单例模式的适用于什么场景当一个类在程序运行期间只需要一个实例的时候,就可以考虑将其做成单例模式例如一些全局的配置,用来储存程序运行期间全局的共享配置,或者可以做一个简单的消息管道,程序中所有......
  • 设计模式备忘录+命令模式实现Word撤销恢复操作
    文章目录前言思路代码实现uml类图总结前言最近学习设计模式行为型的模式,学到了备忘录模式提到这个模式可以记录一个对象的状态属性值,用于下次复用,于是便想到了我们在Windows系统上使用的撤销操作,于是便想着使用这个模式进行一次模仿复现思路以下是按照备忘录和命令模式结合的思路......
  • 设计模式行为模式-访问者模式
    文章目录介绍基本原理1.定义访问者模式2.角色及其职责2.1元素(Element)2.2具体元素(ConcreteElement)2.3访问者(Visitor)2.4具体访问者(ConcreteVisitor)2.5对象结构(ObjectStructure)3.访问者模式的工作流程适用场景1.对象结构稳定,但需要经常增加新的操作2.处理复杂的对象结构,并......
  • 设计模式-原型模式详解
    文章目录前言理论基础1.原型模式定义2.原型模式角色3.原型模式工作过程4.原型模式的优缺点实战应用1.原型模式适用场景2.原型模式实现步骤3.原型模式与单例模式的区别原型模式的变体1.带有原型管理器的原型模式2.懒汉式单例模式的原型模式实现3.细粒度原型模式总结前言......
  • 设计模式-装饰模式
    文章目录一、简介二、基本概念三、装饰模式的结构和实现类图解析:装饰器的实现方式继承实现:组合实现:继承和组合对比四、装饰模式的应用场景五、与其他模式的关系六、总结一、简介装饰模式是一种结构型设计模式,它允许动态地向对象添加额外的功能。二、基本概念装饰模式定义:在不改变......
  • 设计模式-迭代器
    文章目录1.引言1.1概述1.2设计模式1.3迭代器模式的应用场景1.4迭代器模式的作用2.基本概念2.1迭代器Iterator2.2聚合Aggregate2.3具体聚合ConcreteAggregate3.Java实现迭代器模式3.1Java集合框架3.2Java迭代器接口3.3Java迭代器模式实现示例4.迭代器模式的优......