zoukankan      html  css  js  c++  java
  • php面向对象(设计模式 工厂模式)

    //设计模式
    //单例模式
    //类的计划生育
    //让该类在外界无法造成对象
    //让外界可以造一个对象,做一个静态方法返回对象
    //在累里面可以通过静态变量控制返回对象只能有一个

    //class Cat
    //{
    // public $name;
    // private function __construct()
    // {
    // }
    // static $temp;
    // static function new_ob(){
    // if (empty(self::$temp)){
    // self::$temp=new cat();
    // }
    // return self::$temp;
    //
    // }
    // function speak(){
    // return"miaomiaomiao";
    // }
    //}
    //$mao = Cat::new_ob();
    //$mao2= Cat::new_ob();
    //$mao->name="py";
    //echo $mao2->name;
    //$mao = new cat();
    //$mao;
    //if (empty($mao)){
    // $mao=new cat();
    //}
    // $xxx= $mao;



    //工厂模式
    //class jsq
    //{
    // public $a;
    // public $b;
    //
    // public function jiafa(){
    // return $this->a+$this->b;
    // }
    //}

    abstract class jsq
    {
    public $a;
    public $b;
    function yunsuan(){
    }
    }

    class jiafa extends jsq
    {
    function yunsuanfangfa(){
    return $this->a+$this->b;
    }
    }
    class jianfa extends jsq
    {
    function yunsuanfangfa(){
    return $this->a-$this->b;
    }
    }

    class Factory
    {
    static function create($x){
    switch ($x){
    case"+";
    return new jiafa();
    break;
    case "-";
    return new jianfa();
    break;
    }
    }
    }

    //$j1 = new jiafa();
    //$j1->a = 1;
    //$j1->b = 2;
    //$j1->yunsuanfangfa();

    $j1= Factory::create("+");
    $j1->a=1;
    $j1->b=2;
    echo $j1->yunsuanfangfa();
  • 相关阅读:
    The Triangle_DP
    LITTLE SHOP OF FLOWERS_DP
    K Best(最大化平均数)_二分搜索
    Number Game_状态压缩
    Stockbroker Grapevine_Floyd
    A very hard Aoshu problem
    AOE 网络
    AOV网
    最小生成树
    [POJ] 1562 Oil Deposits (DFS)
  • 原文地址:https://www.cnblogs.com/ping04/p/7650847.html
Copyright © 2011-2022 走看看