PHP设计模式之桥接模式

<?php
/**
 * 2020-6-3 Xun
 * bridging(桥接模式)
 * 在面向对象设计法则中有一个开闭原则:对修改封闭,对扩展开放
 * 通过发信息举例子,发送信息的方式有两种:短信与邮箱,信息的类型有三种:普通,加急,特急
 */

//邮箱发送
class Post{

    //定义保存信息内容的属性
    protected  $type=null;

    //构造函数
    public function __construct($type)
    {
        $this->type=$type;
    }

    //定义发送方法
    public function send()
    {
        return '邮箱发送'.$this->type->send();
    }
}

//短信发送
class Sms{

    //定义保存信息内容的属性
    protected  $type=null;

    //构造函数
    public function __construct($type)
    {
        $this->type=$type;
    }

    //定义发送方法
    public function send()
    {
        return '短信发送:'.$this->type->send();
    }
}

//定义信息类型
class Common{
    //定义信息属性
    protected $text;

    //定义构造函数
    public function __construct($text)
    {
        //给属性赋值
        $this->text=$text;
    }

    //定义发送的方法
    public function send()
    {
        return $this->text.'(普通)';
    }
}

//定义信息类型
class Urgent{
    //定义信息属性
    protected $text;

    //定义构造函数
    public function __construct($text)
    {
        //给属性赋值
        $this->text=$text;
    }

    //定义发送的方法
    public function send()
    {
        return $this->text.'(加急)';
    }
}

//定义信息类型
class ExtraUrgent{
    //定义信息属性
    protected $text;

    //定义构造函数
    public function __construct($text)
    {
        //给属性赋值
        $this->text=$text;
    }

    //定义发送的方法
    public function send()
    {
        return $this->text.'(特急)';
    }
}


//邮件形式发送特急信息
$post=new Post(new ExtraUrgent('我永远喜欢菠萝赛东'));
echo $post->send();

echo '<br/>';

//短信形式发送普通信息
$post=new Sms(new Urgent('我永远喜欢菠萝赛东'));
echo $post->send();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值