@Chiang
2019-12-30T16:41:54.000000Z
字数 2972
阅读 473
设计模式
颜色抽象类
<?php
/**
* Color.php 颜色抽象类
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/11
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Bridge;
abstract class Color
{
abstract public function output();
}
红色类
<?php
/**
* RedColor.php 红色
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/11
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Bridge;
class RedColor extends Color
{
public function output()
{
return "红色";
}
}
绿色类
<?php
/**
* GreenColor.php 绿色
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/11
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Bridge;
class GreenColor extends Color
{
public function output()
{
return "绿色";
}
}
黄色类
<?php
/**
* YellowColor.php 黄色
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/11
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Bridge;
class YellowColor extends Color
{
public function output()
{
return "黄色";
}
}
形状抽象类 注入颜色类
<?php
/**
* Shape.php 形状抽象类
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/11
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Bridge;
abstract class Shape
{
/**
* @var Color
*/
protected $color;
/**
* Shape constructor.
*
* @param Color $color
*/
public function __construct(Color $color)
{
$this->color = $color;
}
/**
* @return mixed
*/
abstract public function run();
}
圆形
<?php
/**
* Circle.php 圆形
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/11
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Bridge;
class Circle extends Shape
{
public function run()
{
echo "{$this->color->output()} 圆形\n";
}
}
正方形
<?php
/**
* Square.php 正方形
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/11
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Bridge;
class Square extends Shape
{
public function run()
{
echo "{$this->color->output()} 正方形\n";
}
}
三角形
<?php
/**
* Triangle.php 三角形
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/11
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Bridge;
class Triangle extends Shape
{
public function run()
{
echo "{$this->color->output()} 三角形\n";
}
}
客户端调用
<?php
/**
* Client.php
*
* @author Luffy (lufei@swoole.com)
* @date 2019/11/11
* @copyright Swoole, Inc.
* @package sy-records/design-patterns
*/
namespace Luffy\DesignPatterns\Bridge;
require __DIR__ . '/../vendor/autoload.php';
class Client
{
public function run()
{
$red = new RedColor();
$yellow = new YellowColor();
$green = new GreenColor();
// 红色的正方形
$redSquare = new Square($red);
$redSquare->run();
// 黄色的正方形
$yellowSquare = new Square($yellow);
$yellowSquare->run();
// 绿色的正方形
$greenSquare = new Square($green);
$greenSquare->run();
// 红色的三角形
$redTriangle = new Triangle($red);
$redTriangle->run();
// 黄色的三角形
$yellowTriangle = new Triangle($yellow);
$yellowTriangle->run();
// 绿色的三角形
$greenTriangle = new Triangle($green);
$greenTriangle->run();
// 红色的圆形
$redCircle = new Circle($red);
$redCircle->run();
// 黄色的圆形
$yellowCircle = new Circle($yellow);
$yellowCircle->run();
// 绿色的圆形
$greenCircle = new Circle($green);
$greenCircle->run();
}
}
$client = new Client();
$client->run();