享元模式,“享元”这两个字在中文里其实并无什么特殊的意思,因此咱们要把它拆分来看。“享”就是共享,“元”就是元素,这样一来彷佛就很容易理解了,共享某些元素嘛。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迟早也会模仿咱们的,看来咱们得先把各类金、各类土豪色集齐才行,说不定还能召唤神龙呢!!优化
完整代码:github.com/zhangyue050…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
复制代码
说明
享元模式的例子说得有点牵强,不过确实这类设计模式在咱们平常的开发中一方面用得很少,另外一方面典型的例子又太经典,反正只要记住它的特色就行了,具体在应用的时候说不许写完代码回头一看你会发现这不就是我学过的享元模式嘛!好了,下一篇咱们学习一个也是比较少用并且复杂的模式,可是也许你也能常常见到哦!组合模式