This pattern becomes the interface of a class into another which is what customers expect and allows cooperating classes that might not otherwise having incompatible interfaces.
Responsible for providing an interface client class needs to work on its implementation using a class or classes that do not comply with that interface.
The best way to see this pattern through an example. Suppose we have a class Customer performing complex mathematical calculations whose implementation is delegated to another class. Let us assume for example that these complex calculations can be an addition or subtraction. The interface required by this customer class for computations defined in the interface Calculator:
com.roldan.adapter package;
Calculator {public interface
public int sum (int Operator1, Operator2 int) public int subtract
(Operator1 int, int Operator2);}
To carry out these complex calculations are available in class processor that implements:
com.roldan.adapter package;
public class Processor {public static final int ADD = 1;
public static final int SUBTRACT = 2;
realizarOperacion
public static int (int
Operator1 , int
Operator2,
int operation) {
int result = 0;
switch (operation) {case
SUMA
Operator1 + result = Operator2;
break;
case SUBTRACT:
result = Operator1 - Operator2;
break ;
} return result;}
} In this case, despite the class processor performs the operations required by the Customer class, does not implement the interface it needs to use it. However, you can create a class adapter class to adapt the interface processor Calculator:
com.roldan.adapter package;
public class Calculator implements Adapter
{public int sum (int
Operator1,
Operator2 int) {return
Procesador.realizarOperacion (
Operator1,
Operator2,
Procesador.SUMA)
} public int subtract (int
Operator1,
Operator2 int) {return
Procesador.realizarOperacion (
Operator1,
Operator2,
Procesador.RESTA)
}}
Thus, the class Customer can use calculations performed in processor clse through class adapter, which fits the interface that needs the class Calculator Customer .
The following UML diagram reflect the relationships among these classes:
Adapter pattern in Wikipedia
Adapter Pattern in Wikipedia
Uncovered Design Patterns: The Adapter Pattern in Dzone
Adapter Pattern in OODesign.com
Otros patrones de diseƱo
0 comments:
Post a Comment