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