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();
  • 相关阅读:
    iOS -一些常用的方法
    handoff了解
    UIlabel
    扩展运行机制
    github -- fork提交项目
    iOS
    AppDelegate解析
    KVC
    KVO
    xcode升级后, 插件失效修复
  • 原文地址:https://www.cnblogs.com/ping04/p/7650847.html
Copyright © 2011-2022 走看看