Adapter Design Pattern
Total Page:16
File Type:pdf, Size:1020Kb
Adapter Design Pattern • The primary purpose of an adapter is to make usable existing, functional code that does not directly meet the interface requirements of a new application; this is a structural design pattern • There are two main implementation techniques – Class adapter –translates the client’s method calls into calls to existing class methods – Object adapter –forwards a client’s calls to an instance of an existing class • A JTable in Java is an example of using the adapter design pattern Acknowledgements • Materials were borrowed from – Design Patterns in Java by Steven Metsker and William Wake (textbook for course) – Head First Design Patterns by Elisabeth Freeman, Eric Freeman, Bert Bates, and Kathy Sierra – The first example is borrowed from lecture notes on Design Patterns by Brian Malloy at Clemson University Example: A Java Interface for a Stack public interface Stack { public void push(int x); public void pop(); public int top(); boolean empty(); } This example is borrowed from lecture notes on Design Patterns by Brian Malloy at Clemson University The Actual Java Implementation public class FixedArray { public FixedArray() { index = -1; items = new int[10]; } public void insert(int x) { items[++index] = x; } public void removeLast() { --index; } public boolean empty() { return index == -1; } public int top() { return items[index]; } private int index; private int [] items; } Using the Java Stack public class StackTest { public static void main(String [] args) { MyStack stk = new MyStack(); stk.push(99); stk.push(7); stk.push(23); while ( !stk.empty() ) { System.out.println(stk.top()); stk.pop(); } } } The Adapter for a Java Stack using an object adapter public class MyStack implements Stack { public MyStack() { array = new FixedArray(); } public void push(int x) { array.insert(x); } public void pop() { array.removeLast(); } public int top() { return array.top(); } public boolean empty() { return array.empty(); } private FixedArray array; } Object Adapter The Adapter for a Java Stack using a class adpater public class MyStack extends FixedArray implements Stack { public MyStack() { super(); } public void push(int x) { insert(x); } public void pop() { removeLast(); } public int top() { return super.top(); } public boolean empty() { return super.empty(); } } Adapting old Java to new Java Enumeration has been present in Java since the beginning (1.0) Iterator was added in Java 1.2 Here is how we can use an adapter We use the object adapter approach The Implementation Class Adapter • The client is using a known (“required”) interface • An existing class does the same work but the methods are named differently • The class adapter is a new class that has the existing class as a parent and at the same time implements the interface • The class adapter calls the “usefulMethod” from its parent to do the work and returns this result to the client Rocket Adapter • Your task is to implement the interface RocketSim • There is an existing class that provides the necessary information, but the method calls don’t match • Design the adapter class OozinozRocket to implement the interface using the existing code Solution 3.1 Challenge 3.2 3.2 Solution Object Adapter • The client calls directly a required method from a required class; no interface is involved • The object adapter is a subclass of the RequiredClass; it will actually implement the required method • The new class will create an instance of the ExistingClass and then call the usefulMethod to do the work • The results are returned back as if the requiredMethod had done the work directly • Assume an existing class called PhysicalRocket can carry out the desired work Solution 3.3 Solution 3.4 JTable in Swing • The JTable API in Swing implements the TableModel interface; the AbstractTableModel is an abstract class in Java the implements the TableModel interface • The user of the AbstractTableModel is only required to implement three methods: getColumnCount, getRowCount, and getValueAt RocketTableModel • The table will display the name, price, and apogee of each rocket • Instances of the Rocket class will contain the necessary data • The RocketTableModel will implement: getColumnCount, getRowCount, and getValueAt RocketTableModel Code Solution 3.5 the main program MouseAdapter • The mouse adapter class implements all the methods in the MouseListerner interface • HOWEVER, all these methods are EMPTY and nonfunctional • The user must implement the methods needed for the particular application Challenge 3.6 • Are you applying the Adapter pattern when you use the MouseAdapter class? Explain how (if yes) or why not (if no). Solution 3.6.