From aefe6aa5be826e9eba752e0bbc88800c97d8f3ca Mon Sep 17 00:00:00 2001 From: Trismegiste Date: Sun, 8 Sep 2013 20:58:41 +0200 Subject: [PATCH] fixing a DIP violation in the implementation Object Adapter pattern --- .../GoF/Structural/Adapter/ObjectAdapter/Adapter.php | 9 ++++++--- .../GoF/Structural/Adapter/ObjectAdapter/Client.php | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Whizark/DesignPatterns/GoF/Structural/Adapter/ObjectAdapter/Adapter.php b/src/Whizark/DesignPatterns/GoF/Structural/Adapter/ObjectAdapter/Adapter.php index 1477849..a5c8499 100644 --- a/src/Whizark/DesignPatterns/GoF/Structural/Adapter/ObjectAdapter/Adapter.php +++ b/src/Whizark/DesignPatterns/GoF/Structural/Adapter/ObjectAdapter/Adapter.php @@ -23,11 +23,14 @@ class Adapter implements TargetInterface /** * The constructor. + * + * To achieve Dependency Inversion Principle, we need to inject adaptee(s) + * in the constructor of adapter. This is a key feature. */ - public function __construct() + public function __construct(AdapteeA $a, AdapteeB $b) { - $this->adapteeA = new AdapteeA(); - $this->adapteeB = new AdapteeB(); + $this->adapteeA = $a; + $this->adapteeB = $b; } /** diff --git a/src/Whizark/DesignPatterns/GoF/Structural/Adapter/ObjectAdapter/Client.php b/src/Whizark/DesignPatterns/GoF/Structural/Adapter/ObjectAdapter/Client.php index 11c601c..cd2b210 100644 --- a/src/Whizark/DesignPatterns/GoF/Structural/Adapter/ObjectAdapter/Client.php +++ b/src/Whizark/DesignPatterns/GoF/Structural/Adapter/ObjectAdapter/Client.php @@ -5,5 +5,5 @@ require '../../../../../../../vendor/autoload.php'; -$adapter = new Adapter(); +$adapter = new Adapter(new AdapteeA(), new AdapteeB()); $adapter->targetMethod();