记录PHP常用几种设计模式

时间:2020-06-15 16:09:00   收藏:0   阅读:58

什么是设计模式

每一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心。这样,你就能一次又一次地使用该方案而不必做重复劳动。

主要设计模式

创建型

结构型

行为型

单例模式

保证一个类仅有一个实例,并提供一个访问它的全局访问点。让类自身负责保存它的唯一实例,并提供一个访问该实例的方法。这就是单例模式

适用性

优点

例子:

<?php
  
/**
* Singleton of Database
*/
class Database
{
  // We need a static private variable to store a Database instance.
  privatestatic $instance;
  
  // Mark as private to prevent it from being instanced.
  private function__construct()
  {
    // Do nothing.
  }
  
  private function__clone() 
  {
    // Do nothing.
  }
  
  public static function getInstance() 
  {
    if (!(self::$instance instanceof self)) {
      self::$instance = new self();
    }
  
    return self::$instance;
  }
}
  
$a =Database::getInstance();
$b =Database::getInstance();
  
// true
var_dump($a === $b);

抽象工厂模式

提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类

“工厂”是创建产品(对象)的地方,其目的是将产品的创建与产品的使用分离。抽象工厂模式的目的,是将若干抽象产品的接口与不同主题产品的具体实现分离开。这样就能在增加新的具体工厂的时候,不用修改引用抽象工厂的客户端代码

适用性

优点

缺点

工厂方法模式

定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类

适用性

例子:

<?php
      
    interface InterfaceShape
    {
     function getArea();
     function getCircumference();
    }
      
    /**
    * 矩形
    */
    class Rectangle implements InterfaceShape
    {
      private $width;
      private $height;
       
      public function __construct($width, $height)
      {
        $this->width = $width;
        $this->height = $height;
      }
      
      public function getArea()
      {
        return $this->width* $this->height;
      }
      
      public function getCircumference()
      {
        return 2 * $this->width + 2 * $this->height;
      }
    }
      
    /**
    * 圆形
    */
    class Circle implements InterfaceShape
    {
      private $radius;
      
      function __construct($radius)
      {
        $this->radius = $radius;
      }
      
      
      public function getArea()
      {
        return M_PI * pow($this->radius, 2);
      }
      
      public function getCircumference()
      {
        return 2 * M_PI * $this->radius;
      }
    }
      
    /**
    * 形状工厂类
    */
    class FactoryShape
    {
      public static function create()
      {
        switch (func_num_args()) {
          case1:
          return newCircle(func_get_arg(0));
          case2:
          return newRectangle(func_get_arg(0), func_get_arg(1));
          default:
            # code...
            break;
        }
      }
    }
      
    $rect =FactoryShape::create(5, 5);
    // object(Rectangle)#1 (2) { ["width":"Rectangle":private]=> int(5) ["height":"Rectangle":private]=> int(5) }
    var_dump($rect);
    echo "<br>";
      
    // object(Circle)#2 (1) { ["radius":"Circle":private]=> int(4) }
    $circle =FactoryShape::create(4);
    var_dump($circle);

适配器模式

将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作

适用性

例子:

//老的代码     
     
    class User {    
     
        private $name;    
     
        function __construct($name) {    
     
            $this->name = $name;    
     
        }    
     
        public function getName() {    
     
            return $this->name;    
     
        }    
     
    }   
    //新代码,开放平台标准接口    
     
    interface UserInterface {    
     
        function getUserName();    
     
    }    
     
    class UserInfo implements UserInterface {    
     
        protected $user;    
     
        function __construct($user) {    
     
            $this->user = $user;    
     
        }    
     
        public function getUserName() {    
     
            return $this->user->getName();    
     
        }    
     
    }   
    $olduser = new User(‘张三‘);    
     
    echo $olduser->getName()."n";    
     
    $newuser = new UserInfo($olduser);    
     
    echo $newuser->getUserName()."n";

 

观察者模式

定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新

适用性

例子:

