第一种 单一责任原则
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();
开闭原则可以有效的应对需求的变化
开闭原则可以指导或者引领我们设计且灵活的系统
第三种里氏替换原则
php中有很多思想都遵循这个原则 一时间想不起来了
abstract class SetStr
{
public abstract function Str();
public function rand_str()
{
$arr = ['!','#','$','%','^','&','*','(',')','_','-','+','='];
$indexs = array_rand($arr,4);
$str = $this->Str();
foreach ($indexs as $index){
$str .= $arr[$index];
}
return $str;
}
}
class Num extends SetStr
{
public function Str()
{
return rand(0,10000);
}
}
class English extends SetStr
{
public function Str()
{
$arr = ['a','b','c','d','e','f','g'];
$indexs = array_rand($arr,4);
$str = '';
foreach ($indexs as $index){
$str .= $arr[$index];
}
return $str;
}
}
class client
{
public function run()
{
$SetStr = new Num();
$rand_str = new English();
echo "4位随机数字字符串:",$SetStr->Str().PHP_EOL;
echo "4为随机英文字符串:",$rand_str->Str(),PHP_EOL;
echo "随机数字与特殊字符串:".$SetStr->rand_str(),PHP_EOL;
echo "随机英文与特殊字符串:".$rand_str->rand_str();
}
}
$client = new client();
$client->run();
依赖替换原则
interface I
{
public function str();
}
class B implements I
{
public function str()
{
return rand(0,10000);
}
}
class C implements I
{
public function str()
{
$arr = ['a','b','c','d','e','f','g'];
$indexs = array_rand($arr,4);
$str = '';
foreach ($indexs as $index){
$str .= $arr[$index];
}
return $str;
}
}
class D implements I
{
public function str()
{
return "1111";
}
}
class A
{
public function boot(I $I)
{
echo "开始生成字符串,预计时间3秒...".PHP_EOL;
for($i=3;$i>=0;$i--){
sleep(1);
echo $i."秒".PHP_EOL;
}
echo "生成字符串结束,你的字符串为:",$I->str().PHP_EOL;
}
}
class client
{
public function run()
{
$set = new A();
$set->boot(new B());
$set->boot(new C());
$set->boot(new D());
}
}
$client = new client();
$client->run();
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 Set
{
public function new(SetStr $setStr)
{
echo "开始生成字符串".PHP_EOL;
return $setStr->boot();
}
}
class client
{
public function run()
{
$set = new Set();
echo $set->new(new Num()).PHP_EOL;
echo $set->new(new English()).PHP_EOL;
}
}
$client = new client();
$client->run();
接口隔离原则
interface I
{
public function action1();
public function action2();
public function action3();
public function action4();
public function action5();
public function action6();
}
class C implements I
{
public function