<?php
//工厂模式
interface Doing
{
    function eat();
    function sleep();
}

class Cat implements Doing
{
    function eat()
    {
        echo '猫正在吃东西!<br />';
    }

    function sleep()
    {
        echo '猫正在睡觉!<br />';
    }
}

class Dog implements Doing
{
    function eat()
    {
        echo '狗正在吃东西!<br />';
    }

    function sleep()
    {
        echo '狗正在睡觉!<br />';
    }
}

class Factory
{
    static function createDoing($type)
    {
        switch($type){
            case 'cat':
                return new Cat();
                break;
            case 'dog':
                return new Dog();
                break;
        }
    }
}

$cat=Factory::createDoing('cat');
$dog=Factory::createDoing('dog');

 

相关文章:

  • 2021-10-16
  • 2021-11-30
  • 2021-07-29
猜你喜欢
  • 2022-12-23
  • 2021-08-15
  • 2021-04-11
  • 2021-04-13
  • 2021-07-22
  • 2021-10-02
相关资源
相似解决方案