CSC/ECE 517 Fall 2012/ch2a 2w16 dp: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
(Created page with "= Adapter Design Pattern = In computer programming, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one in...")
 
No edit summary
Line 10: Line 10:


Adapter pattern is also used to translate calls made by a user to any wrapped interface. e.g. for efficiency, a data structure may be used to store multiple values in it. but it becomes difficult for the user to understand them and edit them, so the adapter class may simplify the call to the wrapped class, and provide users with only the variables/methods that he might be interested in. Another scenario where adapter patterns may be used is that in a system, different users may be given different access level depending of their classification, so instead of changing the whole design, a number of adapter classes will do the work, instead of modifying the whole design.
Adapter pattern is also used to translate calls made by a user to any wrapped interface. e.g. for efficiency, a data structure may be used to store multiple values in it. but it becomes difficult for the user to understand them and edit them, so the adapter class may simplify the call to the wrapped class, and provide users with only the variables/methods that he might be interested in. Another scenario where adapter patterns may be used is that in a system, different users may be given different access level depending of their classification, so instead of changing the whole design, a number of adapter classes will do the work, instead of modifying the whole design.
This added layer given between two interfaces decreases cohesion amongst modules. if there are lots of interfaces interacting with an interface(one to many), adding an adapter class in between is advisable, as change in the base class will not introduce change in all caller classes, but only in adapter class.
The main use of this pattern is when a class that you need to use doesn't meet the requirements of an interface. Adapters are common across Eclipse plug-ins. For a particular object to contribute to the Properties view, adapters are used display the objects data. The view itself doesn't need to know anything about the object the it is displaying properties for.
Adapter pattern is also very useful during development phase of any software. since the high level modules need not know the details of the low level modules, after a common interface for the adapter class is decided, work on the low level and the high level modules can be started in parallel.
===Properties of adapter pattern===
'''Delegation:''' Delegation is basically delegating a task to another part of program. Adapter pattern uses this property as described in the above example,, wrapper class may perform some operations  on the values returned by wrapped class before forwarding it to the caller method. The advantage of this is that the view has no idea about how the model/controller is working. If we have to make changes to any of the business logic, we need not change the view. we only need to make appropriate changes to the wrapper class.
'''Dependency Inversion Principle'''
This is another principle that adapter pattern uses. It refers to a specific form of decoupling relationship dependency from high-level modules to low level modules are inverted so that high level modules are independent of the implementation of low level modules. There are two important ideas this principle is based on:
A) High level modules should not depend on low level modules, and vice versa. Both of these should depend on the abstraction.
This means that there should be middle layer in between that acts a common interface between the two levels of modules. this provides a very good way of decoupling them and thus changes in any level need not lead to changes at the other level.  This will also lead to re-use principle as low-level as well as high level modules can be made generic and not application specific.

Revision as of 20:40, 26 October 2012

Adapter Design Pattern

In computer programming, 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.

Introduction

An adapter allows classes to work together that normally could not because of incompatible interfaces.It “wraps” its own interface around the interface of a pre-existing class. It may also translate data formats from the caller to a form needed by the callee.

Adapter pattern is also used to translate calls made by a user to any wrapped interface. e.g. for efficiency, a data structure may be used to store multiple values in it. but it becomes difficult for the user to understand them and edit them, so the adapter class may simplify the call to the wrapped class, and provide users with only the variables/methods that he might be interested in. Another scenario where adapter patterns may be used is that in a system, different users may be given different access level depending of their classification, so instead of changing the whole design, a number of adapter classes will do the work, instead of modifying the whole design.

This added layer given between two interfaces decreases cohesion amongst modules. if there are lots of interfaces interacting with an interface(one to many), adding an adapter class in between is advisable, as change in the base class will not introduce change in all caller classes, but only in adapter class.

The main use of this pattern is when a class that you need to use doesn't meet the requirements of an interface. Adapters are common across Eclipse plug-ins. For a particular object to contribute to the Properties view, adapters are used display the objects data. The view itself doesn't need to know anything about the object the it is displaying properties for.

Adapter pattern is also very useful during development phase of any software. since the high level modules need not know the details of the low level modules, after a common interface for the adapter class is decided, work on the low level and the high level modules can be started in parallel.

Properties of adapter pattern

Delegation: Delegation is basically delegating a task to another part of program. Adapter pattern uses this property as described in the above example,, wrapper class may perform some operations on the values returned by wrapped class before forwarding it to the caller method. The advantage of this is that the view has no idea about how the model/controller is working. If we have to make changes to any of the business logic, we need not change the view. we only need to make appropriate changes to the wrapper class.

Dependency Inversion Principle This is another principle that adapter pattern uses. It refers to a specific form of decoupling relationship dependency from high-level modules to low level modules are inverted so that high level modules are independent of the implementation of low level modules. There are two important ideas this principle is based on: A) High level modules should not depend on low level modules, and vice versa. Both of these should depend on the abstraction.

This means that there should be middle layer in between that acts a common interface between the two levels of modules. this provides a very good way of decoupling them and thus changes in any level need not lead to changes at the other level. This will also lead to re-use principle as low-level as well as high level modules can be made generic and not application specific.