140 likes | 301 Views
API Migration Patterns. November 6 th , 2012 Erik Fredericks. Adapter Pattern. Object Adapter Adapter contains instance of class it wraps. http:// en.wikipedia.org /wiki/ File:ObjectAdapter.png. Adapter Pattern. Class Adapter Implements/inherits both expected and pre-existing interface.
E N D
API Migration Patterns November 6th, 2012 Erik Fredericks
Adapter Pattern • Object Adapter • Adapter contains instance of class it wraps http://en.wikipedia.org/wiki/File:ObjectAdapter.png
Adapter Pattern • Class Adapter • Implements/inherits both expected and pre-existing interface http://en.wikipedia.org/wiki/File:ClassAdapter.png
Adapter Sub-types [1] • Layered adapter • Stateful adapter • Delayed instantiation • Wrapping identify map • Terminology
Adapter Sub-types [1] • Layered adapter • Stateful adapter • Delayed instantiation • Wrapping identify map • Terminology Use classic Adapter pattern
Adapter Sub-types [1] • Layered adapter • Stateful adapter • Delayed instantiation • Wrapping identify map • Terminology Completely re-implement by surrogate Surrogate may require multiple combinations of adapteesat runtime
Adapter Sub-types [1] Possible with Adapter sub-types • Layered adapter • Stateful adapter • Delayed instantiation • Wrapping identify map • Terminology Completely re-implement by surrogate Surrogate may require multiple combinations of adapteesat runtime
Layered Adapter • Instead of directly referencing target API types… • Maintain an object of interface type • Alternative mappings uniformly accessed through surrogate • Example • SimpleJListAdapter composes JList and ListModel • ScrollableJListAdapter also inherits from JScrollpane
Stateful Adapter • Delegation not possible • Mapped types don’t agree on features/data • Source API offers richer features than target API • Examples: • Surrogate re-implements missing functionality (a disposed flag) • Adjusting interaction protocols • Client 1 uses a Serializer • Client 2 uses a different protocol • Surrogate collects state and adjusts protocol when dispatching operations
Delayed Instantiation • Surrogate’s lifecycle typically bound to adaptee • API enforces relationships at construction time • Surrogates may need to delay adaptee construction • Example: • JButton <-> Button • JButton can’t create Button until JButton is added to a Container • Parent-child relationship unestablished • Factory design pattern decouples surrogate/adaptee life cycles • Check if instance has been created • Store data temporarily until creation
Wrapping Identity Map • Source APIs may return new object with each call • May be necessary to maintain communication between adapter/adaptee • Query an Identity map for correspondence • Example: • Call to getComponents() : return array of sub-widgets • Identity map queried to get Component surrogate mapped to widget adaptee • If adaptee has no surrogate, one can be created on the fly • Allows for reflective instantiation or hard-coded type mapping
Wrapping Identity Map • Only necessary if surrogate is an object adapter • Class adapters already typed by both APIs and don’t require any translation • Possible issues with memory leaks • Garbage collection procedures must ignore reference in identity map
References • [1] T.T. Bartolomei, K. Czarnecki, and R. Landmmel. Swing to swt and back: Patterns for api migration by wrapping. In Software Maintenance (ICSM), 2010 IEEE International Conference on, pages 1 –10, sept. 2010.