私は現在、アプリケーションのスケルトン クラスのセットを作成しています。まず、税規則、割引規則などを保持する StoreLogic という基本クラスから始めます。Cart、Order、Quote などのクラスは、すべてがStoreLogic が提供するメソッドのセットと同じです。
これらのコア クラスが完成したら、Cart、Order、Quote、および StoreLogic を拡張して実装します。これらのクラスの各アプリケーションは、さまざまなクライアントのニーズに応じて異なるためです。親クラスのメソッドをオーバーライドするのは簡単ですが、子が拡張する前に祖父母クラスをオーバーライドするのは不可能に思えますか? 私はこれを間違った方法でやっていると感じます..そして、あなたのような経験豊富な誰かが私を正しい方向に向けることができると思います. コードを見て、あなたの考えを見てください!
/* My core classes are something like this: */
abstract class StoreLogic
{
public function applyDiscount($total)
{
return $total - 10;
}
}
abstract class Cart extends StoreLogic
{
public function addItem($item_name)
{
echo 'added' . $item_name;
}
}
abstract class Order extends StoreLogic
{
// ....
}
/* Later on when I want to use those core classes I need to be able to override
* methods from the grandparent class so the grandchild can use the new overriden
* methods:
*/
class MyStoreLogic extends StoreLogic
{
public function applyDiscount($total) {
return $total - 5;
}
}
class MyOrder extends Order
{
// ...
}
class MyCart extends Cart
{
public $total = 20;
public function doDiscounts()
{
$this->total = $this->applyDiscount($this->total);
echo $this->total;
}
}
$cart = new MyCart();
$cart->doDiscounts(); // Uses StoreLogic, not MyStoreLogic..