[php]简单工厂模式模式面向对象设计

2012/10/11 23:10:00  人气 3272    编程语言区  
文件1 phone.php
<?php
class GSMphone{
private $$screen;
//组装手机
public function assemble(){
echo ‘正在组装手机的零部件,请稍后。。。。‘;
}
public function box(){
echo ‘正在给手机装盒,请稍后。。。。‘;
}
}
//滑盖的手机
class SlideGSMphone extends GSMphone{
private $$slidelen =0;//滑出去多少
public function __construct(){
echo ‘滑盖手机,一滑到底<br/>‘;
}
}
//翻盖手机
class FlipGSMphone extends GSMphone{
private $$angle =0;//你怎么翻
public function __construct(){
echo ‘翻盖手机,手机中的战斗机<br/>‘;
}
}
//直板手机
class BarGSMphone extends GSMphone{
public function __construct(){
echo ‘直板手机,向胡总致敬<br/>‘;
}
}



?>
文件2 *.php
<?php
header(‘content-Type:text/html;charset=utf-8‘);
require_once ‘phone.php‘;
//把变化的部分封装起来
class SimpleFactory{
public function *Phone($$type){
//确定选造哪一部手机
if($$type ==‘filp‘){
$$phone = new FlipGSMPhone();
}elseif($$type == ‘slide‘){
$$phone = new SlideGSMPhone();
}else{
$$phone = new BarGSMPhone();
}
return $$phone;
}
}
class Producer{
private $$factory = null;
public function __construct(SimpleFactory $$factory){
$$this->factory = $$factory;
}
public function getPhone($$type){
$$phone = $$this->factory->*Phone($$type);


//造手机流程
$$phone->assemble();
$$phone->box();
}
}

$$factory = new SimpleFactory();
$$pro = new Producer($$factory);
$$type = ‘slide‘;
$$pro->getPhone($$type);
?>

. 自从来到了琴剑听月,忽然找到了生活最大的乐趣!琴剑听月!我的最爱!

发表回复

   


  通知楼主
暂无回复!

朋友圈|阅读|美图|切换

琴剑听月 微信:qjtywwx 12月22日(日) 16:58
©2015 数据时代 京ICP备13050724号