《Head First设计模式》读书笔记(一)

本文通过PHP实现了策略模式和观察者模式。策略模式示例中创建了不同行为的接口和实现,如飞行和叫声行为,并应用于鸭子类。观察者模式示例展示了天气数据变化时通知观察者的机制。

php来实现书中的示例和习题,昨天完成策略模式和观察者模式,代码如下:

ExpandedBlockStart.gif代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><?php
interfaceFlyBehavior
{
functionfly();
}
interfaceQuackBehavior
{
functionquack();
}
classFlyWithWingsimplementsFlyBehavior
{
publicfunctionfly()
{
print("flywithwings/n");
}
}
classFlyNoWayimplementsFlyBehavior
{
publicfunctionfly()
{
print("flynoway/n");
}
}
classQuackimplementsQuackBehavior
{
publicfunctionquack()
{
print("quack,gua,gua/n");
}
}
abstractclassDuck
{
protected$flyBehavior=null;
protected$quackBehaviro=null;
publicfunction__construct(FlyBehavior$flyBehavior=null,QuackBehavior$quackBehavior=null)
{
$this->setFlyBehavior($flyBehavior);
$this->setQuackBehavior($quackBehavior);
}
publicfunctionfly()
{
$this->flyBehavior->fly();
}
publicfunctionquack()
{
$this->quackBehavior->quack();
}
publicfunctiongetFlyBehavior()
{
return$flyBehavior;
}
publicfunctionsetFlyBehavior(FlyBehavior$fly)
{
$this->flyBehavior=$fly;
}
publicfunctiongetQuackBehavior()
{
return$quackBehavior;
}
publicfunctionsetQuackBehavior(QuackBehavior$quack)
{
$this->quackBehavior=$quack;
}
abstractpublicfunctiondisplay();
}
classRedheadDuckextendsDuck
{
publicfunction__construct(FlyBehavior$flyBehavior=null,QuackBehavior$quackBehavior=null)
{
parent
::__construct($flyBehavior,$quackBehavior);
}
publicfunctiondisplay()
{
print("damnit!RedheadDuckdisplay()/n");
}
}
$duck=newRedheadDuck();
$duck->setFlyBehavior(newFlyWithWings());
$duck->setQuackBehavior(newQuack());
$duck->fly();
$duck->quack();
$duck->display();
?>

ExpandedBlockStart.gif代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><?php
abstractclassCharacter
{
protected$weapon=null;
publicfunction__construct(WeaponBehavior$weapon=null)
{
$this->setWeapon($weapon);
}
publicfunctionsetWeapon(WeaponBehavior$weapon)
{
$this->weapon=$weapon;
}
publicfunctiongetWeapon()
{
return$this->weapon;
}
publicfunctionuseWeapon()
{
$this->weapon->useWeapon();
}
}
classKnightextendsCharacter
{
}
classQueenextendsCharacter
{
}
interfaceWeaponBehavior
{
functionuseWeapon();
}
classKnifeWeaponimplementsWeaponBehavior
{
publicfunctionuseWeapon()
{
print("Knifeused/n");
}
}
$man=newKnight();
$man->setWeapon(newKnifeWeapon());
$man->useWeapon();
?>

ExpandedBlockStart.gif代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><?php
interfaceObservable
{
functionaddObserver(Observer$observer);
functionremoveObserver(Observer$observer);
functionnotifyServers();
}
interfaceObserver
{
functionupdate(WeatherData$weatherData);
}
classWeatherDataimplementsObservable
{
private$observers=array();
private$temperature=0;
private$humidity=0;
private$pressure=0;
publicfunctiongetTemperature()
{
return$this->temperature;
}
publicfunctionsetTemperature($temp)
{
$this->temperature=$temp;
}
publicfunctiongetHumidity()
{
return$this->humidity;
}
publicfunctionsetHumidity($humidity)
{
$this->humidity=$humidity;
}
publicfunctiongetPressure()
{
return$this->pressure;
}
publicfunctionsetPressure($pressure)
{
$this->pressure=$pressure;
}
publicfunctionsetMeasurements($temperature,$humidity,$pressure)
{
$this->setTemperature($temperature);
$this->setHumidity($humidity);
$this->setPressure($pressure);
$this->measureChanged();
}
publicfunctionmeasureChanged()
{
$this->notifyServers();
}
publicfunctionaddObserver(Observer$observer)
{
$this->observers[]=$observer;
}
publicfunctionremoveObserver(Observer$observer)
{
$result=array();
foreach($this->observersas$obj){
if($obj!==$observer){
$result[]=$obj;
}
}
unset($this->observers);
$this->observers=$result;
}
publicfunctionnotifyServers()
{
foreach($this->observersas$observer){
$observer->update($this);
}
}
publicfunctiongetObservers()
{
return$this->observers;
}
}
interfaceDisplayable
{
functiondisplay();
}
abstractclassBaseDisplayimplementsObserver,Displayable
{
private$weatherData=null;
publicfunction__construct(Observable$weatherData=null)
{
$this->weatherData=$weatherData;
if($weatherData!=null){
$this->weatherData->addObserver($this);
}
}
publicfunctionupdate(WeatherData$weatherData)
{
}
publicfunctiondisplay()
{
}
}
classCurrentConditionDisplayextendsBaseDisplay
{
private$weatherData=null;
private$temperature=0;
private$humidity=0;
private$pressure=0;
publicfunction__construct(Observable$weatherData=null)
{
$this->weatherData=$weatherData;
if($weatherData!=null){
$this->weatherData->addObserver($this);
}
}
publicfunctionupdate(WeatherData$weatherData)
{
$this->temperature=$weatherData->getTemperature();
$this->humidity=$weatherData->getHumidity();
$this->pressure=$weatherData->getPressure();
$this->display();
}
publicfunctiondisplay()
{
print("temp:".$this->temperature.",humi:".$this->humidity.",pressure:".$this->pressure."/n");
}
}
classGeneralDisplayextendsBaseDisplay
{

publicfunctionupdate(WeatherData$weatherData)
{
$this->display();
}
publicfunctiondisplay()
{
print("GeneralDisplay().display/n");
}
}
$weatherData=newWeatherData();

$display=newCurrentConditionDisplay($weatherData);
$display2=newGeneralDisplay($weatherData);
$weatherData->setMeasurements(20,30,40);
$weatherData->removeObserver($display2);
print("after.../n");
$weatherData->setMeasurements(33,44,55);
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值