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
|
||||
* 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.
|
||||
* 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.
|
||||
* <p>
|
||||
* 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.
|
||||
* 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.
|
||||
* <p>
|
||||
* The Adapter ({@link GnomeEngineer}) converts the interface of the target class
|
||||
* ({@link GoblinGlider}) into a suitable one expected by the client
|
||||
* ({@link GnomeEngineeringManager}).
|
||||
* 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
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Engineer manager = new GnomeEngineeringManager();
|
||||
manager.operateDevice();
|
||||
}
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Engineer manager = new GnomeEngineeringManager();
|
||||
manager.operateDevice();
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,5 @@ package com.iluwatar.adapter;
|
||||
*/
|
||||
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
|
||||
* {@link Engineer} interface expected by the client ({@link GnomeEngineeringManager}).
|
||||
* Adapter class. Adapts the interface of the device ({@link GoblinGlider}) into {@link Engineer}
|
||||
* interface expected by the client ({@link GnomeEngineeringManager}).
|
||||
*
|
||||
*/
|
||||
public class GnomeEngineer implements Engineer {
|
||||
|
||||
private GoblinGlider glider;
|
||||
private GoblinGlider glider;
|
||||
|
||||
public GnomeEngineer() {
|
||||
glider = new GoblinGlider();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operateDevice() {
|
||||
glider.attachGlider();
|
||||
glider.gainSpeed();
|
||||
glider.takeOff();
|
||||
}
|
||||
public GnomeEngineer() {
|
||||
glider = new GoblinGlider();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operateDevice() {
|
||||
glider.attachGlider();
|
||||
glider.gainSpeed();
|
||||
glider.takeOff();
|
||||
}
|
||||
}
|
||||
|
@ -7,14 +7,14 @@ package com.iluwatar.adapter;
|
||||
*/
|
||||
public class GnomeEngineeringManager implements Engineer {
|
||||
|
||||
private Engineer engineer;
|
||||
private Engineer engineer;
|
||||
|
||||
public GnomeEngineeringManager() {
|
||||
engineer = new GnomeEngineer();
|
||||
}
|
||||
public GnomeEngineeringManager() {
|
||||
engineer = new GnomeEngineer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void operateDevice() {
|
||||
engineer.operateDevice();
|
||||
}
|
||||
@Override
|
||||
public void operateDevice() {
|
||||
engineer.operateDevice();
|
||||
}
|
||||
}
|
||||
|
@ -7,15 +7,15 @@ package com.iluwatar.adapter;
|
||||
*/
|
||||
public class GoblinGlider {
|
||||
|
||||
public void attachGlider() {
|
||||
System.out.println("Glider attached.");
|
||||
}
|
||||
public void attachGlider() {
|
||||
System.out.println("Glider attached.");
|
||||
}
|
||||
|
||||
public void gainSpeed() {
|
||||
System.out.println("Gaining speed.");
|
||||
}
|
||||
public void gainSpeed() {
|
||||
System.out.println("Gaining speed.");
|
||||
}
|
||||
|
||||
public void takeOff() {
|
||||
System.out.println("Lift-off!");
|
||||
}
|
||||
public void takeOff() {
|
||||
System.out.println("Lift-off!");
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import com.iluwatar.adapter.App;
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user