无为清净楼资源网 Design By www.qnjia.com

1、单例模式

一个类,只能允许有一个对象存在

<"htmlcode">
interface mysql{ 
  public function connect();
}
 
class mysqli2 implements mysql{
  public function connect(){
    echo 'mysqli';
  }
}
 
class pdo2 implements mysql{
  public function connect(){
    echo 'pdo';
  }
}

class mysqlFactory{
  static public function factory($class_name){
    return new $class_name();
  }
}
$obj = mysqlFactory::factory('pdo2');
$obj->connect();

3、注册模式

注册模式,解决全局共享和交换对象。已经创建好的对象,挂在到某个全局可以使用的数组上,

在需要使用的时候,直接从该数组上获取即可。将对象注册到全局的树上。任何地方直接去访问。

<"htmlcode">
//目标角色
interface Aims
{
  public function newMethod1();
  public function newMethod2();
}
 
//需要被适配的类(Adaptee)
Class Man
{
  public function oldMethod1()
  {
    echo 'man';
  }
 
  public function oldMethod2()
  {
    echo '男人';
  }
}
 
//需要被适配的类(Adaptee)
Class Woman
{
  public function oldMethod1()
  {
    echo 'woman';
  }
 
  public function oldMethod2()
  {
    echo '女人';
  }
}
 
//适配器,
Class Adapters implements Aims
{
  private $adaptee;
  public function __construct($adaptee)
  {
    $this->adaptee = $adaptee;
  }
 
  public function newMethod1()
  {
    //以少量的代码对被适配者作出适配
    echo 'sex :';
    $this->adaptee->oldMethod1();
  }
 
  public function newMethod2()
  {
    echo 'sex name :';
    $this->adaptee->oldMethod2();
  }
}
 
$adapter1 = new Adapters(new Man);
$adapter1->newMethod1();
$adapter2 = new Adapters(new Woman);
$adapter2->newMethod2();

5、策略模式

这是一个男人和女人的问题,将一组特定的行为和算法封装成类,以适应某些特定的上下文环境。

UserStrategy.php
<"2016冬季女装";
  }
  function showCategory(){
    echo "女装";
  }
}

MaleUser.php
<"IPhone6s";
  }
  function showCategory(){
    echo "电子产品";
  }
}

Page.php//执行文件
<"AD";
    $this->strategy->showAd();
    echo "<br>";
    echo "Category";
    $this->strategy->showCategory();
    echo "<br>";
  }
  function setStrategy(UserStrategy $strategy){
    $this->strategy=$strategy;
  }
}

$page = new Page();
if(isset($_GET['male'])){
  $strategy = new MaleUser();
}else {
  $strategy = new FemaleUser();
}
$page->setStrategy($strategy);
$page->index();

6、原型模式

不常用,大的对象类才使用,表现在clone

7、观察者模式

从面向过程的角度来看,首先是观察者向主题注册,注册完之后,主题再通知观察者做出相应的操作,整个事情就完了

/**
 * 事件产生类
 * Class EventGenerator
 */
abstract class EventGenerator
{
  private $ObServers = [];

  //增加观察者
  public function add(ObServer $ObServer)
  {
    $this->ObServers[] = $ObServer;
  }

  //事件通知
  public function notify()
  {
    foreach ($this->ObServers as $ObServer) {
      $ObServer->update();
    }
  }

}

/**
 * 观察者接口类
 * Interface ObServer
 */
interface ObServer
{
  public function update($event_info = null);
}

/**
 * 观察者1
 */
class ObServer1 implements ObServer
{
  public function update($event_info = null)
  {
    echo "观察者1 收到执行通知 执行完毕!\n";
  }
}

/**
 * 观察者1
 */
class ObServer2 implements ObServer
{
  public function update($event_info = null)
  {
    echo "观察者2 收到执行通知 执行完毕!\n";
  }
}

/**
 * 事件
 * Class Event
 */
class Event extends EventGenerator
{
  /**
   * 触发事件
   */
  public function trigger()
  {
    //通知观察者
    $this->notify();
  }
}

//创建一个事件
$event = new Event();
//为事件增加旁观者
$event->add(new ObServer1());
$event->add(new ObServer2());
//执行事件 通知旁观者
$event->trigger();

以上就是深入分析PHP设计模式的详细内容,更多关于PHP设计模式的资料请关注其它相关文章!

标签:
PHP,设计模式,PHP,设计模式分析

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com