• php 模式



    设计模式
    1.单例模式
    类的计划生育
    1.让该类在外界无法造对象
    2.让外界可以造一个对象,做一个静态方法返回对象
    3.在类里面通过静态变量控

      1 class Dog
      2 {
      3     static $dx;
      4     public $test;
      5     
      6     private function __construct()
      7     {
      8         
      9     }
     10     
     11     static function DuiXiang()
     12     {
     13         //return new Dog();
     14         
     15         if(empty(self::$dx))
     16         {
     17             self::$dx = new Dog();
     18         }
     19         
     20         return self::$dx;
     21     }
     22 }
     23 
     24 $a = Dog::DuiXiang();
     25 
     26 
     27 $b = Dog::DuiXiang();
     28 $b->test="hello";
     29 
     30 
     31 var_dump($a);
     32 
     33 //工厂模式
     34 /*class YunSuan
     35 {
     36     public $a;
     37     public $b;
     38     
     39     function Jia()
     40     {
     41         return $a+$b;
     42     }
     43     function Jian()
     44     {
     45         return $a-$b;
     46     }
     47 }*/
     48 
     49 abstract class YuanSuan
     50 {
     51     public $a;
     52     public $b;
     53     
     54     function Suan()
     55     {
     56     }
     57 }
     58 
     59 class Jia extends YuanSuan
     60 {
     61     function Suan()
     62     {
     63         return $this->a+$this->b;
     64     }
     65 }
     66 
     67 class Jian extends YuanSuan
     68 {
     69     function Suan()
     70     {
     71         return $this->a-$this->b;
     72     }
     73 }
     74 
     75 class Cheng extends YuanSuan
     76 {
     77     function Suan()
     78     {
     79         return $this->a*$this->b;
     80     }
     81 }
     82 
     83 /*$j = new Cheng();
     84 $j->Suan();
     85 */
     86 
     87 class GongChang
     88 {
     89     static function ShengChan($f)
     90     {
     91         switch($f)
     92         {
     93             case "+":
     94                 return new Jia();
     95                 break;
     96             case "-":
     97                 return new Jian();
     98                 break;
     99             case "*":
    100                 return new Cheng();
    101                 break;
    102         }
    103     }
    104 }
    105 
    106 
    107 $r = GongChang::ShengChan("*");
    108 $r->a=10;
    109 $r->b=5;
    110 echo $r->Suan();
    View Code

    制返回对象只能是一个

  • 相关阅读:
    Django的一点基本知识点
    python函数定义及作用域
    爬取妹子图片
    mysql常用命令大全
    python扫面端口
    python中重要的基础概念
    pexpect模块获取root密码
    mysql
    requests库
    C#中使用SQLite数据库简介(上)
  • 原文地址:https://www.cnblogs.com/liuran123/p/6005858.html
Copyright © 2020-2023  润新知