php 和设计模式 - 模板方法模式
发表于|更新于|设计模式
|总字数:110|阅读时长:1分钟|浏览量:
这个模式是对继承的最好诠释。当子类有重复动作时,将其重复动作放入父类统一处理,这就是模板方法最简单通俗的解释。
1 | abstract class BaseController |
这个模式太简单了,就不多说了。
文章作者: m-finder
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 M-finder!
相关推荐

2021-03-21
php 和设计模式 - 享元模式
享元模式会尽量使相似的对象共享内存,能让你在有限的内存中载入更多对象。 当一个应用程序需要创建大量对象,并且这些对象的大多数状都可变为外部状态时,就很适合享元模式。 一如既往的举个🌰: 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889interface Message{ public function send(User $user);}class AliMessage implements Message{ // 内部状态 protected Template $template; public function __construct(Template $template) { $this->template = $template; } // user 属于外部状态 public function send(User $user) { echo 'use ', $this->template->getTemplate(), ' send msg ', 'to user ', $user->getName(), ' by ali', PHP_EOL;; }}class MessageFactory{ protected array $messages = []; public function getMessage(Template $template) { $key = md5($template->getTemplate()); if (!array_key_exists($key, $this->messages)) { echo 'message create', PHP_EOL; $this->messages[$key] = new AliMessage($template); } return $this->messages[$key]; }}class Template{ protected string $template; public function setTemplate(string $template) { $this->template = $template; } public function getTemplate(): string { return $this->template; }}class User{ protected string $name; public function setName(string $name) { $this->name = $name; } public function getName(): string { return $this->name; }}$templateA = new Template();$templateA->setTemplate('template a');$templateB = new Template();$templateB->setTemplate('template b');$userA = new User();$userA->setName('wu');$userB = new User();$userB->setName('yf');$factory = new MessageFactory();$flyweightA = $factory->getMessage($templateA);$flyweightA->send($userA);$flyweightA->send($userB);$flyweightB = $factory->getMessage($templateB);$flyweightB->send($userA); 这次来点不一样的,贴张截图帮助理解: 可以看到,在享元工厂中,一共创建了两次 message,当我们重复用一个模板发送消息时,模板作为内部状态已经被缓存了,调用的时候直接取出即可,避免了重复创建造成的资源浪费。 例子虽然不太贴切,但是看完应该也能总结出,享元模式需要依赖于一个享元工厂以及一个享元角色,也就是咱们代码中的 AliMessage 类。

2021-03-21
php 和设计模式 - 原型模式
原型模式通常用于大对象的创建。因为每次 new 一个对象会造成很大开销,原型模式仅需内存拷贝即可。 比较简单,直接上🌰: 12345678910111213141516171819202122232425262728293031interface Book{ public function setTitle(string $title); public function getTitle(): string;}class eBook implements Book{ protected string $title; public function setTitle(string $title) { $this->title = $title; } public function getTitle(): string { return $this->title; }}$book1 = new eBook();$book1->setTitle('1 号电子书');echo $book1->getTitle(), PHP_EOL;$book2 = new eBook();$book2->setTitle('2 号电子书');echo $book2->getTitle(), PHP_EOL; 常规情况下,我们会用 new 创建两个对象,然后分别设置各自的书名。现在用原型模式改造一下: 1234567$prototype = new eBook();foreach(range(1, 10) as $index){ $book = clone $prototype; $book->setTitle($index. ' 号电子书'); echo $book->getTitle(), PHP_EOL;} 类保持不变,只在创建时该用 clone 即可。

2021-03-21
php 和设计模式 - 生成器模式
生成器模式也叫建造者模式,主要用于将一个复杂对象的构造与它的表示分离。该模式允许你使用相同的代码生成不同类型和形式的对象。 什么是复杂对象呢?举个🌰,人类,都有个脑袋,有个身体,又有两条胳膊腿儿,那么,我们就可以把人看作是一个复杂对象。 那么,对于生成器模式来说,我们要把人类对象的创建与它的实例表示进行分离。 class Human { public function setHead(string $head) { echo 'head:', $head, PHP_EOL; } public function setBody(string $body) { echo 'body:', $body, PHP_EOL; } public function setArms(string $leftArm, string $rightArm) { echo 'left arm:', $leftArm, ' right arm:', $rightArm, PHP_EOL; } } interface Builder { public function buildHead(); public function buildBody(); public function buildArms(); public function getResult(): Human; } class HumanBuilder implements Builder{ private Human $human; public function __construct() { $this->human = new Human(); } public function buildHead() { $this->human->setHead('ai'); } public function buildBody() { $this->human->setBody('body'); } public function buildArms() { $this->human->setArms('left', 'right'); } public function getResult(): Human { return $this->human; } } class Director{ public function builder(Builder $builder): Human { $builder->buildHead(); $builder->buildBody(); $builder->buildArms(); return $builder->getResult(); } } $director = new Director(); $human = $director->builder(new HumanBuilder()); 好了,生成器模式到此结束。

2021-03-21
php 和设计模式 - 迭代器模式
提供一种方法顺序访问一个集合对象中的各种元素,而又不暴露该对象的内部表示。 foreach 的底层就是迭代器。很多编程语言都已经将其作为一个基础类库实现出来了,所以也就有了这个模式目前学习意义大于实际意义的说法。 在 php 中,内部已提供 Iterator 接口,可以直接使用。 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879class Bookshelf implements \Countable, \Iterator{ protected array $books = []; protected int $current = 0; public function addBook(Book $book) { $this->books[] = $book; } public function current() { return $this->books[$this->current]; } public function next() { return $this->current++; } public function key(): int { return $this->current; } public function valid(): bool { return isset($this->books[$this->current]); } public function rewind() { $this->current = 0; } public function count(): int { return count($this->books); }}class Book{ protected string $author; protected string $title; public function __construct(string $author, string $title) { $this->author = $author; $this->title = $title; } public function getAuthor(): string { return $this->author; } public function getTitle(): string { return $this->title; } public function getAuthorAndTitle(): string { return $this->getAuthor() . '-' . $this->getTitle(); }}$bookA = new Book('wu', 'php');$bookB = new Book('wu', 'redis');$bookshelf = new Bookshelf();$bookshelf->addBook($bookA);$bookshelf->addBook($bookB);foreach ($bookshelf as $book) { echo $book->getAuthorAndTitle(), PHP_EOL;} 使用起来还是比较简单的,至于如何实现就不写了。

2021-03-21
php 和设计模式
场面话工作几年,复杂的业务场景,重复的 CURD 一直在消耗着我作为程序员的激情与精力,在设计模式这方面的积累从来都不够完善,出去面试时还经常会面临面试官的灵魂拷问,总觉得自己是不是就快被淘汰了。 所以,是时候下功夫整理下这方面的知识了。 开始之前,要考虑一个问题,我们为什么要学习设计模式呢? 首先从概念来讲,设计模式作为一种描述问题及其解决方案的方法,是无数的 IT 前辈在工作中总结出的 特定场景 下的 最佳解决方案,那么当我们遇到同样的场景时,就可以通过使用模式,来实现符合自己程序的解决方案,以此降低代码的耦合度,提高代码的质量,同时也方便我们后期对程序进行调整或拓展。 第二,现在大部分 PHP 程序都是依托于框架进行开发,一般情况下,我们对于框架的使用,只是局限于在一个强大的程序基础设施上添加一些小装饰。那么学习并掌握设计模式以后,我们就能够理解框架是如何解决问题,以及框架解决问题的策略,随着开发的深入,我们也能够以设计为导向,开发出自己的可复用的代码库,这对我们来说,也是一种极大的积累和提升。 第三,对于团队来说,人来人往是常态,对于从一开始就已经接手项目的成员来说,理解程序的逻辑会很轻松,但是对于新加入的成员来说,采用标准化设计模式的程序才是更容易的理解和掌握的存在,这可以使新成员更快的参与到项目的开发工作中,发挥出他作为项目成员的作用。 第四,设计模式定义了专业词汇,通过这些词汇,开发人员之间的沟通变得更加容易,可以节省很多沟通成本。 设计模式与面向对象密切相关,因此我应该不会简单的复制一堆模式来加以理解,而是从面向对象入手,逐渐向设计模式演深。 源码 目录对象 设计原则 设计模式分类 工厂模式 单例模式 生成器模式 原型模式 门面模式 适配器模式 装饰器模式 桥接模式 代理模式 组合模式 享元模式 依赖注入模式 注册模式 流接口模式 策略模式 模板方法模式 观察者模式 迭代器模式 责任链模式 命令行模式 备忘录模式 状态模式 访问者模式 中介者模式

2021-03-21
php 和设计模式 - 装饰器模式
装饰器模式主要用于动态添加修改类的功能。 一般情况下,一个类提供了某些功能,如果要扩展或修改该类,我们可以扩展一个子类出来。但是装饰器模式可以使我们更为灵活的实现。 那么,装饰器模式相对继承灵活在哪儿呢? 举个🌰,我们有一个发送短信的类,现在要在发送短信前增加一些校验,发送短信后我们要记录 log: 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374interface SendSms{ public function Send();}interface Decorator{ public function beforeSend(); public function afterSend();}class SmsDecorator implements Decorator{ public function beforeSend() { echo 'check', PHP_EOL; } public function afterSend() { echo 'log', PHP_EOL; }}class AuthSms implements SendSms{ protected $decorators = []; public function addDecorator(Decorator $decorator) { array_push($this->decorators, $decorator); } protected function beforeSend() { /** * @var Decorator $decorator */ foreach ($this->decorators as $decorator) { $decorator->beforeSend(); } } protected function afterSend() { $decorators = array_reverse($this->decorators); /** * @var Decorator $decorator */ foreach ($decorators as $decorator) { $decorator->afterSend(); } } public function Send() { $this->beforeSend(); echo 'auth sms is send', PHP_EOL; $this->afterSend(); }}$sms = new AuthSms();$sms->addDecorator(new SmsDecorator());$sms->send();