Advertisement
rfv123

/34535288/bridge-pattern-double-bridge

Dec 30th, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.11 KB | None | 0 0
  1. <?php // http://stackoverflow.com/questions/34535288/bridge-pattern-double-bridge
  2.  
  3. // I need to draw a shape depending on the shape type, the colour and the wall where is going to be painted on.
  4. // There are two shapes: Rectangle and Circle There are two colours: Red and Blue There are two walls: Wood wall and Concrete wall
  5.  
  6. /*
  7.  * Need the Painter Factory...
  8.  */
  9. $factory = new PainterFactory();
  10.  
  11. /*
  12.  * Create a 'Painter'
  13.  */
  14. $painter = $factory->make('Concrete', 'Rectangle', 'Blue');
  15.  
  16. /*
  17.  * Get the painter to do the action...
  18.  */
  19. echo '<pre>';
  20. echo $painter->paint();
  21. echo '</pre>';
  22.  
  23. exit;
  24.  
  25. /* ---------------------------------------------------------------------------
  26.  *  This makes 'Painters'. It tells a 'Painter' what Wall to paint with a Shape.
  27.  *
  28.  *  It may have to do checks.
  29.  *
  30.  *  This version doesn't.
  31.  */
  32. class PainterFactory {
  33.  
  34.     public function make($wallMaterial, $shapeName, $shapeColor)
  35.     {
  36.         $wall   = new Wall($wallMaterial);
  37.         $shape  = new Shape($shapeName, $shapeColor);
  38.         return new Painter($wall, $shape);
  39.     }
  40. }
  41.  
  42. /* ---------------------------------------------------------------------------
  43.  * This is the thing which does the action of 'paint' the 'Wall' with the 'Shape'.
  44.  *
  45.  * It knows where to find the 'Wall'. It knows that the 'Shape' is to be sprayed on
  46.  * the 'Wall'.
  47.  *
  48.  *   o It does not know about what the Wall is made of.
  49.  *   o It does not know what the Shape is or the color.
  50.  *
  51.  *  It doesn't care.
  52.  */
  53. class Painter {
  54.  
  55.     protected $wall   = null;
  56.     protected $shape  = null;
  57.  
  58.     public function __construct($wall, $shape)
  59.     {
  60.         $this->wall  = $wall;
  61.         $this->shape = $shape;
  62.     }
  63.  
  64.     public function __get($property)
  65.     {
  66.         return $this->$property;
  67.     }
  68.  
  69.     public function paint()
  70.     {
  71.         return 'Painting on: '. PHP_EOL
  72.                 . $this->wall .' was painted with: '. $this->shape . PHP_EOL;
  73.     }
  74. }
  75.  
  76. /* ---------------------------------------------------------------------------
  77.  * This is a surface to be 'painted on'. It doesn't  care how it happens
  78.  * or who does it or why.
  79.  *
  80.  * It is made of a material
  81.  */
  82. class Wall {
  83.  
  84.     protected $material = null;
  85.  
  86.     public function __construct($material) {
  87.         $this->material = $material;
  88.     }
  89.  
  90.     public function __get($property)
  91.     {
  92.         return $this->$property;
  93.     }
  94.  
  95.     public function __toString()
  96.     {
  97.         return ' Wall: Made of: '. $this->material . PHP_EOL;
  98.     }
  99. }
  100.  
  101. /* ---------------------------------------------------------------------------
  102.  * This 'sprays' itself when asked... It has a color and name of the shape.
  103.  * It doesn't know or care about where or why
  104.  */
  105. class Shape {
  106.  
  107.     protected $name = null;
  108.     protected $color  = null;
  109.  
  110.     public function __construct($shape, $color) {
  111.         $this->name = $shape;
  112.         $this->color = $color;
  113.     }
  114.  
  115.     public function __get($property)
  116.     {
  117.         return $this->$property;
  118.     }
  119.  
  120.     public function __toString()
  121.     {
  122.         return 'Shape: '. $this->name .' of Color: '. $this->color . PHP_EOL;
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement