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 ormore 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.
}
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
0 comments:
Post a Comment