Design Patterns - 04 Adapter and Facade Pattern


Published on

Fourth presentation of a multi part series covering design patterns. This one is about adapter and facade design patterns.

Published in: Design

Design Patterns - 04 Adapter and Facade Pattern

  1. 1. 04 Adapter and Façade Pattern<br />Design Patterns<br />© Prafulla Paraskar 2010<br />
  2. 2. Adapter<br />Structural Design Pattern<br />© Prafulla Paraskar 2010<br />
  3. 3. Definition<br />The adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface. (wikipedia)<br />© Prafulla Paraskar 2010<br />
  4. 4. Adapter Pattern – Explained (1/2)<br />© Prafulla Paraskar 2010<br />
  5. 5. Adapter Pattern – Explained (2/2)<br /> An adapter allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface.<br />The adapter translates calls to its interface into calls to the original interface, and the amount of code necessary to do this is typically small.<br />The adapter is also responsible for transforming data into appropriate forms.<br />© Prafulla Paraskar 2010<br />
  6. 6. Adapter Pattern – Types of Adapters<br />Class adapter<br />Simple and versatile, invisible to the client.<br />Object adapter<br />Extensible to subclasses of the adapter.<br />Two-way adapter<br />Enables different clients to view an object differently.<br />Pluggable adapter<br />Presence of adapter is transparent; it can be put in and taken out. Several adapter can be active.<br />© Prafulla Paraskar 2010<br />
  7. 7. Adapter Pattern – UML Diagram<br />© Prafulla Paraskar 2010<br />
  8. 8. Adapter Pattern – Real World Samples<br />AC adaptors<br />Car charger for mobile phones<br />COM InterOp in .NET<br />SQLAdapter in .NET<br />© Prafulla Paraskar 2010<br />
  9. 9. Adapter Pattern – Guidelines<br />You have :<br />A domain specific interface.<br />A Class to connect to with a mismatching interface.<br />You Want :<br />Create a reusable class to cooperate with yet-to-be-built classes.<br />Change the names of methods as called and as implemented.<br />Support different sets of methods for different purposes.<br />© Prafulla Paraskar 2010<br />
  10. 10. Facade<br />Structural Design Pattern<br />© Prafulla Paraskar 2010<br />
  11. 11. Definition<br />A facade is an object that provides a simplified interface to a larger body of code, such as a class library (wikipedia).<br />© Prafulla Paraskar 2010<br />
  12. 12. Façade Pattern – Explained (1/2)<br />© Prafulla Paraskar 2010<br />
  13. 13. Façade Pattern – Explained (2/2)<br />The name is by analogy to an architectural facade.<br />A facade can:<br />make a software library easier to use and understand and test, since the facade has convenient methods for common tasks.<br />make code that uses the library more readable, for the same reason.<br />reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system.<br />wrap a poorly-designed collection of APIs with a single well-designed API (as per task needs).<br />© Prafulla Paraskar 2010<br />
  14. 14. Façade Pattern – Types of Adapters<br />Opaque<br />Subsystem operations can only be called through the Façade.<br />Transparent<br />Subsystem operations can be called directly as well as through the Façade.<br />Singleton<br />Only one instance of the Façade is meaningful.<br />© Prafulla Paraskar 2010<br />
  15. 15. Façade Pattern – UML Diagram<br />© Prafulla Paraskar 2010<br />
  16. 16. Façade Pattern – Real World Samples<br />Wedding planners / Event management.<br />DirectX / OpenGL<br />Online bill payment<br />Macros in MS Office<br />© Prafulla Paraskar 2010<br />
  17. 17. Façade Pattern – Guidelines<br />When:<br />A system has several identifiable subsystems and:<br />The abstractions and implementations of a subsystem are tightly coupled.<br />The system evolves and gets more complex, but early adapters might want to retain their simple views.<br />You want to provide alternative novice, intermediate and power user interfaces.<br />There is a need for an entry point to each level of layered software. <br />© Prafulla Paraskar 2010<br />
  18. 18. Thank you!<br /><br />Presentation by Prafulla Paraskar<br />© 2010 S1 India Pvt. Ltd.<br />Presentation by Prafulla Paraskar<br />© 2010 Prafulla Paraskar<br />