Copyright © 2002 Systek The Adapter Pattern Overview And Experience Report Johannes Brodwall, Systek AS
Copyright © 2002 Systek Contents Introduction to the adapter pattern A DOM to JTree adapter A logical to physical presentation adapter A gateway Lessons learned
Copyright © 2002 Systek Why adapter It’s simple It appears in most programs It’s the pattern I have identified in the most number of instances in my work
Copyright © 2002 Systek What is an adapter? Add an interface implementation in front of an existing class Adapt an existing class to a new environment
Copyright © 2002 Systek DOM to JTree Adapt W3C DOM XML Tree model to Java JTree interface The full code for this example is available at /johannes/code/ /johannes/code/
Copyright © 2002 Systek Code Highlights public class DomTreeModel implements javax.swing.tree.TreeModel { public Object getChild(Object parent, int index) { return((Node)parent).getChildNodes().item(index); } public int getIndexOfChild(Object parent, Object child) { NodeList children = ((Node)parent).getChildNodes(); for ( int i=0; i<children.getLength(); i++ ) if ( children.item(i).equals(child) ) return i; return -1; } public void setRoot(TreeNode newRoot) { throw new UnsupportedOperationException("DomTreeModel controls its own root!"); } public void addTreeModelListener(TreeModelListener listener) {} org.w3c.dom.Document document; }
Copyright © 2002 Systek What is m-Commerce? Commerce Server Merchant Financial Institution Customer
Copyright © 2002 Systek Telenor m-Commerce Different kinds of user channels: SMS users – communicate through an SMS Gateway STK Dialog uses – communicate through a Wireless Information Gateway All user channels should be able to implement the following: Send an offer to purchase an item Receive an answer Send a receipt Send an error This our Adapter Target interface
Copyright © 2002 Systek Adapter: UserConnection
Copyright © 2002 Systek Adapter: UserConnection sendOffer(String text, String callbackId) Sent by SMS or STK Dialog If SMS: “Send OK to … to accept” If STK Dialog: Presented as menus sendReceipt(String text) If STK: Send message “Receipt will come as SMS” Send Receipt as SMS sendError(String text) If STK: Present error in STK If SMS: Send SMS to user
Copyright © 2002 Systek Example II: Gateway Problem: Not all merchants were happy with the Commerce Server interface For some merchants, we implemented a custom adapter for them The Target interface is chosen by merchant to be one that would work better with them The Adaptee is the Commerce Server interface
Copyright © 2002 Systek Adaptee: Commerce Server interface HTTP GET with arguments as HTTP parameters First: Final Offer Sends offer to user Blocks until user answers Second: Receipt Issue Sends receipt to user Verifies that merchant has accepted payment
Copyright © 2002 Systek Target interface HTTP Post with XML documents Asynchronous Implicit receipt Possible rollback after the fact No rollback from merchant means confirm
Copyright © 2002 Systek The Adapter
Copyright © 2002 Systek Lessons learned Adapter is very useful and very common The Target interface does not need to support the same time constrains as the Adoptee The Adapter pattern applies on several logical levels Individual classes Internal module divisions Standalone processes
Copyright © 2002 Systek Lessons from the DOMTreeAdapter Three options Populate an entirely new JTree Adapt org.w3c.dom.Node to javax.swing.tree.TreeNode Adapt org.w3c.dom.Document to javax.swing.tree.TreeModel Node – TreeNode adaption seemed best at first, but Document – TreeModel turned out to be the best
Copyright © 2002 Systek Mapping Hierarchies Very hard to go from Adaptee hierarchy back to Adapter hierarchy What should treechild1.getParent() return?