<?php
      
    /*
    观察者接口
    */
    interface InterfaceObserver
    {
      function onListen($sender, $args);
      function getObserverName();
    }
      
    // 可被观察者接口
    interface InterfaceObservable
    {
      function addObserver($observer);
      function removeObserver($observer_name);
    }
      
    // 观察者抽象类
    abstract class Observer implements InterfaceObserver
    {
      protected $observer_name;
      
      function getObserverName()
      {
        return $this->observer_name;
      }
      
      function onListen($sender, $args)
      {
      
      }
    }
      
    // 可被观察类
    abstract class Observable implements InterfaceObservable
    {
      protected $observers = array();
      
      public function addObserver($observer)
      {
        if ($observerinstanceofInterfaceObserver)
        {
          $this->observers[] = $observer;
        }
      }
      
      public function removeObserver($observer_name)
      {
        foreach ($this->observersas $index => $observer)
        {
          if ($observer->getObserverName() === $observer_name)
          {
            array_splice($this->observers, $index, 1);
            return;
          }
        }
      }
    }
      
    // 模拟一个可以被观察的类
    class A extends Observable
    {
      public function addListener($listener)
      {
        foreach ($this->observersas $observer)
        {
          $observer->onListen($this, $listener);
        }
      }
    }
      
    // 模拟一个观察者类
    class B extends Observer
    {
      protected $observer_name = ‘B‘;
      
      public function onListen($sender, $args)
      {
        var_dump($sender);
        echo "<br>";
        var_dump($args);
        echo "<br>";
      }
    }
      
    // 模拟另外一个观察者类
    class C extends Observer
    {
      protected $observer_name = ‘C‘;
      
      public function onListen($sender, $args)
      {
        var_dump($sender);
        echo "<br>";
        var_dump($args);
        echo "<br>";
      }
    }
      
    $a = new A();
    // 注入观察者
    $a->addObserver(new B());
    $a->addObserver(new C());
      
    // 可以看到观察到的信息
    $a->addListener(‘D‘);
      
    // 移除观察者
    $a->removeObserver(‘B‘);
      
    // 打印的信息:
    // object(A)#1 (1) { ["observers":protected]=> array(2) { [0]=> object(B)#2 (1) { ["observer_name":protected]=> string(1) "B" } [1]=> object(C)#3 (1) { ["observer_name":protected]=> string(1) "C" } } }
    // string(1) "D"
    // object(A)#1 (1) { ["observers":protected]=> array(2) { [0]=> object(B)#2 (1) { ["observer_name":protected]=> string(1) "B" } [1]=> object(C)#3 (1) { ["observer_name":protected]=> string(1) "C" } } }
    // string(1) "D"

 

策略模式

定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化

适用性

例子:

MaleUserStrategy.php

<?php
     
    namespace IMooc;
     
     
    interface UserStrategy {
        function showAd();
        function showCategory();
    }

MaleUserStrategy.php

<?php
     
    namespace IMooc;
     
     
    class MaleUserStrategy implements UserStrategy  {
     
        function showAd()
        {
            echo "IPhone6";
        }
     
        function showCategory()
        {
            echo "电子产品";
        }
    }

FemaleUserStrategy.php

<?php
     
    namespace IMooc;
     
    class FemaleUserStrategy implements UserStrategy {
        function showAd()
        {
            echo "2014新款女装";
        }
        function showCategory()
        {
            echo "女装";
        }
    }
<?php
    interface FlyBehavior{
        public function fly();
    }
     
    class FlyWithWings implements FlyBehavior{
        public function fly(){
            echo "Fly With Wings \n";
        }
    }
     
    class FlyWithNo implements FlyBehavior{
        public function fly(){
            echo "Fly With No Wings \n";
        }
    }
    class Duck{
        private $_flyBehavior;
        public function performFly(){
            $this->_flyBehavior->fly();
        }
     
        public function setFlyBehavior(FlyBehavior $behavior){
            $this->_flyBehavior = $behavior;
        }
    }
     
    class RubberDuck extends Duck{
    }
    // Test Case
    $duck = new RubberDuck();
     
    /*  想让鸭子用翅膀飞行 */
    $duck->setFlyBehavior(new FlyWithWings());
    $duck->performFly();            
     
    /*  想让鸭子不用翅膀飞行 */
    $duck->setFlyBehavior(new FlyWithNo());
    $duck->performFly();

参考:

https://www.cnblogs.com/lxwphp/p/10484661.html

https://github.com/colinlet/PHP-Interview-QA/blob/master/docs/09.%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/QA.md

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!