You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: adapter/README.md
+79-3Lines changed: 79 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,17 +19,93 @@ Convert the interface of a class into another interface the clients
19
19
expect. Adapter lets classes work together that couldn't otherwise because of
20
20
incompatible interfaces.
21
21
22
-

22
+
## Explanation
23
23
24
-
## General usage of Adapter Pattern:
25
-
+ Wrappers used to adopt 3rd parties libraries and frameworks - most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
24
+
Real world example
25
+
26
+
> Consider that you have some pictures in your memory card and you need to transfer them to your computer. In order to transfer them you need some kind of adapter that is compatible with your computer ports so that you can attach memory card to your computer. In this case card reader is an adapter.
27
+
> Another example would be the famous power adapter; a three legged plug can't be connected to a two pronged outlet, it needs to use a power adapter that makes it compatible with the two pronged outlet.
28
+
> Yet another example would be a translator translating words spoken by one person to another
29
+
30
+
In plain words
31
+
32
+
> Adapter pattern lets you wrap an otherwise incompatible object in an adapter to make it compatible with another class.
33
+
34
+
Wikipedia says
35
+
36
+
> In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.
37
+
38
+
**Programmatic Example**
39
+
40
+
Consider a captain that can only use rowing boats and cannot sail at all.
41
+
42
+
First we have interfaces `RowingBoat` and `FishingBoat`
43
+
44
+
```
45
+
public interface RowingBoat {
46
+
void row();
47
+
}
48
+
49
+
public class FishingBoat {
50
+
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoat.class);
51
+
public void sail() {
52
+
LOGGER.info("The fishing boat is sailing");
53
+
}
54
+
}
55
+
```
56
+
57
+
And captain expects an implementation of `RowingBoat` interface to be able to move
58
+
59
+
```
60
+
public class Captain implements RowingBoat {
61
+
62
+
private RowingBoat rowingBoat;
63
+
64
+
public Captain(RowingBoat rowingBoat) {
65
+
this.rowingBoat = rowingBoat;
66
+
}
67
+
68
+
@Override
69
+
public void row() {
70
+
rowingBoat.row();
71
+
}
72
+
}
73
+
```
74
+
75
+
Now let's say the pirates are coming and our captain needs to escape but there is only fishing boat available. We need to create an adapter that allows the captain to operate the fishing boat with his rowing boat skills.
76
+
77
+
```
78
+
public class FishingBoatAdapter implements RowingBoat {
79
+
80
+
private static final Logger LOGGER = LoggerFactory.getLogger(FishingBoatAdapter.class);
81
+
82
+
private FishingBoat boat;
83
+
84
+
public FishingBoatAdapter() {
85
+
boat = new FishingBoat();
86
+
}
87
+
88
+
@Override
89
+
public void row() {
90
+
boat.sail();
91
+
}
92
+
}
93
+
```
94
+
95
+
And now the `Captain` can use the `FishingBoat` to escape the pirates.
96
+
97
+
```
98
+
Captain captain = new Captain(new FishingBoatAdapter());
99
+
captain.row();
100
+
```
26
101
27
102
## Applicability
28
103
Use the Adapter pattern when
29
104
30
105
* you want to use an existing class, and its interface does not match the one you need
31
106
* you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces
32
107
* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
108
+
* most of the applications using third party libraries use adapters as a middle layer between the application and the 3rd party library to decouple the application from the library. If another library has to be used only an adapter for the new library is required without having to change the application code.
33
109
34
110
## Consequences:
35
111
Class and object adapters have different trade-offs. A class adapter
0 commit comments