• php框架中常用的设计模式


    1.单例模式

      

     1 //单例模式
     2 class Demo
     3 {
     4     private static $obj;
     5     
     6     private function __construct()
     7     {
     8         
     9     }
    10     
    11     
    12     private function __clone()
    13     {
    14     
    15     }
    16     
    17     public static function OBJ()
    18     {
    19         if(!isset(self::$obj))
    20         {
    21             self::$obj = new self;
    22         }
    23         return self::$obj;
    24     }
    25             
    26 }

    2.工厂模式

    //简单工厂模式
    //抽象方法
    abstract  class  operation
    {
        abstract public function getValue($one,$two);
    }
    
    //jia  fa  lei
    class operationAdd extends operation
    {
        public function getValue($one, $two)
        {
            return $one + $two;    
        }
    }
    
    class operationSub extends operation
    {
        public function getValue($one, $two)
        {
            return $one - $two;    
        }
    }
    
    class factory
    {
        public static function getObject($name)
        {
            switch($name){
            case '+': return new operationAdd(); break;
            case '-': return new operationSub(); break;
            default: 
                break;    
            
            }
        }
    
    }
    
    
    
    $demo = factory::getObject('+');
    $value = $demo->getValue(2,1);
    echo $value;

    3.注册器模式(把每次new的对象放到一个静态数组中)

    4. 观察者模式

       把每个需要通知的类,实例化后的对象交给观察者类中的静态数组保存,然后调用观察者类的方法,方法的作用是便利静态数组,调用每个对象中的通知方法

     

     1 <?php
     2 //guan cha zhe lei
     3 class Server
     4 {
     5     private static $objList = array();
     6 
     7     public function add($obj)
     8     {
     9         self::$objList[] = $obj;    
    10     }
    11     
    12     public function notify()
    13     {
    14         foreach(self::$objList as $obj)
    15         {
    16             $obj->notify();    
    17         }    
    18     }
    19 }
    20 
    21 abstract class client
    22 {
    23     abstract public function notify();
    24 }
    25 
    26 class one extends client
    27 {
    28     public function notify()
    29     {
    30         echo "one<br>";    
    31     }
    32 }
    33 
    34 class two extends client
    35 {
    36     public function notify()
    37     {
    38         echo "two<br>";    
    39     }
    40 }
    41 
    42 
    43 $server = new Server(); 
    44 
    45 $one = new one();
    46 $two = new two();
    47 
    48 $server->add($one);
    49 $server->add($two);
    50 
    51 $server->notify();
  • 相关阅读:
    Building a Basic .NET Remoting Application
    Building a Basic .NET Remoting Application 之一 Building a Remotable Type
    Type Library Exporter (Tlbexp.exe)
    .NET Remoting Architecture
    Runtime Hosts
    Building a Basic .NET Remoting Application 之二 Building a Host Application
    Building a Basic .NET Remoting Application 之三 Building a Client Application
    C语言字符串大全(转自百度百科)
    作业 双向链表
    递推练习 简单n!
  • 原文地址:https://www.cnblogs.com/hanyouchun/p/4192888.html
Copyright © 2020-2023  润新知