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

来源:互联网 发布:武功知乎 编辑:程序博客网 时间:2024/06/02 06:59
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();

0 0
原创粉丝点击