你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

理解23种设计模式(php)

2021/12/21 19:41:13

面向对象设计的目标:高内聚,低耦合;设计模式应该遵从该目标;

tips:以下都是示例代码,实际业务需求要复杂得多,仅作为入门学习

1、迭代器

特点: 对类的属性进行遍历,优点(隐藏遍历逻辑)

// 接口
<?php

namespace App\Service;

interface CommonServiceInterface
{
    public function hasNext();
    public function next();
    public function add($user);
    public function delete($pointer);
}

// 实现类
<?php

namespace App\Service;

class CommonService implements CommonServiceInterface
{
    private $set = [];
    private $pointer = 0;
    public function hasNext()
    {
        if (isset($this->set[$this->pointer])) {

            return true;
        } else {

            return false;
        }
    }

    public function next()
    {
        return $this->set[$this->pointer++];
    }

    public function add($user)
    {
        // 存在则不能添加
        if (in_array($user,$this->set)) {

            throw new \Exception('已存在');
        }
        array_push($this->set,$user);
    }

    public function delete($user)
    {
        $key = array_search($user,$this->set);

        if ($key) {

            unset($this->set[$key]);
            // 处理指针
            if ($this->pointer < $key) {
                $this->pointer --;
            }

            return true;
        } else {
            return false;
        }

    }
}

 // 测试
$commonService = new CommonService();
  $commonService->add(4);
  $commonService->add(2);
  $commonService->add(3);

  while($commonService->hasNext()) {
      echo $commonService->next();
  }

// 结果
在这里插入图片描述

2、适配器
特点:根据不同的类型,选择执行对应的类型的成员方法 (适配不同的类型)

// 接口
<?php

namespace App\Service;

interface CommonServiceInterface
{
    public function v220();
    public function v110();
}

// 实现
<?php

namespace App\Service;

class CommonService implements CommonServiceInterface
{

    public function v220()
    {
       echo 'this is 220 voltage';
    }

    public function v110()
    {
        echo 'this is 110 voltage';
    }
}

// 测试适配器 假设电压是220伏
$v = 220;

 $commonService = new CommonService();

 $commonService->{'v'.$v}();

// 结果
在这里插入图片描述
3、普通工厂

特点:不包含抽象类

// 接口
<?php

namespace App\Service;

interface CommonServiceInterface
{
    public function make();
    public function test();
}

// 实现
<?php

namespace App\Service;

class CommonService implements CommonServiceInterface
{
    public function make()
    {
        echo '制造一台设备'.PHP_EOL;
    }

    public function test()
    {
        echo '设备测试正常'.PHP_EOL;
    }

    public function out()
    {
        echo '设备发往山东'.PHP_EOL;
    }
}

// 测试普通工厂
        $commonService = new CommonService();
        $commonService->make();
        $commonService->test();
        $commonService->out();

// 结果:
在这里插入图片描述

4、抽象工厂
特点:工厂是抽象类,包含抽象方法,功能相比普通工厂要复杂,需要实现抽象方法

// 接口
<?php

namespace App\Service;

interface CommonServiceInterface
{
    public function make();
    public function test();
    public function out();
}

// 抽象工厂类,实现工厂接口
<?php

namespace App\Service;

abstract class CommonServiceAbstract implements CommonServiceInterface
{
    protected $makeout;

    abstract public function toMake();

    public function make()
    {
        $this->toMake();
        echo $this->makeout;
    }


    public function test()
    {
        echo '设备测试正常'.PHP_EOL;
    }

    public function out()
    {
        echo '设备发往山东'.PHP_EOL;
    }
}

实现抽象工厂内的抽象方法
<?php

namespace App\Service;

class CommonService extends CommonServiceAbstract
{
    public function toMake()
    {
        $this->makeout = "制造出一台汽车\n";
    }
}

// 测试抽象工厂
        $commonService = new CommonService();
        $commonService->make();
        $commonService->test();
        $commonService->out();

结果
在这里插入图片描述
5、单例设计模式
特点:定义很简单,就是类只实例化一次的设计模式,节省服务器资源

// 单例类,通过静态属性$instance持久化保存类的实例
<?php

namespace App\Service;

class CommonService
{
    private static $instance;

    public static function instance()
    {
          if (!self::$instance) {

              self::$instance = new self();
          }

         return self::$instance;
    }

    public function hello()
    {
        echo "你好啊\n";
    }
}

// 测试单例
        $instance = CommonService::instance();
        $instance->hello();

        $instance = CommonService::instance();
        $instance->hello();

// 结果 只有第一次进行了实例化,第二次不再进行实例化了

在这里插入图片描述