PHP学习之工厂模式

<?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');

 

posted @ 2019-06-03 18:00  随缘盛世  阅读(252)  评论(0编辑  收藏  举报