您的位置:首页 > 编程语言 > PHP开发

php-设计模式-模板方法模式

2015-02-09 19:35 573 查看
abstract class Journey {
final public function takeATrip() {
$this->buyAFlight();
$this->takePlane();
$this->enjoyVacation();//!!!
$this->buyGift();
$this->takePlane();
}
//key feature
abstract protected function enjoyVacation();

//optional.
protected function buyGift() {
}

/**
* This method will be unknown by subclasses (better)
*/
private function buyAFlight() {
echo "Buying a flight\n";
}

final protected function takePlane() {
echo "Taking the plane\n";
}
}

class CityJourney extends Journey {
protected function enjoyVacation() {
echo "Eat, drink, take photos and sleep\n";
}
}

class BeachJourney extends Journey{
protected function enjoyVacation() {
echo "Swimming and sun-bathing\n";
}
}

$journey = new BeachJourney();
$journey->takeATrip();
//Buying a flight
//Taking the plane
//Swimming and sun-bathing

$journey = new CityJourney();
$journey->takeATrip();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: