@Chiang
2019-12-12T14:30:12.000000Z
字数 4492
阅读 582
设计模式
- 原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
- 用于创建重复的对象,使用克隆技术来复制实例化的对象,新对象是复制原型实例创造来的
- 使用克隆对象来减少new对象的开销
- 原型: 是被克隆的那一个
这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当直接创建对象的代价比较大时,则采用这种模式。例如,一个对象需要在一个高代价的数据库操作之后被创建。我们可以缓存该对象,在下一个请求时返回它的克隆,在需要的时候更新数据库,以此来减少数据库调用。
- 克隆不会调用构造函数
- 使用克隆来创建新的对象的时候,并不能触发类的构造函数
- 避免构造函数的强依赖
- 原型被改变了,克隆出来的对象也会被改变
<?php
/**
* 键盘类
*
* @author Luffy (lufei@swoole.com)
* @date 2019/10/24
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Prototype;
class Keyboard
{
/**
* 键盘名称
*
* @var
*/
public $name;
}
<?php
/**
* 浅复制
*
* @author Luffy (lufei@swoole.com)
* @date 2019/10/24
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Prototype;
class ShallowCopy
{
private $keyboard;
public function __construct()
{
echo '亲爱的大哥'.PHP_EOL;
}
public function setKeyboard($keyboard)
{
$this->keyboard = $keyboard;
}
public function get()
{
echo '我想要个'.$this->keyboard->name. '的键盘' .PHP_EOL;
}
}
<?php
/**
* User:çlufei
* Date: 2019/10/24
* Email: lufei@swoole.com.
*/
require __DIR__ . '/../vendor/autoload.php';
use Luffy\DesignPatterns\Prototype\ShallowCopy;
use Luffy\DesignPatterns\Prototype\Keyboard;
use Luffy\DesignPatterns\Prototype\DeepCopy;
class Client
{
/**
* 浅复制
*/
public static function shallowCopy()
{
$shallowCopy = new ShallowCopy();
$keyboard = new Keyboard();
$keyboard->name = "ikbc";
$shallowCopy->setKeyboard($keyboard);
$shallowCopy->get();
echo "-----\n";
// clone不会调用构造函数
$cloneShallow = clone $shallowCopy;
$cloneShallow->get();
}
public static function shallowCopy2()
{
$shallowCopy = new ShallowCopy();
$keyboard = new Keyboard();
$keyboard->name = "ikbc";
$shallowCopy->setKeyboard($keyboard);
$shallowCopy->get();
echo "-----\n";
// clone不会调用构造函数
$cloneShallow = clone $shallowCopy;
$cloneShallow->get();
echo "=====\n";
$keyboard->name = "hhkb";
$shallowCopy->get();
$cloneShallow->get();
}
public static function deepCopy()
{
$keyboard = new Keyboard();
$keyboard->name = "ikbc";
$deepCopy = new DeepCopy();
$deepCopy->setKeyboard($keyboard);
$deepCopy->get();
echo "-----\n";
$cloneDeep = clone $deepCopy;
$cloneDeep->get();
echo "=====\n";
$keyboard->name = "hhkb";
$deepCopy->get();
$cloneDeep->get();
}
}
Client::shallowCopy();
echo "***************".PHP_EOL;
Client::shallowCopy2();
echo "***************".PHP_EOL;
Client::deepCopy();
- 实现了克隆的接口
<?php
/**
* 键盘类
*
* @author Luffy (lufei@swoole.com)
* @date 2019/10/24
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Prototype;
class Keyboard
{
/**
* 键盘名称
*
* @var
*/
public $name;
}
<?php
/**
* 原型模式
*
* @author Luffy (lufei@swoole.com)
* @date 2019/10/24
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Prototype;
interface Prototype
{
public function __clone();
}
<?php
/**
* 深复制
*
* @author Luffy (lufei@swoole.com)
* @date 2019/10/25
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Prototype;
class DeepCopy implements Prototype
{
private $keyboard;
public function __construct()
{
echo '美丽的姐姐'.PHP_EOL;
}
public function setKeyboard($keyboard)
{
$this->keyboard = $keyboard;
}
public function get()
{
echo '我想要个'.$this->keyboard->name. '的键盘' .PHP_EOL;
}
public function __clone()
{
$this->keyboard = clone $this->keyboard;
}
}
<?php
/**
* User:çlufei
* Date: 2019/10/24
* Email: lufei@swoole.com.
*/
require __DIR__ . '/../vendor/autoload.php';
use Luffy\DesignPatterns\Prototype\ShallowCopy;
use Luffy\DesignPatterns\Prototype\Keyboard;
use Luffy\DesignPatterns\Prototype\DeepCopy;
class Client
{
/**
* 浅复制
*/
public static function shallowCopy()
{
$shallowCopy = new ShallowCopy();
$keyboard = new Keyboard();
$keyboard->name = "ikbc";
$shallowCopy->setKeyboard($keyboard);
$shallowCopy->get();
echo "-----\n";
// clone不会调用构造函数
$cloneShallow = clone $shallowCopy;
$cloneShallow->get();
}
public static function shallowCopy2()
{
$shallowCopy = new ShallowCopy();
$keyboard = new Keyboard();
$keyboard->name = "ikbc";
$shallowCopy->setKeyboard($keyboard);
$shallowCopy->get();
echo "-----\n";
// clone不会调用构造函数
$cloneShallow = clone $shallowCopy;
$cloneShallow->get();
echo "=====\n";
$keyboard->name = "hhkb";
$shallowCopy->get();
$cloneShallow->get();
}
public static function deepCopy()
{
$keyboard = new Keyboard();
$keyboard->name = "ikbc";
$deepCopy = new DeepCopy();
$deepCopy->setKeyboard($keyboard);
$deepCopy->get();
echo "-----\n";
$cloneDeep = clone $deepCopy;
$cloneDeep->get();
echo "=====\n";
$keyboard->name = "hhkb";
$deepCopy->get();
$cloneDeep->get();
}
}
Client::shallowCopy();
echo "***************".PHP_EOL;
Client::shallowCopy2();
echo "***************".PHP_EOL;
Client::deepCopy();