Friday 25, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Monday, 24 July 2017

Design Patterns - Adapter Pattern

Adapter Pattern

Also known as Wrapper.

Definition

The Adapter pattern is used to translate the interface of one class into another interface. This means that we can make classes work together that couldn't otherwise because of incompatible interfaces. A class adapter uses multiple inheritance (by extending one class and/or implementing one or
more classes) to adapt one interface to another. An object adapter relies on object aggregation.

Where to use

•When you want to use an existing class, and its interface does not match the one you need.
•When you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces.
•When you want to increase transparency of classes.
•When you want to make a pluggable kit.

Benefits

•Highly class reusable.
•Introduces only one object

Drawbacks/consequences

When using Java, Target must be an interface.

Adapter Pattern - Class Diagram


In the class-diagram above:
•A Client class expects a certain interface (called the Target interface)
•An available interface doesn't match the Target interface
•An Adapter class bridges the gap between the Target interface and the available interface
•The available interface is called the Adaptee

Adapter Pattern Example

01.public interface FileManager {
02.public String open(String s);
03.public String close();
04.public String read(int pos, int amount, byte[] data);
05.public String write(int pos, int amount, byte[] data);
06.}
07.import java.util.*;
08.import java.io.*;
09.public class FileManagerUtil {
10.private RandomAccessFile f;
11.public boolean openFile(String fileName) {
12.System.out.println("Opening file: "+fileName);
13.boolean success=true;
14.return success;
15.}
16.public boolean closeFile() {
17.System.out.println("Closing file");
18.boolean success=true;
19.return success;
20.}
21.public boolean writeToFile(String d, long pos, long amount) {
22.System.out.print("Writing "+amount+
23." chars from string: "+d);
24.System.out.println(" to pos: "+pos+" in file");
25.boolean success=true;
26.return success;
27.}
28.public String readFromFile(long pos, long amount) {
29.System.out.print("Reading "+amount+
30." chars from pos: "+pos+" in file");
31.return new String("dynamite");
32.}
33.}
34. 
35.public class FileManagerImpl extends FileManagerUtil implements
36.FileManager {
37.public String close() {
38.return new Boolean(closeFile()).toString();
39.}
40.public String open(String s) {
41.return new Boolean(openFile(s)).toString();
42.}
43.public String read(int pos, int amount, byte[] data) {
44.return readFromFile(pos, amount);
45.}
46.public String write(int pos, int amount, byte[] data) {
47.boolean tmp= writeToFile(new String(data), pos, amount);
48.return String.valueOf(tmp);
49.}
50.}
51.public class FileManagerClient {
52.public static void main(String[] args) {
53.FileManager f = null;
54.String dummyData = "dynamite";
55.f = new FileManagerImpl();
56.System.out.println("Using filemanager: "+
57.f.getClass().toString());
58.f.open("dummyfile.dat");
59.f.write(0, dummyData.length(), dummyData.getBytes());
60.String test = f.read(0,dummyData.length(),
61.dummyData.getBytes());
62.System.out.println("Data written and read: "+test);
63.f.close();
64.}
65.}
Imagine you need to develop a simple file manager to handle text documents. There is an existing resource that already handles this, but by some reason you are forced to a specific interface for your file manager. By using a class adapter we can use the forced interface and still reuse the existing functionality. In the class diagram below the interface FileManager is the target (desired interface). FileManagerUtil is the existing utility class that we would like to adapt to FileManager interface. We do the actual adaptation in the class FileManagerImpl, this class uses the desired interface and the
existing functionality through inheritance, i.e. a class adapter.


When FileManagerClient is executed the result is:
c:>Opening file: dummyfile.dat
c:>Writing 8 chars from string: dynamite to pos: 0 in file
c:>Reading 8 chars from pos: 0 in fileData written and read: dynamite
c:>Closing file

Usage example

The Java API uses the Adapter pattern, WindowAdapter, ComponentAdapter, ContainerAdapter, FocusAdapter, KeyAdapter, MouseAdapter, MouseMotionAdapter.

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Design Patterns - Observer Pattern Observer Pattern Definition An observer is a structural pattern that enables publish/subscribe functionality. This is accomplished by an autonomous object, publisher that allows other objects to attach or detach their subs… Read More
  • Design Patterns - Strategy Pattern Strategy Pattern Definition Use strategy when you need to define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Related… Read More
  • Design Patterns - Memento Pattern Memento Pattern Definition To record an object internal state without violating encapsulation and reclaim it later without knowledge of the original object. A memento is an object that stores a snapshot of the internal sta… Read More
  • Design Patterns - Mediator Pattern Mediator Pattern Definition With the mediator pattern communication between objects is encapsulated with a mediator object. Objects no longer communicate directly with each other, but instead communicate through the mediat… Read More
  • Design Patterns - Iterator Pattern Iterator Pattern Definition The Iterator design pattern provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Where to use Use to access the elements of… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Design Patterns - Adapter Pattern Rating: 5 Reviewed By: eHowToNow