Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2002 Systek The Adapter Pattern Overview And Experience Report Johannes Brodwall, Systek AS.

Similar presentations


Presentation on theme: "Copyright © 2002 Systek The Adapter Pattern Overview And Experience Report Johannes Brodwall, Systek AS."— Presentation transcript:

1 Copyright © 2002 Systek ASbrodwall@systek.no The Adapter Pattern Overview And Experience Report Johannes Brodwall, Systek AS

2 Copyright © 2002 Systek ASbrodwall@systek.no Contents Introduction to the adapter pattern A DOM to JTree adapter A logical to physical presentation adapter A gateway Lessons learned

3 Copyright © 2002 Systek ASbrodwall@systek.no 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

4 Copyright © 2002 Systek ASbrodwall@systek.no What is an adapter? Add an interface implementation in front of an existing class Adapt an existing class to a new environment

5 Copyright © 2002 Systek ASbrodwall@systek.no DOM to JTree Adapt W3C DOM XML Tree model to Java JTree interface The full code for this example is available at http://brodwall.com /johannes/code/ http://brodwall.com /johannes/code/

6 Copyright © 2002 Systek ASbrodwall@systek.no 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; }

7 Copyright © 2002 Systek ASbrodwall@systek.no What is m-Commerce? Commerce Server Merchant Financial Institution Customer

8 Copyright © 2002 Systek ASbrodwall@systek.no 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

9 Copyright © 2002 Systek ASbrodwall@systek.no Adapter: UserConnection

10 Copyright © 2002 Systek ASbrodwall@systek.no 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

11 Copyright © 2002 Systek ASbrodwall@systek.no 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

12 Copyright © 2002 Systek ASbrodwall@systek.no 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

13 Copyright © 2002 Systek ASbrodwall@systek.no Target interface HTTP Post with XML documents Asynchronous Implicit receipt Possible rollback after the fact No rollback from merchant means confirm

14 Copyright © 2002 Systek ASbrodwall@systek.no The Adapter

15 Copyright © 2002 Systek ASbrodwall@systek.no 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

16 Copyright © 2002 Systek ASbrodwall@systek.no 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

17 Copyright © 2002 Systek ASbrodwall@systek.no Mapping Hierarchies Very hard to go from Adaptee hierarchy back to Adapter hierarchy What should treechild1.getParent() return?


Download ppt "Copyright © 2002 Systek The Adapter Pattern Overview And Experience Report Johannes Brodwall, Systek AS."

Similar presentations


Ads by Google