
illustrate
1. Combine two originally unrelated classes, and then use the methods and attributes in the two classes to output a new result.
2. The structure is divided into abstract classes, refined abstract classes, implementation classes, concrete implementation classes and client code.
Example
/**
* Color abstract class * Class Color
*/
abstract class Color
{
/**
* @return mixed
*/
abstract public function run();
}
/**
* Black * Class Black
*/
class Black extends Color
{
public function run()
{
// TODO: Implement run() method.
return 'black';
}
}
/**
*White*Class White
*/
class White extends Color
{
public function run()
{
// TODO: Implement run() method.
return 'white';
}
}
/**
* Red * Class Red
*/
class Red extends Color
{
public function run()
{
// TODO: Implement run() method.
return 'red';
}
}
/**
* Shape abstract class * Class Shape
*/
abstract class Shape
{
/**
* Color * @var Color
*/
protected $colour;
/**
* Shape constructor.
* @param Color $colour
*/
public function __construct(Colour $colour)
{
$this->colour = $colour;
}
/**
* @return mixed
*/
abstract public function operation();
}
/**
* Round * Class Round
*/
class Round extends Shape
{
/**
* @return mixed|void
*/
public function operation()
{
// TODO: Implement operation() method.
echo $this->colour->run() . 'circle<br>';
}
}
/**
* Rectangle * Class Rectangle
*/
class Rectangle extends Shape
{
/**
* @return mixed|void
*/
public function operation()
{
// TODO: Implement operation() method.
echo $this->colour->run() . 'Rectangle<br>';
}
}
/**
* Square * Class Square
*/
class Square extends Shape
{
/**
* @return mixed|void
*/
public function operation()
{
// TODO: Implement operation() method.
echo $this->colour->run() . 'square<br>';
}
}
// Client code // White round $whiteRound = new Round(new White());
$whiteRound->operation();
// Black Square $blackSquare = new Square(new Black());
$blackSquare->operation();
// Red rectangle $redRectangle = new Rectangle(new Red());
$redRectangle->operation();
//The running result is white circle, black square, red rectangleThe above is an introduction to the PHP bridge mode. I hope it will be helpful to everyone.