享元模式,“享元”这两个字在中文里其实并无什么特殊的意思,因此咱们要把它拆分来看。“享”就是共享,“元”就是元素,这样一来彷佛就很容易理解了,共享某些元素嘛。php
GoF定义:运用共享技术有效地支持大量细粒度的对象git
GoF类图github
代码实现设计模式
interface Flyweight { public function operation($extrinsicState) : void; } class ConcreteFlyweight implements Flyweight { private $intrinsicState = 101; function operation($extrinsicState) : void { echo '共享享元对象' . ($extrinsicState + $this->intrinsicState) . PHP_EOL; } } class UnsharedConcreteFlyweight implements Flyweight { private $allState = 1000; public function operation($extrinsicState) : void { echo '非共享享元对象:' . ($extrinsicState + $this->allState) . PHP_EOL; } }
定义共享接口以及它的实现,注意这里有两个实现,ConcreteFlyweigh进行状态的共享,UnsharedConcreteFlyweight不共享或者说他的状态不须要去共享数组
class FlyweightFactory { private $flyweights = []; public function getFlyweight($key) : Flyweight { if (!array_key_exists($key, $this->flyweights)) { $this->flyweights[$key] = new ConcreteFlyweight(); } return $this->flyweights[$key]; } }
保存那些须要共享的对象,作为一个工厂来建立须要的共享对象,保证相同的键值下只会有惟一的对象,节省相同对象建立的开销微信
$factory = new FlyweightFactory(); $extrinsicState = 100; $flA = $factory->getFlyweight('a'); $flA->operation(--$extrinsicState); $flB = $factory->getFlyweight('b'); $flB->operation(--$extrinsicState); $flC = $factory->getFlyweight('c'); $flC->operation(--$extrinsicState); $flD = new UnsharedConcreteFlyweight(); $flD->operation(--$extrinsicState);
客户端的调用,让外部状态$extrinsicState可以在各个对象之间共享闭包
仍是说到科技以换壳为本这件事上。毕竟,你们都仍是喜欢各类颜色的手机来彰显本身的个性。以前说过,若是每种颜色咱们都要作一条生产线的话那岂不是一项巨大的投入。还好,每一个型号咱们的工厂(享元工厂)只生产最基本的背景壳(对象),而后经过专门的印刷线(状态变化)来进行上色不就好啦!嗯,下一款Iphone迟早也会模仿咱们的,看来咱们得先把各类金、各类土豪色集齐才行,说不定还能召唤神龙呢!!学习
完整代码:https://github.com/zhangyue0503/designpatterns-php/blob/master/13.flyweights/source/flyweights.php优化
果真不出意外的咱们仍是来发短信,这回的短信依然使用的阿里云和极光短信来进行发送,不过此次咱们使用享元模式来实现,这里的享元工厂咱们保存了两种不一样类型的对象哦,经过内外状态来让它们变幻无穷吧!this
短信发送类图
<?php interface Message { public function send(User $user); } class AliYunMessage implements Message { private $template; public function __construct($template) { $this->template = $template; } public function send(User $user) { echo '使用阿里云短信向' . $user->GetName() . '发送:'; echo $this->template->GetTemplate(), PHP_EOL; } } class JiGuangMessage implements Message { private $template; public function __construct($template) { $this->template = $template; } public function send(User $user) { echo '使用极光短信向' . $user->GetName() . '发送:'; echo $this->template->GetTemplate(), PHP_EOL; } } class MessageFactory { private $messages = []; public function GetMessage(Template $template, $type = 'ali') { $key = md5($template->GetTemplate() . $type); if (!key_exists($key, $this->messages)) { if ($type == 'ali') { $this->messages[$key] = new AliYunMessage($template); } else { $this->messages[$key] = new JiGuangMessage($template); } } return $this->messages[$key]; } public function GetMessageCount() { echo count($this->messages); } } class User { public $name; public function GetName() { return $this->name; } } class Template { public $template; public function GetTemplate() { return $this->template; } } // 内部状态 $t1 = new Template(); $t1->template = '模板1,不错哟!'; $t2 = new Template(); $t2->template = '模板2,还好啦!'; // 外部状态 $u1 = new User(); $u1->name = '张三'; $u2 = new User(); $u2->name = '李四'; $u3 = new User(); $u3->name = '王五'; $u4 = new User(); $u4->name = '赵六'; $u5 = new User(); $u5->name = '田七'; // 享元工厂 $factory = new MessageFactory(); // 阿里云发送 $m1 = $factory->GetMessage($t1); $m1->send($u1); $m2 = $factory->GetMessage($t1); $m2->send($u2); echo $factory->GetMessageCount(), PHP_EOL; // 1 $m3 = $factory->GetMessage($t2); $m3->send($u2); $m4 = $factory->GetMessage($t2); $m4->send($u3); echo $factory->GetMessageCount(), PHP_EOL; // 2 $m5 = $factory->GetMessage($t1); $m5->send($u4); $m6 = $factory->GetMessage($t2); $m6->send($u5); echo $factory->GetMessageCount(), PHP_EOL; // 2 // 加入极光 $m1 = $factory->GetMessage($t1, 'jg'); $m1->send($u1); $m2 = $factory->GetMessage($t1); $m2->send($u2); echo $factory->GetMessageCount(), PHP_EOL; // 3 $m3 = $factory->GetMessage($t2); $m3->send($u2); $m4 = $factory->GetMessage($t2, 'jg'); $m4->send($u3); echo $factory->GetMessageCount(), PHP_EOL; // 4 $m5 = $factory->GetMessage($t1, 'jg'); $m5->send($u4); $m6 = $factory->GetMessage($t2, 'jg'); $m6->send($u5); echo $factory->GetMessageCount(), PHP_EOL; // 4
说明
关注公众号:【硬核项目经理】获取最新文章
添加微信/QQ好友:【xiaoyuezigonggong/149844827】免费得PHP、项目管理学习资料
知乎、公众号、抖音、头条搜索【硬核项目经理】
B站ID:482780532