重剑无锋


  • Home

  • Archives

门面模式

Posted on 2017-11-30

0x00 门面模式

外观模式.

定义

Provide a unified interface to a set of interface in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.(要求一个子系统的外部与内部的通信必须通过一个统一的对象进行.)

角色

  • Facade 门面角色, 该角色知晓所有功能和责任,委托类.
  • Subsystem 子系统角色, 子系统并不知道门面类的存在.

优点

减少系统的相互依赖

缺点

不符合开闭原则.

应用场景

适用于:

  • 为一个复杂的模块或子系统提供一个供外界访问的接口.
  • 子系统相对独立.
  • 预防低水平人员带来的风险扩散.

php实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

// 子系统
class SubsystemOne {
public function dosthFir()
{
echo 'subsystem class one.';
}
}
class SubsystemTwo {
public function dosthSec()
{
echo 'subsystem class two.';
}
}
class SubsystemThr {
public function dosthThr()
{
echo 'subsystem class three.';
}
}

// 门面类
class Facade {
private $objOne;
private $objTwo;
private $objThree;
private $context;

public function __construct()
{
$this->objOne = new SubsystemOne();
$this->objTwo = new SubsystemTwo();
$this->objThr = new SubsystemThr();
$this->context = new Context();
}

public function jobFir()
{
$this->objOne->dosthFir();
}
public function jobSec()
{
$this->objTwo->dosthSec();
}
public function jobThr()
{
$this->context->combineJob();
}
}

// 封装类, 不要直接参与子系统内的业务逻辑
class Context {
private $objOne;
private $objThr;

public function __construct()
{
$this->objOne = new SubsystemOne();
$this->objThr = new SubsystemThr();
}

public function combineJob()
{
$this->objOne->doSthFir();
$this->objThr->doSthThr();
}
}

0x01 小结

门面不参与子系统内的业务逻辑.Laravel 框架就用了不少 Facade.

设计模式-观察者模式

Posted on 2017-11-29

0x00 观察者模式

也可称发布订阅模式(Publish/Subscribe).

定义

Define a one-to-many dependency between objets so that when one object changes state, all its dependents are notified and updated automatically.(定义对象间一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都会得到通知并自动更新.)

Read more »

设计模式-组合模式

Posted on 2017-11-28

0x00 组合模式

组合模式,即合成模式.

定义

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.(将抽象对象组合成树形结构以表示"部分-整天"的层次结构,使得用户对单个对象和组合对象的使用具有一致性.)

关键角色

  • Component 抽象构件, 定义参加组合对象的共有属性和方法, 可以定义一些默认的属性或行为.
  • Leaf 叶子构件, 叶子对象,无剩余分支, 属于遍历的最小单位.
  • Composite 树枝构件, 树枝对象,组合树枝节点和叶子节点形成一个树形结构.

优点

  • 高层模块调用简单, 一棵树形机构中的所有节点都是 Component, 局部和整体对调用者来说没有区别。即高层模块无需关心自己处理的是单个对象还是整个组合结构,简化了高层模块的代码。
  • 节点自由增加
    如果想增加一个树枝节点,树叶节点,只要找到它的父节点即可。容易扩展。

缺点

树叶和树枝直接使用了实现类,限制了接口的使用范围.

使用场景

  • 维护和展示”部分-整体”关系的场景,诸如树形菜单,文件和文件夹管理.
  • 从一个整体中能够独立出部分模块或功能的场景.

php实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php

// 抽象构件
abstract class Component {
public function doSth() {}
}

// 树枝构件
class Composite extends Component {
private $compArr = [];
public function add(Component $_component)
{
array_push($this->compArr, $_component);
}

public function remove(Component $_component)
{
$key = array_search($_component, $this->compArr);
if (!$key !== false) {
array_splice($this->compArr, $key, 1);
}
}

public function getChildren()
{
return $this->compArr;
}
}

// 叶子构件
class Leaf extends Component {
public function doSth()
{}
}

class Client {
public static function main()
{
$root = new Composite();
$root->doSth();
$branch = new Composite();
$root->add($branch);
$leaf = new Leaf();
$branch->add($leaf);
}
public static function showTree(Composite $root)
{
}
}

0x 小结

不管你在不在, 设计方式就在那里……

设计模式-迭代器模式

Posted on 2017-11-27

0x00 迭代器模式

迭代器类似于一个数据库中(MySQL的游标是单向的)的游标,可以在一个容器内翻滚,遍历所有的查看的元素.

定义

Provide a way to access the elements of an arrgegate object sequentially without exposing its underlying representation.(它提供一种方法访问容器对象中的各个元素,而又不需暴露对象的内部细节.)

Read more »

设计模式-适配器模式

Posted on 2017-11-26

0x00 适配器模式

适配器模式,也叫变压器模式, 包装模式(Wrapper)。 装饰者模式也是包装模式的一种。电流传输过程中使用的高压电流,输送到千里之外的家家户户前,并不能直接使用,否则直接烧坏了电器。这就需要进行降压,使用适配器将高压电流转变成家用电器适用的 220v(中国) 电流。

定义

Convert the interface of a class into another interface clients expect. Apdapter lets classes work togetherthat couldn’t otherwise because of incompatible interfaces.(将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。)

Read more »

设计模式-策略模式

Posted on 2017-11-24

0x00 策略模式

