第一种 单一责任原则
class product
{
public function productInfo()
{
echo "查看商品详细信息".PHP_EOL;
}
}
class seckill
{
public function seckillInfo()
{
echo "查看秒杀商品详细信息".PHP_EOL;
}
}
class client
{
public function run()
{
$product = new product();
$seckill = new seckill();
$product->productInfo();
$seckill->seckillInfo();
}
}
$client = new client();
$client->run();
laravel框架的服务容器与服务容器的注册 => 严格遵守了 单一原则
开闭原则
interface SetStr
{
public function boot();
}
class Num implements SetStr
{
public function boot()
{
return rand(0,10000);
}
}
class English implements SetStr
{
public function boot()
{
$length = 4;
$arr = ['a','b','c','d','e','f','g'];
$indexs = array_rand($arr,$length);
$str = '';
foreach ($indexs as $index){
$str .= $arr[$index];
}
return $str;
}
}
class client
{
public function run()
{
$num = new Num();
return $num->boot();
}
}
$client = new client();
echo $client->run();
开闭原则可以有效的应对需求的变化
开闭原则可以指导或者引领我们设计且灵活的系统