Reformat Adapter Pattern - Issue #224
This commit is contained in:
parent
c0c21ebd91
commit
95c16200e7
@ -2,30 +2,29 @@ package com.iluwatar.adapter;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* An adapter helps two incompatible interfaces to work together. This is the real
|
* An adapter helps two incompatible interfaces to work together. This is the real world definition
|
||||||
* world definition for an adapter. Interfaces may be incompatible but the inner
|
* for an adapter. Interfaces may be incompatible but the inner functionality should suit the need.
|
||||||
* functionality should suit the need. The Adapter design pattern allows otherwise
|
* The Adapter design pattern allows otherwise incompatible classes to work together by converting
|
||||||
* incompatible classes to work together by converting the interface of one class
|
* the interface of one class into an interface expected by the clients.
|
||||||
* into an interface expected by the clients.
|
|
||||||
* <p>
|
* <p>
|
||||||
* There are two variations of the Adapter pattern: The class adapter implements
|
* There are two variations of the Adapter pattern: The class adapter implements the adaptee's
|
||||||
* the adaptee's interface whereas the object adapter uses composition to
|
* interface whereas the object adapter uses composition to contain the adaptee in the adapter
|
||||||
* contain the adaptee in the adapter object. This example uses the object
|
* object. This example uses the object adapter approach.
|
||||||
* adapter approach.
|
|
||||||
* <p>
|
* <p>
|
||||||
* 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 GoblinGlider}) into a suitable one expected by the client ({@link GnomeEngineeringManager}
|
||||||
* ({@link GnomeEngineeringManager}).
|
* ).
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point
|
||||||
* @param args command line args
|
*
|
||||||
*/
|
* @param args command line args
|
||||||
public static void main(String[] args) {
|
*/
|
||||||
Engineer manager = new GnomeEngineeringManager();
|
public static void main(String[] args) {
|
||||||
manager.operateDevice();
|
Engineer manager = new GnomeEngineeringManager();
|
||||||
}
|
manager.operateDevice();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,5 @@ package com.iluwatar.adapter;
|
|||||||
*/
|
*/
|
||||||
public interface Engineer {
|
public interface Engineer {
|
||||||
|
|
||||||
void operateDevice();
|
void operateDevice();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,23 +2,22 @@ package com.iluwatar.adapter;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Adapter class. Adapts the interface of the device ({@link GoblinGlider}) into
|
* Adapter class. Adapts the interface of the device ({@link GoblinGlider}) into {@link Engineer}
|
||||||
* {@link Engineer} interface expected by the client ({@link GnomeEngineeringManager}).
|
* interface expected by the client ({@link GnomeEngineeringManager}).
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class GnomeEngineer implements Engineer {
|
public class GnomeEngineer implements Engineer {
|
||||||
|
|
||||||
private GoblinGlider glider;
|
private GoblinGlider glider;
|
||||||
|
|
||||||
public GnomeEngineer() {
|
public GnomeEngineer() {
|
||||||
glider = new GoblinGlider();
|
glider = new GoblinGlider();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void operateDevice() {
|
|
||||||
glider.attachGlider();
|
|
||||||
glider.gainSpeed();
|
|
||||||
glider.takeOff();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void operateDevice() {
|
||||||
|
glider.attachGlider();
|
||||||
|
glider.gainSpeed();
|
||||||
|
glider.takeOff();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,14 +7,14 @@ package com.iluwatar.adapter;
|
|||||||
*/
|
*/
|
||||||
public class GnomeEngineeringManager implements Engineer {
|
public class GnomeEngineeringManager implements Engineer {
|
||||||
|
|
||||||
private Engineer engineer;
|
private Engineer engineer;
|
||||||
|
|
||||||
public GnomeEngineeringManager() {
|
public GnomeEngineeringManager() {
|
||||||
engineer = new GnomeEngineer();
|
engineer = new GnomeEngineer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void operateDevice() {
|
public void operateDevice() {
|
||||||
engineer.operateDevice();
|
engineer.operateDevice();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,15 +7,15 @@ package com.iluwatar.adapter;
|
|||||||
*/
|
*/
|
||||||
public class GoblinGlider {
|
public class GoblinGlider {
|
||||||
|
|
||||||
public void attachGlider() {
|
public void attachGlider() {
|
||||||
System.out.println("Glider attached.");
|
System.out.println("Glider attached.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void gainSpeed() {
|
public void gainSpeed() {
|
||||||
System.out.println("Gaining speed.");
|
System.out.println("Gaining speed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void takeOff() {
|
public void takeOff() {
|
||||||
System.out.println("Lift-off!");
|
System.out.println("Lift-off!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ import com.iluwatar.adapter.App;
|
|||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
String[] args = {};
|
||||||
App.main(args);
|
App.main(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user