Monday, June 9, 2014

Adapter Pattern in Java


Simple Note

From reference:
The adapter design pattern is used when you want two different classes with incompatible interfaces to work together. Interfaces may be incompatible but the inner functionality should suit the need. The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.

Program

https://github.com/benbai123/JSP_Servlet_Practice/tree/master/Practice/JAVA/DesignPattern/src/adapter

Run TestMain.java to test.

Assume you hope all calculators implement the interface DesiredCalculator but the PowerOfTwoCalc didn't, and assume it is provided by 3rd party in a jar.
Now you can use Adapter Pattern to create DesiredPowerOfTwoCalculator that implements DesiredCalculator and let PowerOfTwoCalc do real operation inside rather than rewrite all functions yourself.

Reference:

Adapter pattern

No comments:

Post a Comment