Improved the adapter pattern example. Updated the class diagram.
This commit is contained in:
		
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 33 KiB | 
| @@ -3,14 +3,15 @@ package com.iluwatar; | ||||
| /** | ||||
|  *  | ||||
|  * Adapter (GnomeEngineer) converts the interface of the | ||||
|  * target class (GoblinGlider) into suitable one. | ||||
|  * target class (GoblinGlider) into suitable one expected | ||||
|  * by the client (GnomeEngineeringManager). | ||||
|  * | ||||
|  */ | ||||
| public class App  | ||||
| { | ||||
|     public static void main( String[] args ) | ||||
|     { | ||||
|     	GnomeEngineer engineer = new GnomeEngineer(); | ||||
|     	engineer.operateDevice(); | ||||
|     	GnomeEngineeringManager manager = new GnomeEngineeringManager(); | ||||
|     	manager.operateDevice(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -2,8 +2,7 @@ package com.iluwatar; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * The interface the client knows how | ||||
|  * to use. | ||||
|  * Engineers can operate devices. | ||||
|  * | ||||
|  */ | ||||
| public interface Engineer { | ||||
|   | ||||
| @@ -2,9 +2,9 @@ package com.iluwatar; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Adapter class. Adapts the interface of the | ||||
|  * GoblinGlider into Engineer expected by the | ||||
|  * client. | ||||
|  * Adapter class. Adapts the interface of the device | ||||
|  * (GoblinGlider) into Engineer interface expected  | ||||
|  * by the client (GnomeEngineeringManager). | ||||
|  * | ||||
|  */ | ||||
| public class GnomeEngineer implements Engineer { | ||||
|   | ||||
| @@ -0,0 +1,21 @@ | ||||
| package com.iluwatar; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * GnomeEngineering manager uses Engineer to | ||||
|  * operate devices. | ||||
|  * | ||||
|  */ | ||||
| public class GnomeEngineeringManager implements Engineer { | ||||
|  | ||||
| 	private Engineer engineer; | ||||
| 	 | ||||
| 	public GnomeEngineeringManager() { | ||||
| 		engineer = new GnomeEngineer(); | ||||
| 	} | ||||
| 	 | ||||
| 	@Override | ||||
| 	public void operateDevice() { | ||||
| 		engineer.operateDevice(); | ||||
| 	} | ||||
| } | ||||
| @@ -2,21 +2,20 @@ package com.iluwatar; | ||||
|  | ||||
| /** | ||||
|  *  | ||||
|  * Adaptee class. | ||||
|  * Device class (adaptee in the pattern). | ||||
|  * | ||||
|  */ | ||||
| public class GoblinGlider { | ||||
|  | ||||
| 	public void attachGlider() { | ||||
| 		System.out.println("glider attached"); | ||||
| 		System.out.println("Glider attached."); | ||||
| 	} | ||||
| 	 | ||||
| 	public void gainSpeed() { | ||||
| 		System.out.println("gaining speed"); | ||||
| 		System.out.println("Gaining speed."); | ||||
| 	} | ||||
| 	 | ||||
| 	public void takeOff() { | ||||
| 		System.out.println("lift-off!"); | ||||
| 		System.out.println("Lift-off!"); | ||||
| 	} | ||||
| 	 | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user