鉴于
Sportman
abstract class Sportman
{
public abstract function run();
}
class Soccer extends Sportman
{
public function run()
{
// soccer running
}
}
class Tennisplayer extends Sportman
{
public function run()
{
// tennisplayer running (jumping)
}
}
现在他们都有目标了,所以
abstract class Goals
{
public abstract function hit();
}
class Runner extends Goals
{
public function hit()
{
// kick a ball and score a goal
}
}
class Tennisplayer extends Goals
{
public function run()
{
// hit back the ball so that his opponent wont be able to return
}
}
现在这里是层次结构。每当添加新的
Sportman
时,也应添加新的Goals
。
我们可以尝试将层次结构合并为一个:
abstract class Sportman
{
public abstract function run();
public abstract function hit();
}
class Runner extends Sportman
{
public function run()
{
// sportman running
}
public function hit()
{
// kick a ball and score a goal
}
}
class Tennisplayer extends Sportman
{
public function run()
{
// tennisplayer running (jumping)
}
public function run()
{
// hit back the ball so that his opponent wont be able to return
}
}
但我真的不认为它在这里起作用,因为随着时间的推移,这些类变成了一个“包”,有很多方法和很多依赖项。还有其他想法吗?