zoukankan      html  css  js  c++  java
  • 面向对象 【单例模式,工厂模式】

      单例模式的意思就是一个类只能实例出一个对象

       class Ren

       {

            private function __conssteuct();

            public static function DuiXiang()      生成对象,做成静态访问 

               {

                     return new Ren();

                }

        }

       $r=Ren::DuiXiang;         虽然能生成对象,但是还是没有限制条件。

       class Ren

          {

             private static $dx,      加入一个判断量。

             private function __conssteuct();

             public static function DuiXiang()

                {

                     if(empty(self::$dx)

                         {

                              self::$dx= new Ren;    如果判断量为空,则新建一个对象,

                          }

                      return self::$dx;                 如果不为空,则返回值为新建的那个对象。

                 }

             }

           。工厂模式

             class yunsuan

             {

                  public $a;

                  public $b;

                  function suan(){}

             }

             class jia extends yunsuan           用继承来写类名会比较多,但是扩展性比价好。

            {

                 retuen $this->a +$this->b;

             }

             class jian extends yunsuan

            {

                 retuen $this->a  - $this->b;

             }

             工厂类

             class Gongchang

             {

                  function shengchan($f)           $f 可以用运算符

                  {

                      switch ($f)

                           {

                              case "+"

                              return new jia();

                              case "-"

                              return new jian();

                            }

                       }

             }

             调用 Gongchang::shengchan(“+”)

  • 相关阅读:
    android作业10.21
    安卓10.7作业
    安卓9.30
    9.23作业
    9.17安卓作业
    6.12作业
    5.29作业
    5.28上机作业
    leetcode 219
    策略模式
  • 原文地址:https://www.cnblogs.com/cyd123/p/6742564.html
Copyright © 2011-2022 走看看