top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Difference between Adapter and Decorator pattern in Java?

+1 vote
329 views
Difference between Adapter and Decorator pattern in Java?
posted Oct 12, 2016 by Arun Angadi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

The purpose of the adapter pattern (aka wrapper) is to convert (or present) the interface of an existing class, into an interface that is compatible with an otherwise incompatible client class. In an adapter class, you are actually changing the interface that is being presented (not the actual class's public methods, but by wrapping the class in another class). No new functionality is created in an adapter class, it merely does as its name implies, makes two things that weren't designed to fit together - fit together.

A decorator class does not change the interface of the original class, but may extend it with additional functionality. The decorator pattern allows for the functionality of the original class to be modified at runtime without subclassing. A decorator is slightly confusing to explain but: it is a subclass of a component class, that takes a concrete instance of the component class in it's constructor, and will will either proxy method calls to the concrete instance that was passed in, or provide it's own implementation.

           Adapter / Wrapper    Decorator
Composes "origin" class True    True
Modifies original interface True    False
Modifies behavior of interface False    True
Proxies method calls    True    True

The adapter class will proxy calls from the interface to the client to the interface of the original class that is being wrapped. The adapter class may perform translation upon the inputs of the client class that are being passed to the original class, and upon the output from the original class that are being returned to the client class.

Often, the use of an adapter, or wrapper, pattern is to bridge new code with legacy code, especially when the legacy code cannot be modified. For instance, a library, or software package that exists in a system that cannot be updated, but needs to be communicated with is a good candidate to be wrapped.

answer Nov 2, 2016 by Karthick.c
Similar Questions
0 votes

Given,
the total levels in a hill pattern (input1),
the weight of the head level (input2), and
the weight increments of each subsequent level (input3),
you are expected to find the total weight of the hill pattern.

"Total levels" represents the number of rows in the pattern.
"Head level" represents the first row.
Weight of a level represents the value of each star (asterisk) in that row.

The hill patterns will always be of the below format, starting with 1 star at head level and increasing 1 star at each level till level N.

*
**
***
****
*****
******

. . .and so on till level N

Example1 -
Given,
the total levels in the hill pattern = 5 (i.e. with 5 rows)
the weight of the head level (first row) = 10
the weight increments of each subsequent level = 2
Then, The total weight of the hill pattern will be calculated as = 10 + (12+12) + (14+14+14) + (16+16+16+16) + (18+18+18+18+18) = 10 + 24 + 42 + 64 + 90 = 230

Example2 -
Given,
the total levels in the hill pattern = 4
the weight of the head level = 1
the weight increments of each subsequent level = 5
Then, Total weight of the hill pattern will be = 1 + (6+6) + (11+11+11) + (16+16+16+16) = 1 + 12 + 33 + 64 = 110

...