Skip to content

Instantly share code, notes, and snippets.

@hastinbe
Created September 13, 2019 01:47
Show Gist options
  • Save hastinbe/2d2177a2e6e0b53181a898a12917b3e5 to your computer and use it in GitHub Desktop.
Save hastinbe/2d2177a2e6e0b53181a898a12917b3e5 to your computer and use it in GitHub Desktop.
Bridge Pattern Example #php #design-patterns #bridge-pattern
<?php
abstract class Window
{
protected $impl;
public function __construct(WindowImpl $impl)
{
$this->setWindowImpl($impl);
}
public function setWindowImpl($impl)
{
$this->impl = $impl;
}
}
class ApplicationWindow extends Window
{
public function __construct(WindowImpl $impl)
{
parent::__construct($impl);
}
public function drawLine($x, $y)
{
$this->impl->drawLine($x, $y);
}
}
abstract class WindowImpl
{
public abstract function drawLine($x, $y);
}
class X11Window extends WindowImpl
{
public function drawLine($x, $y)
{
echo "X11Window->drawLine($x, $y)\n";
}
}
class MotifWindow extends WindowImpl
{
public function drawLine($x, $y)
{
echo "MotifWindow->drawLine($x, $y)\n";
}
}
class WindowFactory
{
public static function getWindowImpl()
{
return new X11Window();
}
public static function getApplicationWindow()
{
return new ApplicationWindow(self::getWindowImpl());
}
}
$window = WindowFactory::getApplicationWindow();
$window->drawLine(50, 100);
$window->setWindowImpl(new MotifWindow());
$window->drawLine(50, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment