diff --git a/adapter/pom.xml b/adapter/pom.xml
index 2284c2974..ddb81441f 100644
--- a/adapter/pom.xml
+++ b/adapter/pom.xml
@@ -14,5 +14,10 @@
- * There are two variations of the Adapter pattern: The class adapter implements the adaptee's + * + *
There are two variations of the Adapter pattern: The class adapter implements the adaptee's * interface whereas the object adapter uses composition to contain the adaptee in the adapter * object. This example uses the object adapter approach. - *
- * The Adapter ({@link GnomeEngineer}) converts the interface of the target class ( + * + *
The Adapter ({@link GnomeEngineer}) converts the interface of the target class ( * {@link GoblinGlider}) into a suitable one expected by the client ({@link GnomeEngineeringManager} * ). - * */ public class App { /** - * Program entry point - * + * Program entry point. + * * @param args command line args */ public static void main(String[] args) { - Engineer manager = new GnomeEngineeringManager(); + Engineer manager = new GnomeEngineeringManager(new GnomeEngineer()); manager.operateDevice(); } } diff --git a/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java b/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java index a2faf1e54..ff4ddb617 100644 --- a/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java +++ b/adapter/src/main/java/com/iluwatar/adapter/GnomeEngineeringManager.java @@ -1,20 +1,26 @@ package com.iluwatar.adapter; /** - * * GnomeEngineering manager uses {@link Engineer} to operate devices. - * */ public class GnomeEngineeringManager implements Engineer { private Engineer engineer; public GnomeEngineeringManager() { - engineer = new GnomeEngineer(); + + } + + public GnomeEngineeringManager(Engineer engineer) { + this.engineer = engineer; } @Override public void operateDevice() { engineer.operateDevice(); } + + public void setEngineer(Engineer engineer) { + this.engineer = engineer; + } } diff --git a/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java b/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java new file mode 100644 index 000000000..866bfb968 --- /dev/null +++ b/adapter/src/test/java/com/iluwatar/adapter/AdapterPatternTest.java @@ -0,0 +1,68 @@ +package com.iluwatar.adapter; + +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; + +import java.util.HashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +/** + * An adapter helps two incompatible interfaces to work together. This is the real world definition + * for an adapter. Interfaces may be incompatible but the inner functionality should suit the need. + * The Adapter design pattern allows otherwise incompatible classes to work together by converting + * the interface of one class into an interface expected by the clients. + * + *
There are two variations of the Adapter pattern: + * The class adapter implements the adaptee's + * interface whereas the object adapter uses composition to contain the adaptee in the adapter + * object. This example uses the object adapter approach. + * + *
The Adapter ({@link GnomeEngineer}) converts the interface
+ * of the target class ({@link GoblinGlider}) into a suitable one expected by
+ * the client ({@link GnomeEngineeringManager}
+ * ).
+ */
+public class AdapterPatternTest {
+
+ private Map