博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Design Pattern的万剑归宗 => Mediator
阅读量:7187 次
发布时间:2019-06-29

本文共 4835 字,大约阅读时间需要 16 分钟。

Overview

今天看了YouTube上的一个讲Design Pattern的视频,把这个视频的大意给大家分享一下,该视频的作者是Anthony Ferrara。

大意就是作者把22种Design Pattern不断的重组归纳抽象直道最后抽象为一种设计模式,Mediator。
而所有的Design Pattern关注的核心问题就是如何控制信息流(但是我个人认为核心是如何解耦)。再根据信息流划分出对象在系统中担任的5种角色,Representer, Doer, Dispatcher, Translator, Maker。

大概就是以上的内容,但是具体如何实践还不太清楚。。。


演变过程

Re-grouped

GoF Design Pattern分类 v.s 作者分类

Creational Structural Behavioral
Shim Abstract Factory
Object Pool
Prototype
Flyweight
Iterator
Null Object
Compositional Builder
Adapter
Composite
Decorator
Facade
Proxy
Interpreter
Mediator
Observer
Decompositional Factory Method Bridge
Composite
Proxy
Chain of Responsibility
Command
Mediator
Memento
Observer
Strategy
Template Method

如表格所示,GoF把26种设计模式分为了Creational, structural和Behavioral三大类。

而作者把设计模式按照Shim, Compsitional, Decompsitional分类

  • Shim Patterns: 编程语言不能处理当前情况
    例子:iterator模式,没有其他的方法能够更方便的遍历对象的时候,就会使用iterator模式。
  • Compositional Patterns:要把一系列的object组合在一起
  • Decompositional Patterns: 要把一个object拆分成多个object

要注意的是有些模式在多个分类里, 比如Mediator既可以用于组合模式又可以用于拆分模式。

由于现有的26种模式,有些模式彼此之间很难分辨区别。

如: Adapter, Facade, Bridge, Decorator, Proxy
图片描述
如图所示,UML结构相同。

代码例子:

Adapter
要把已有的系统watchdog插入系统PSRLogger中,在log函数中,旧的接口(watchdog)被转换成了新的接口(log)。
用这个方法可以把watchdog插入任何系统。

phpuse Psr\Log\LoggerInterface as Logclass PSR3Logger implements Log {    public function log ($level, $msg, array $ctx = array())    {        $severity = $this->convertLevelToSeverity($level);        watchdog("unknown", $msg, $ctx, $severity);        }    /* ... */}

Facade

把一个复杂的系统转换成一个简单的接口。

phpclass EntityMetadataWrapper {    public function __construct($type, $data = null, $info = array())    {        $this->type = $type;        $this->info = $info + array(            "langcode" => null,        );        $this->info["type"] = ¥type;        if (isset($data)) {            $this->set($data);        }    }    /* ... */}

抽象一下过程

把已有代码用于其他代码中,而设计模式提供的就是这个转化的过程
图片描述

由于结构相同,差别比较细微,把这5种归为一种,用adapter作为代表。

同理把其他design pattern按UML相似合并,有以下表格

De-duplicated Grouping

Creational Structural Behavioral
Compositional
Adapter
Composite
Mediator
Observer
Decompositonal
Adapter
Composite
Observer

这里除去重复的只有6种pattern。

  • Adapter - This has a single class which makes one or more other classes behave as a single interface.
  • Composite - This abstracts a recursive structure.
  • Command - This abstracts determination of execution from actual execution
  • Mediator - This abstracts communication between several objects
  • Memento - This abstracts state representation from execution
  • Observer - This abstracts communication between two objects

如果按照Information Flow的传递来分,有三种

  • Controlling Information Flow Between Multiple Systems (多系统间)
  • Controlling Information Flow Within An Individual System (单系统)
  • Controlling Information Flow Between Individual Objects (object之间)

De-Duplicated Re-Groupings

下面开始继续简化

Multiple Systems? Single System? Single Objects?
Information Flow? Mediator Command Observer
Structure Adapter Composite Memento

