PHP学习之工厂模式
程序员文章站
2022-07-02 17:03:25
'; } function sleep() { echo '猫正在睡觉!'; } } class Dog implements Doing { function eat() { echo '狗正在吃东西!'; } function sleep() { echo '狗正在睡... ......
<?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');