设计模式一详解

一、观察者模式

  1. 当一个对象状态发生改变时,依赖它的对象全部会收到通知,并自动更新

  2. 场景:一个事件发生后,要执行一连串更新操作。传统的编程方式,就是在事件的代码之后直接加入处理逻辑。当更新的逻辑增多之后,代码会变得难以维护。这种方式是藕合的,侵入式的,增加新的逻辑需要修改事件主体的代码

  3. 观察者模式实现了低耦合,非侵入式的通知与更新机制

<?php
​
interface watcher{
    public function update();
}
​
abstract class watch{
    private $watchArr = array();
    public function addWatchArr(watcher $watcher){
        $this->watchArr[] = $watcher;
    }
    public function notify(){
        foreach($this->watchArr as $value){
            $value->update();
        }
    }
}
​
class watcher1 implements watcher{
    public function update(){
        echo "update watcher1";
    }
}
​
class watcher2 implements watcher{
    public function update(){
        echo "update watcher2";
    }
}
​
​
class main extends watch{
    public function test(){
        echo "update1";
        $watcher1 = new watcher1();
        $watcher2 = new watcher2();
        $this->addWatchArr($watcher1);
        $this->addWatchArr($watcher2);
        $this->notify();
​
    }
}
​
$main = new main();
$main->test();

上面的interface{} 为每个实例提供了统一的方法,那么抽象方法提供了添加实例以及统一遍历实例的方法。

二、适配器模式

  1. 适配器模式,可以将截然不同的函数接口封装成统一的API

  2. 实际应用举例,PHP 的数据库操作有mysql,mysqli 等,可以通过适配器模式统一成一致

interface database{
    public function connect();
    public function query();
​
    public function delete();
}
​
class mysql implements database{
    public function connect(){
​
    }
​
    public function query(){
​
    }
​
    public function delete(){
        
    }
}

三、策略模式

  1. 策略模式,将一组特定的行为和算法封装成类,以适应某些特定的上下文环境,这种模式就是策略模式

  2. 使用策略模式可以实现IOC,依赖倒置、控制反转

<?php

interface advertise{
    public function show();
}
​
​
class man implements advertise{ 
    public function show(){
        echo "显示man";
    }
}
​
​
class woman implements advertise{
    public function show(){
        echo "显示woman";
    }
}
​
​
class showindex{
    private $advertise;
    public function addObject(advertise $advertise){
        $this->advertise = $advertise;
    }
    public function index(){
        $this->advertise->show();
    }
}
​
​
$showindex = new showindex();
$women = new woman();
$showindex->addObject($women);
$showindex->index();

四、装饰器模式

  1. 装饰器模式,可以动态地添加修改类的功能

  2. 一个类提供了一项功能,如果修改并添加额外的功能,传统的编程模式,需要写一个子类继承它,并重新实现类的方法

  3. 使用装饰器模式,仅需在运行时添加一个装饰器对象即可实现,可以实现最大的灵活性

interface drawInterface{
    public function beforeDraw();
    public function afterDraw();
}
​
class Draw{
​
    private $addDraw = array();
    public function addDrwa(drawInterface $draw){
        $this->addDraw[] = $draw;
    }
    private function beforeDraw(){
        foreach($this->addDraw as $row){
            $row->beforeDraw();
        }
    }
​
    private function afterDraw(){
        $t = array_reverse($this->addDraw);
        foreach($t as $row){
            $row->afterDraw();
        }
    }
    public function show(){
        $this->beforeDraw();
        echo "*";
        $this->afterDraw();
    }
}
​
​
class A implements drawInterface{
    public function beforeDraw(){
        echo "<h1>";
    }
    public function afterDraw(){
        echo "</h1>";
    }
}
​
class B implements drawInterface{
    public function beforeDraw(){
        echo "<h2>";
    }
    public function afterDraw(){
        echo "</h2>";
    } 
}
​
$draw = new Draw();
$draw->addDrwa(new A());
$draw->addDrwa(new B());
$draw->show();

相关推荐

  1. 设计模式详解

    2024-03-29 07:46:01       37 阅读
  2. 设计模式详解(十)——组合模式

    2024-03-29 07:46:01       55 阅读
  3. Python设计模式详解

    2024-03-29 07:46:01       48 阅读
  4. 设计模式】原型模式详解

    2024-03-29 07:46:01       41 阅读
  5. 设计模式详解---模板方法模式

    2024-03-29 07:46:01       42 阅读
  6. Python设计模式()

    2024-03-29 07:46:01       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-29 07:46:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-29 07:46:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-29 07:46:01       82 阅读
  4. Python语言-面向对象

    2024-03-29 07:46:01       91 阅读

热门阅读

  1. 每天一个数据分析题(二百三十七)

    2024-03-29 07:46:01       41 阅读
  2. admin端

    admin端

    2024-03-29 07:46:01      32 阅读
  3. 滑动模块-力扣-下一个排列

    2024-03-29 07:46:01       47 阅读
  4. 汇编开关atapi设备

    2024-03-29 07:46:01       40 阅读
  5. 实时数据库测试-汇编小程序

    2024-03-29 07:46:01       34 阅读
  6. openEuler 22.03 安装 .NET 8.0

    2024-03-29 07:46:01       40 阅读
  7. kotlin使用validation注解无效

    2024-03-29 07:46:01       42 阅读
  8. python中对象赋值、引用与拷贝的理解与应用

    2024-03-29 07:46:01       44 阅读
  9. 护网行动HW中的蓝队和红队的区别和关系?

    2024-03-29 07:46:01       42 阅读
  10. YTU 3379

    2024-03-29 07:46:01       40 阅读
  11. 在Vue中创建生产和开发环境

    2024-03-29 07:46:01       46 阅读