php设计模式(六)桥接模式

   作用:将两个不相关的类结合在一起,通过两个类中的方法和属性,得到新的功能

// 讲师分配
abstract class Students
{
    abstract public function classroom();
}

class OpenStudents extends Students
{
    public function classroom()
    {
        return '公开课教室1,公开课教室2,公开课教室3';
    }
}

class VipStudents extends Students
{
    public function classroom()
    {
        return 'VIP教室1,VIP教室2,VIP教室3';
    }
}

// 学员上课
abstract class Listen
{
    abstract public function project($who, $course);
}

class Course extends Listen
{
    public function project($who, $course)
    {
        return $course . '这几个教室正在上' . $who;
    }
}

class StudentListen
{
    public $classroom;

    public $course;

    public function __construct($classroom, $course)
    {
        $this->classroom = $classroom;
        $this->course = $course;
    }

    public function privilege($who)
    {
        $roomName = $this->classroom->classroom();
        return $this->course->project($roomName, $who);
    }

}
$classroom = new OpenStudents(); // 公开课教室
$course = new Course();

$obj = new StudentListen($classroom, $course);
echo $obj->privilege('swoole');

 

    原文作者:Yee Jason
    原文地址: https://blog.csdn.net/qq_38287952/article/details/86821776
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