注意:information flow 和 structure是相对的。

再简化

DeDe-Duplicated Re-Groupings

Pattern
Information Flow? Mediator
Structure Adapter

最终简化

DeDe-Duplicated ReRe-Groupings

information flow和structure其实是一种概念的不同表达形式,例如list,可以传递信息作为input,output,也可以作为一种数据结构。所以归为一种

Pattern
Information Flow? Mediator

核心

所有的Design Pattern的职责都是控制information flow。

然而故事到这里并没有完。

下面关于information flow还有logic(变量)和message(即input和output,可以是string,array,object等等)的区别

Information Flow

Logic Hybrid Message
Purpose Behavior General Code State
State Stateless Stateful Stateful
Paradigm Functional OOP? Data

Information Flow传递方式

信息流的传递方式可以归纳为三种:Ask(类似get方法), Tell(类似set方法), Translate(input=>output).

Ask

$msg = $obj->something();

Logic Hybrid Message
No Yes Yes

Tell

$obj->something($msg);

Logic Hybrid Message
No Yes Yes

Translate

$msg2 = $obj->do($msg1);

Logic Hybrid Message
Yes Yes No

Note:

  • ask总是有状态
  • tell总是有状态
  • translate可以是有状态也可以是无状态的

Object Role Patterns

按Object角色role来分,一共5种:Representer, Doer, Dispatcher, Translator, Maker

Representer

phpclass User {    public function getName(); //ask    public function isAdmin();    public function setName($name); //tell}

Doer

phpclass EmailSystem {    /**    * @return boolean    */    public function send(Message $msg); //tell}

Dispatcher

phpclass Controller {    /**    * @return Response    */    public function handle(Request $req); //translate}

Translator

phpclass UserView {    /**    * @return string HTML    */    public function render(User $user);//translate}

Maker

phpclass UserFactory {    /**    * @return User a user object    */    public function getUser();//ask}
State? Logic? Mode
Representer User No Ask/Tell
Doer No Yes Tell
Dispatcher System No Translate
Translator No Yes Translate
Maker System Yes Ask

作者的开发经验得出有95%~99%的应用可以用以上模式表达。

文章还会继续修改,有兴趣的同学可以直接观看YouTube上的视频,我在reference里面给出了链接,还有作者的blog,但是blog中只有前半部分内容。

我个人对于design pattern的理解也很有限,希望各位大神赐教。另外关于一些单词的具体含义,我只能意会不能言传。。。如果有大神愿意仔细解读的话,可以留言。
同步更新在我的gitbook笔记中。

Reference

转载地址:http://doykm.baihongyu.com/

你可能感兴趣的文章
.NET接入UnionPay银联支付(一)手机wap支付
查看>>
Java多线程-工具篇-BlockingQueue
查看>>
js中动态创建json,动态为json添加属性、属性值的实例
查看>>
使用Qemu运行Ubuntu文件系统(1)
查看>>
红黑树
查看>>
2018年10月小结(流水账) -- 1024程序员节快乐
查看>>
SpringBoot(八)配置logback日志
查看>>
单点登录 之 OAuth
查看>>
『流畅的Python』第15章:上下文管理器和else块
查看>>
windows环境下面批量新建文件夹
查看>>
MS CRM 2011 如何获得当前用户使用的界面语言
查看>>
敏捷个人俱乐部(北京)线下活动 开始报名了!
查看>>
IPMSG飞鸽传书——编译源代码的方法
查看>>
80后的我们
查看>>
C语言文件操作解析(四)
查看>>
Windows Azure Cloud Service (25) Windows Azure 新的增强功能
查看>>
MVC开发人员必须拥有的五大工具
查看>>
[New Portal]Windows Azure Virtual Machine (10) 自定义Windows Azure Virtual Machine模板
查看>>
在Web应用程序中执行计划任务(多线程)
查看>>
HDU 3999 The order of a Tree (先序遍历)
查看>>