策略模式,也叫政策模式, 使用的是面向对象的继承和多态机制。

定义

Define a amily of algorithms, encapsulate each one, and make them interchangeable. (定义一组算法,将每个算法都封装起来,并且使它们之间可以交换。)

Read more »

设计模式-装饰者模式

Posted on 2017-11-23

0x00 装饰者模式

定义

Attach additional responsibilities to an object dynamically keeping the same interface. Decorators provide a flexible alternative to subclassing for extending functionality.(动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。)

Read more »

设计模式-责任链模式

Posted on 2017-11-22

0x00 责任链模式

定义

Avoid coupling the sender of a request to its rceiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.(使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止。)

Read more »

设计模式-命令模式

Posted on 2017-11-21

0x00 命令模式

定义

命令模式是一个高内聚的模式,其定义为: Encapsulate a request as an object, there by letting you parameterize clients with different requests, queue or log requests, and support undoable oerations.
(将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求队列或者记录请求日志,可以提供命令的撤销和回复功能。)

通用类

包含三个角色:

  • Receive 接受这角色, 命令传递到这里被执行。
  • Command 命令角色, 声明需要执行的所有命令。
  • Invoker 调用者角色, 接受到命令,并执行命令。

php 实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/*
command pattern
*/

// 通用的 Receiver 类
abstract class Receiver {
abstract function dosth();
}

// 具体的 Receiver 类
class ConcreteReceiver1 extends Receiver {
public function doSth() {
}
}

class ConcreteReceiver2 extends Receiver {
public function doSth() {

}
}

// 抽象的Command 类
abstract class Command {
abstract function execute();
}

// 具体 Command 类
class ConcreteCommand1 extends Command {
private $receiver;

public function ConcreteCommand1(Receiver $_receiver) {
$this->receiver = $_receiver;
}

public function execute() {
$this->receiver->doSth();
}
}

class ConcreteCommand2 extends Command {
private $receiver;

public function ConcreteCommand2(Receiver $_receiver) {
$this->receiver = $_receiver;
}

public function execute() {
$this->receiver->doSth();
}
}

// 调用者 Invoker 类
class Invoker {
private $command;

public function setCommand(Command $_command) {
$this->command = $_command;
}
public function act() {
$this->command->execute();
}
}

// 场景调用
class client {
public static function main() {
$invoker = new Invoker();
$receiver = new ConcreteReceiver1();
$command = new ConcreteCommand1($receiver);
$invoker->setCommand($command);
$invoker->act();
}
}

0x01 小结

切勿好高骛远, 即使你的征途是星辰大海, 也要脚踏实地做好准备工作。

中介者模式

Posted on 2017-11-12

0x00 中介者模式

现实生活中像神舟飞船的指挥中心, 机场的调度中心, MVC 框架中的 C(controller), 媒体网关, 房租中介等等, 都有着中介者的影子.中介者模式还叫 调停者模式 .

定义

Define an boject that encapsulates how a set of objects nteract. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.(用一个中介对象封装一系列的对象交互,中介者是各对象不需要显示地相互作用,从而使其耦合松散,而且可以独立地改变它们之间的交互.)

构成

  • Mediator 抽象中介者角色, 抽象中介者角色定义统一的接口,用于各同事角色之间的通信.
  • Concrete Mediator 具体中介者角色, 具体中介者角色通过协调各同事角色实现协作行为,因此它必须依赖于同事角色.
  • Colleague 同事角色, 每个同事角色与其他的同事角色的通信, 必须经过”和事老”中介者协作.每个同事类的行为分为两种:
    1. 自发行为: 同事本身的行为, 如改变对象自身的状态, 处理自己的行为等不需要依赖中介者的动作.
    2. 依赖方法: 依赖中介者才能完成的行为.

优点

中介者模式减少了类之间的依赖,同事类只依赖于中介者, 将一对多的依赖变成一对一的依赖,减少依赖的同时也降低了类之间的耦合.

缺点

随着逻辑复杂度增加,原本 N 个对象直接的相互依赖关系转嫁给中介者和同事类的依赖关系,中介者的逻辑会逐渐复杂,并且变得越来越庞大臃肿.

使用场景

多个对象之间紧密耦合(在类图中出现了蜘蛛网状结构)的情况下,用中介者是好的选择.
话说那些什么经纪人什么秘书的是不是中介者模式?

php 实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

// 通用抽象中介者
abstract class Mediator {
protected $colleague;

public function getColleague() {
return $this->colleague;
}

public function setColleague(ConcreteColleague $conColl) {
$this->colleague = $conColl;
}

abstract public function doSth();
}

// 通用中介者
class ConcreteMediator extends Mediator {
public function dosth() {}
}

// 抽象同事类
abstract class Colleague {
protected $mediator;

public function Colleague(Mediator $_mediator) {
$this->mediator = $_mediator;
}
}

// 具体同事类
class ConcreteColleague extends Colleague {
public function ConcreteColleague(Mediator $_mediator) {
parent::setColleague($_mediator);
}

// 自发行为
public function selfMethod() {}

// 依赖方法
public function relyMethod() {
parent::doSth(); // 委托中介处理
}
}
?>

小结

字面上中介和代理多少带着模糊的雷同,所以中介和代理的区别是?

1…3456

Gitvim

55 posts
18 tags
RSS
© 2019 Gitvim