设计模式——依赖倒置原则实例(PHP实现)
<?php
/**
* 设计模式——依赖倒置原则实例
* Created by DannyWang
* jue.wang@yulore.com
* 2015-05-05
*/
abstract class People {
public abstract function eat(Fruit $fruit);//这里符合了里氏替换原则
}
abstract class Fruit {
public abstract function getName();
}
class Danny extends People {
public function eat(Fruit $fruit) {//这里符合了里氏替换原则
echo 'Danny like eat ' . $fruit->getName() . '<br/>';
}
}
class Apple extends Fruit {
public function getName() {
return 'Apple';
}
}
class Banana extends Fruit {
public function getName() {
return 'Banana';
}
}
//client
$me = new Danny();
$me->eat(new Apple());//这里符合了里氏替换原则
$me->eat(new Banana());//这里符合了里氏替换原则
//Danny like eat Apple
//Danny like eat Banana
?>
每一天都是崭新的