Adapter Pattern Abhijit Hiremagalur 200603

936 views

Published on

Published in: Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
936
On SlideShare
0
From Embeds
0
Number of Embeds
32
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Adapter Pattern Abhijit Hiremagalur 200603

  1. 1. Adapter Design Pattern Abhijit Hiremagalur abhijith@ibsglobalweb.com Internet Business Systems (IBS) 1
  2. 2. Agenda • Design Patterns, a brief intro • Adapter Pattern, what & why? • When? • Participants, collaborations & some UML • An example • Other issues 2
  3. 3. Design Patterns • In software engineering, a general repeatable solution, to a commonly- occurring problem in software design [Wikipedia] • A computer science book proposing standard solutions and naming conventions to common problems in software design by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, known as the quot;Gang of Fourquot; or simply quot;GoFquot; [Wikipedia] 3
  4. 4. GoF Design Pattern Types • Creational • Structural • Behavioral 4
  5. 5. Adapter - What & Why? • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces • Also known as Wrapper • Is a structural design pattern • Use it if a class intended for reuse isn’t reusable only because its interface doesn’t match the domain-specific interface 5
  6. 6. Applicability - When? • To use an existing class whose interface does not match the one needed • To create a reusable class that co-operates with unrelated or unforeseen classes (yikes!) • (Object Adapter) To adapt several subclasses using a single adapter, adapt interface of parent class 6
  7. 7. 2 Flavours • Object Adapter • Class Adapter 7
  8. 8. Participants & Collaborations • Target - defines the domain-specific interface that Client uses • Client - collaborates with objects conforming to the Target interface • Adaptee - defines an existing interface that needs adapting • Adapter - adapts the interface of Adaptee to the Target interface • Clients call operations on an Adapter instance. Adapter calls Adaptee operations that carry out the request. 8
  9. 9. Object Adapter • Adapter contains an instance of the class it wraps • Makes calls to the physical instance of the wrapped object 9
  10. 10. Class Adapter • Uses multiple inheritance • Created using interfaces of both expected and existing • Object Adapter is more common as popular languages (e.g. Java) don’t support true multiple inheritance 10
  11. 11. An Example • Whiteboard time! 11
  12. 12. Consequences - Pros • A Class Adapter uses inheritance so: • only adapts a class and all its parents, not all its subclasses • lets Adapter override some of Adaptee’s behavior • does not introduce an additional pointer indirection (is this really an issue?) • An Object Adapter uses composition so: • lets single Adapter work with many Adaptees 12
  13. 13. Consequences - Cons • A Class Adapter uses inheritance so: • Requires sub-classing, difficult if you’re using a language that only supports single inheritance • Less flexible than Object Adapter • An Object Adapter uses composition so: • Harder to override Adaptee behaviour (really?) • Requires more code to implement properly 13
  14. 14. Other Issues • How much adapting does Adapter do? • Pluggable adapters • Using two-way adapters to provide transparency 14
  15. 15. How much to adapting does Adapter do? • Depends on how similar the Target interface is to the Adaptee’s • May just map an operation to another • May have to work hard if Target operation doesn’t have a comparable operation in the Adaptee 15
  16. 16. Pluggable Adapter • A class is more reusable when you minimize the assumptions other classes must make to use it • Used where we know in advance we will adapt the component to other interfaces • Common in Smalltalk (Does anyone have experience with Adapters in Smalltalk?) 16
  17. 17. Two-Way Adapter • Address problems with Adapters to provide transparency • An adapted object no longer conforms to the Adaptee interface • Useful when two different clients need to view an object differently 17
  18. 18. Related Patterns • Bridge - Similar structure, different intent - Seperate an interface from its implementation so they can vary independently - Adapter is meant to change to interface of an existing object • Decorator - Enhances another object without changing its interface - More transparent than Adapter - Supports recursive composition, not possible with Adapter • Proxy - Surrogate for another object - Does not change its interface 18
  19. 19. References • Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides • http://en.wikipedia.org/wiki/Design_pattern_(computer_science) • http://en.wikipedia.org/wiki/Adapter_pattern • http://www.industriallogic.com/papers/learning.html 19
  20. 20. Let’s Discuss! • Would you ever create an Adapter that has the same interface as the object which it adapts? Would your Adapter then be a Proxy? [Industrial Logic] • Class Adapters - does anybody have any experience with these? Opinions? • Pluggable Adapters, anyone used them? • Comparison with other structural patterns • (shamelessPlug := IBS is hiring - Let me know if you’d like to know more) 20

×