Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php // http://stackoverflow.com/questions/34535288/bridge-pattern-double-bridge
- // I need to draw a shape depending on the shape type, the colour and the wall where is going to be painted on.
- // There are two shapes: Rectangle and Circle There are two colours: Red and Blue There are two walls: Wood wall and Concrete wall
- /*
- * Need the Painter Factory...
- */
- $factory = new PainterFactory();
- /*
- * Create a 'Painter'
- */
- $painter = $factory->make('Concrete', 'Rectangle', 'Blue');
- /*
- * Get the painter to do the action...
- */
- echo '<pre>';
- echo $painter->paint();
- echo '</pre>';
- exit;
- /* ---------------------------------------------------------------------------
- * This makes 'Painters'. It tells a 'Painter' what Wall to paint with a Shape.
- *
- * It may have to do checks.
- *
- * This version doesn't.
- */
- class PainterFactory {
- public function make($wallMaterial, $shapeName, $shapeColor)
- {
- $wall = new Wall($wallMaterial);
- $shape = new Shape($shapeName, $shapeColor);
- return new Painter($wall, $shape);
- }
- }
- /* ---------------------------------------------------------------------------
- * This is the thing which does the action of 'paint' the 'Wall' with the 'Shape'.
- *
- * It knows where to find the 'Wall'. It knows that the 'Shape' is to be sprayed on
- * the 'Wall'.
- *
- * o It does not know about what the Wall is made of.
- * o It does not know what the Shape is or the color.
- *
- * It doesn't care.
- */
- class Painter {
- protected $wall = null;
- protected $shape = null;
- public function __construct($wall, $shape)
- {
- $this->wall = $wall;
- $this->shape = $shape;
- }
- public function __get($property)
- {
- return $this->$property;
- }
- public function paint()
- {
- return 'Painting on: '. PHP_EOL
- . $this->wall .' was painted with: '. $this->shape . PHP_EOL;
- }
- }
- /* ---------------------------------------------------------------------------
- * This is a surface to be 'painted on'. It doesn't care how it happens
- * or who does it or why.
- *
- * It is made of a material
- */
- class Wall {
- protected $material = null;
- public function __construct($material) {
- $this->material = $material;
- }
- public function __get($property)
- {
- return $this->$property;
- }
- public function __toString()
- {
- return ' Wall: Made of: '. $this->material . PHP_EOL;
- }
- }
- /* ---------------------------------------------------------------------------
- * This 'sprays' itself when asked... It has a color and name of the shape.
- * It doesn't know or care about where or why
- */
- class Shape {
- protected $name = null;
- protected $color = null;
- public function __construct($shape, $color) {
- $this->name = $shape;
- $this->color = $color;
- }
- public function __get($property)
- {
- return $this->$property;
- }
- public function __toString()
- {
- return 'Shape: '. $this->name .' of Color: '. $this->color . PHP_EOL;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement