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 |  * 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 class App  | ||||||
| { | { | ||||||
|     public static void main( String[] args ) |     public static void main( String[] args ) | ||||||
|     { |     { | ||||||
|     	GnomeEngineer engineer = new GnomeEngineer(); |     	GnomeEngineeringManager manager = new GnomeEngineeringManager(); | ||||||
|     	engineer.operateDevice(); |     	manager.operateDevice(); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,8 +2,7 @@ package com.iluwatar; | |||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  *  | ||||||
|  * The interface the client knows how |  * Engineers can operate devices. | ||||||
|  * to use. |  | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| public interface Engineer { | public interface Engineer { | ||||||
|   | |||||||
| @@ -2,9 +2,9 @@ package com.iluwatar; | |||||||
|  |  | ||||||
| /** | /** | ||||||
|  *  |  *  | ||||||
|  * Adapter class. Adapts the interface of the |  * Adapter class. Adapts the interface of the device | ||||||
|  * GoblinGlider into Engineer expected by the |  * (GoblinGlider) into Engineer interface expected  | ||||||
|  * client. |  * by the client (GnomeEngineeringManager). | ||||||
|  * |  * | ||||||
|  */ |  */ | ||||||
| public class GnomeEngineer implements Engineer { | 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 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!"); | ||||||
| 	} | 	} | ||||||
| 	 |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user