用php来实现书中的示例和习题,昨天完成策略模式和观察者模式,代码如下:
代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
<?
php
interface FlyBehavior
{
function fly();
}
interface QuackBehavior
{
function quack();
}
class FlyWithWings implements FlyBehavior
{
public function fly()
{
print ( " flywithwings/n " );
}
}
class FlyNoWay implements FlyBehavior
{
public function fly()
{
print ( " flynoway/n " );
}
}
class Quack implements QuackBehavior
{
public function quack()
{
print ( " quack,gua,gua/n " );
}
}
abstract class Duck
{
protected $flyBehavior = null ;
protected $quackBehaviro = null ;
public function __construct(FlyBehavior $flyBehavior = null , QuackBehavior $quackBehavior = null )
{
$this -> setFlyBehavior( $flyBehavior );
$this -> setQuackBehavior( $quackBehavior );
}
public function fly()
{
$this -> flyBehavior -> fly();
}
public function quack()
{
$this -> quackBehavior -> quack();
}
public function getFlyBehavior()
{
return $flyBehavior ;
}
public function setFlyBehavior(FlyBehavior $fly )
{
$this -> flyBehavior = $fly ;
}
public function getQuackBehavior()
{
return $quackBehavior ;
}
public function setQuackBehavior(QuackBehavior $quack )
{
$this -> quackBehavior = $quack ;
}
abstract public function display();
}
class RedheadDuck extends Duck
{
public function __construct(FlyBehavior $flyBehavior = null , QuackBehavior $quackBehavior = null )
{
parent :: __construct( $flyBehavior , $quackBehavior );
}
public function display()
{
print ( " damnit!RedheadDuckdisplay()/n " );
}
}
$duck = new RedheadDuck();
$duck -> setFlyBehavior( new FlyWithWings());
$duck -> setQuackBehavior( new Quack());
$duck -> fly();
$duck -> quack();
$duck -> display();
?>
interface FlyBehavior
{
function fly();
}
interface QuackBehavior
{
function quack();
}
class FlyWithWings implements FlyBehavior
{
public function fly()
{
print ( " flywithwings/n " );
}
}
class FlyNoWay implements FlyBehavior
{
public function fly()
{
print ( " flynoway/n " );
}
}
class Quack implements QuackBehavior
{
public function quack()
{
print ( " quack,gua,gua/n " );
}
}
abstract class Duck
{
protected $flyBehavior = null ;
protected $quackBehaviro = null ;
public function __construct(FlyBehavior $flyBehavior = null , QuackBehavior $quackBehavior = null )
{
$this -> setFlyBehavior( $flyBehavior );
$this -> setQuackBehavior( $quackBehavior );
}
public function fly()
{
$this -> flyBehavior -> fly();
}
public function quack()
{
$this -> quackBehavior -> quack();
}
public function getFlyBehavior()
{
return $flyBehavior ;
}
public function setFlyBehavior(FlyBehavior $fly )
{
$this -> flyBehavior = $fly ;
}
public function getQuackBehavior()
{
return $quackBehavior ;
}
public function setQuackBehavior(QuackBehavior $quack )
{
$this -> quackBehavior = $quack ;
}
abstract public function display();
}
class RedheadDuck extends Duck
{
public function __construct(FlyBehavior $flyBehavior = null , QuackBehavior $quackBehavior = null )
{
parent :: __construct( $flyBehavior , $quackBehavior );
}
public function display()
{
print ( " damnit!RedheadDuckdisplay()/n " );
}
}
$duck = new RedheadDuck();
$duck -> setFlyBehavior( new FlyWithWings());
$duck -> setQuackBehavior( new Quack());
$duck -> fly();
$duck -> quack();
$duck -> display();
?>
代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
<?
php
abstract class Character
{
protected $weapon = null ;
public function __construct(WeaponBehavior $weapon = null )
{
$this -> setWeapon( $weapon );
}
public function setWeapon(WeaponBehavior $weapon )
{
$this -> weapon = $weapon ;
}
public function getWeapon()
{
return $this -> weapon;
}
public function useWeapon()
{
$this -> weapon -> useWeapon();
}
}
class Knight extends Character
{
}
class Queen extends Character
{
}
interface WeaponBehavior
{
function useWeapon();
}
class KnifeWeapon implements WeaponBehavior
{
public function useWeapon()
{
print ( " Knifeused/n " );
}
}
$man = new Knight();
$man -> setWeapon( new KnifeWeapon());
$man -> useWeapon();
?>
abstract class Character
{
protected $weapon = null ;
public function __construct(WeaponBehavior $weapon = null )
{
$this -> setWeapon( $weapon );
}
public function setWeapon(WeaponBehavior $weapon )
{
$this -> weapon = $weapon ;
}
public function getWeapon()
{
return $this -> weapon;
}
public function useWeapon()
{
$this -> weapon -> useWeapon();
}
}
class Knight extends Character
{
}
class Queen extends Character
{
}
interface WeaponBehavior
{
function useWeapon();
}
class KnifeWeapon implements WeaponBehavior
{
public function useWeapon()
{
print ( " Knifeused/n " );
}
}
$man = new Knight();
$man -> setWeapon( new KnifeWeapon());
$man -> useWeapon();
?>
代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
<?
php
interface Observable
{
function addObserver(Observer $observer );
function removeObserver(Observer $observer );
function notifyServers();
}
interface Observer
{
function update(WeatherData $weatherData );
}
class WeatherData implements Observable
{
private $observers = array ();
private $temperature = 0 ;
private $humidity = 0 ;
private $pressure = 0 ;
public function getTemperature()
{
return $this -> temperature;
}
public function setTemperature( $temp )
{
$this -> temperature = $temp ;
}
public function getHumidity()
{
return $this -> humidity;
}
public function setHumidity( $humidity )
{
$this -> humidity = $humidity ;
}
public function getPressure()
{
return $this -> pressure;
}
public function setPressure( $pressure )
{
$this -> pressure = $pressure ;
}
public function setMeasurements( $temperature , $humidity , $pressure )
{
$this -> setTemperature( $temperature );
$this -> setHumidity( $humidity );
$this -> setPressure( $pressure );
$this -> measureChanged();
}
public function measureChanged()
{
$this -> notifyServers();
}
public function addObserver(Observer $observer )
{
$this -> observers[] = $observer ;
}
public function removeObserver(Observer $observer )
{
$result = array ();
foreach ( $this -> observers as $obj ){
if ( $obj !== $observer ){
$result [] = $obj ;
}
}
unset ( $this -> observers);
$this -> observers = $result ;
}
public function notifyServers()
{
foreach ( $this -> observers as $observer ){
$observer -> update( $this );
}
}
public function getObservers()
{
return $this -> observers;
}
}
interface Displayable
{
function display();
}
abstract class BaseDisplay implements Observer , Displayable
{
private $weatherData = null ;
public function __construct(Observable $weatherData = null )
{
$this -> weatherData = $weatherData ;
if ( $weatherData != null ){
$this -> weatherData -> addObserver( $this );
}
}
public function update(WeatherData $weatherData )
{
}
public function display()
{
}
}
class CurrentConditionDisplay extends BaseDisplay
{
private $weatherData = null ;
private $temperature = 0 ;
private $humidity = 0 ;
private $pressure = 0 ;
public function __construct(Observable $weatherData = null )
{
$this -> weatherData = $weatherData ;
if ( $weatherData != null ){
$this -> weatherData -> addObserver( $this );
}
}
public function update(WeatherData $weatherData )
{
$this -> temperature = $weatherData -> getTemperature();
$this -> humidity = $weatherData -> getHumidity();
$this -> pressure = $weatherData -> getPressure();
$this -> display();
}
public function display()
{
print ( " temp: " . $this -> temperature . " ,humi: " . $this -> humidity . " ,pressure: " . $this -> pressure . " /n " );
}
}
class GeneralDisplay extends BaseDisplay
{
public function update(WeatherData $weatherData )
{
$this -> display();
}
public function display()
{
print ( " GeneralDisplay().display/n " );
}
}
$weatherData = new WeatherData();
$display = new CurrentConditionDisplay( $weatherData );
$display2 = new GeneralDisplay( $weatherData );
$weatherData -> setMeasurements( 20 , 30 , 40 );
$weatherData -> removeObserver( $display2 );
print ( " after.../n " );
$weatherData -> setMeasurements( 33 , 44 , 55 );
?>
interface Observable
{
function addObserver(Observer $observer );
function removeObserver(Observer $observer );
function notifyServers();
}
interface Observer
{
function update(WeatherData $weatherData );
}
class WeatherData implements Observable
{
private $observers = array ();
private $temperature = 0 ;
private $humidity = 0 ;
private $pressure = 0 ;
public function getTemperature()
{
return $this -> temperature;
}
public function setTemperature( $temp )
{
$this -> temperature = $temp ;
}
public function getHumidity()
{
return $this -> humidity;
}
public function setHumidity( $humidity )
{
$this -> humidity = $humidity ;
}
public function getPressure()
{
return $this -> pressure;
}
public function setPressure( $pressure )
{
$this -> pressure = $pressure ;
}
public function setMeasurements( $temperature , $humidity , $pressure )
{
$this -> setTemperature( $temperature );
$this -> setHumidity( $humidity );
$this -> setPressure( $pressure );
$this -> measureChanged();
}
public function measureChanged()
{
$this -> notifyServers();
}
public function addObserver(Observer $observer )
{
$this -> observers[] = $observer ;
}
public function removeObserver(Observer $observer )
{
$result = array ();
foreach ( $this -> observers as $obj ){
if ( $obj !== $observer ){
$result [] = $obj ;
}
}
unset ( $this -> observers);
$this -> observers = $result ;
}
public function notifyServers()
{
foreach ( $this -> observers as $observer ){
$observer -> update( $this );
}
}
public function getObservers()
{
return $this -> observers;
}
}
interface Displayable
{
function display();
}
abstract class BaseDisplay implements Observer , Displayable
{
private $weatherData = null ;
public function __construct(Observable $weatherData = null )
{
$this -> weatherData = $weatherData ;
if ( $weatherData != null ){
$this -> weatherData -> addObserver( $this );
}
}
public function update(WeatherData $weatherData )
{
}
public function display()
{
}
}
class CurrentConditionDisplay extends BaseDisplay
{
private $weatherData = null ;
private $temperature = 0 ;
private $humidity = 0 ;
private $pressure = 0 ;
public function __construct(Observable $weatherData = null )
{
$this -> weatherData = $weatherData ;
if ( $weatherData != null ){
$this -> weatherData -> addObserver( $this );
}
}
public function update(WeatherData $weatherData )
{
$this -> temperature = $weatherData -> getTemperature();
$this -> humidity = $weatherData -> getHumidity();
$this -> pressure = $weatherData -> getPressure();
$this -> display();
}
public function display()
{
print ( " temp: " . $this -> temperature . " ,humi: " . $this -> humidity . " ,pressure: " . $this -> pressure . " /n " );
}
}
class GeneralDisplay extends BaseDisplay
{
public function update(WeatherData $weatherData )
{
$this -> display();
}
public function display()
{
print ( " GeneralDisplay().display/n " );
}
}
$weatherData = new WeatherData();
$display = new CurrentConditionDisplay( $weatherData );
$display2 = new GeneralDisplay( $weatherData );
$weatherData -> setMeasurements( 20 , 30 , 40 );
$weatherData -> removeObserver( $display2 );
print ( " after.../n " );
$weatherData -> setMeasurements( 33 , 44 , 55 );
?>