工厂模式1

 
<?php
/**
 * Created by PhpStorm.
 * User: huiting
 * Date: 18-3-22
 * Time: 上午9:18
 */

interface IUser{

    function getName();
    function getAge();
}

interface IOther{
    function getName();
    function oindex();
}

//
class User implements IUser{

    public  function  __construct($id)
    {
        $this->user_id = $id;

    }

    public function getName()
    {
        // TODO: Implement getName() method.
        return 'name'.$this->user_id;
    }

    public function getAge(){

        return 'age'.$this->user_id;
    }

}

class Other implements IOther{

    public function __construct($id)
    {

        $this->id = $id;
    }

    public function getName(){

        return 'other'.$this->id;
    }

    public function oindex(){

        return 'ccv';
    }
}

class Factory
{

    // 返回学生姓名
    public static function create($id){

        return new User($id);
    }

    // 返回其他信息
    public static function other($id){

        return new Other($id);
    }

    // 根据不同的参数实例化不同的类
    public static function toClass($id,$class='Other'){

        return new $class($id);
    }

    public function index(){
        return 'cc';
    }

}
// 工厂模式1 就是一个工厂类 里面有其他生产类 在工厂类里面已经实例化过其他生产类 在生产过程中只需要针对工厂要对应的产品就可以啦
include  'Factory.php';

$c = Factory::toClass(4,'User');
echo $c->getAge().'<br>';
//echo $c->getName();
https://www.ibm.com/developerworks/cn/opensource/os-php-designptrns/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值