PHP重构之函数上移

参考《重构》

<?php
abstract class Customer
{
    public function addBill($date, $amount)
    {
        echo "addBill()".'<br>';
    } 
    public function CreateBill($date)
    {
        echo "CreateBill()".'<br>';
        $this->chargeFor();
    }
    abstract protected function chargeFor();  // 将子类不同的函数部分抽象为抽象方法,让子类去实现
}

class RegularCustomer extends Customer
{
    protected function chargeFor()
    {
        echo "go to north area, buy some food!".'<br>';
        echo "find a small shop eat noondles!".'<br>';
    }
}

class PreferredCustomer extends Customer
{
    protected function chargeFor()
    {
        echo "buy drinks!!!".'<br>';
        echo "go to bank!!!".'<br>';
    }
}

$jack = new RegularCustomer();
$mike = new PreferredCustomer();

$jack->CreateBill("2013-08-12");
$jack->addBill("2013-08-12", 35);
echo "<br>".'<br>'.'<br>';
$mike->CreateBill("2013-09-11");
$mike->addBill("2013-09-11", 55);
?>
原文地址:https://www.cnblogs.com/Robotke1/p/3264728.html