php设计模式重构篇

第一种 单一责任原则
在这里插入图片描述

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值