工厂模式
单元素模式
观察者模式
命令链模式
策略模式
- class people {
- private $name = '';
- private $user = null;
-
- private function __constract($name){/*此处private定义辅助实现 单元素模式*/
- $this->name = $name;
- }
-
- public static function instance($name){/*此方法实现 工厂模式*/
- static $object = null;/*此变量实现 单元素模式*/
- if (is_null($object))
- $object = new people($name);
- return $object;
- }
-
- public function work_in($who=null)
- {
- if (is_null($who)) echo 'error';
- else {
- $this->user[] = $who;/*此数组变量实现 观察者模式*/
- echo $who->work();/*此方法调用实现 策略模式*/
- }
- }
-
- public function on_action($which=''){
- if (empty($which)) echo 'error';
- else {
- foreach ($this->user as $user)
- $user->action($which);/*此方法调用实现 命令链模式*/
- }
- }
- }
-
- $people = people::instance('jack');
- $people->work_in(new student);
- $people->work_in(new teacher);
- $people->on_action('eat');
-
- class student {
- function work(){
- echo '
我是学生,朝九晚五。';
- }
-
- function action($which){
- if (method_exists($this, $which)) return $this->$which();
- else echo 'you are wrong!';
- }
-
- function eat(){
- echo '
我是学生,只能吃套餐。';
- }
- }
-
- class teacher {
- function work(){
- echo '
我是老师,晚上备课最忙。';
- }
-
- function action($which){
- if (method_exists($this, $which)) return $this->$which();
- else echo 'i can not do it!';
- }
-
- function eat(){
- echo '
我是老师,可以每天吃大餐。';
- }
- }
复制代码
|
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31