|
| 1 | +# Adapter Structural Design Pattern |
| 2 | +Also known as: `Wrapper` |
| 3 | + |
| 4 | + - is a Structural Design Pattern. |
| 5 | + - Adapter pattern works as a` bridge between two incompatible interfaces.` |
| 6 | + - A construct which adapts an |
| 7 | + existing interface X to conform to the required interface Y. |
| 8 | + |
| 9 | +<table> |
| 10 | + <tr> |
| 11 | + <td><img src="assets/adapter.jpeg"></td> |
| 12 | + <td><img src="assets/adapter-diagram.png"><td> |
| 13 | + </tr> |
| 14 | + <tr> |
| 15 | + <td></td> |
| 16 | + <td><img src="assets/adapter_explanation.jpg"><td> |
| 17 | + </tr> |
| 18 | +</table> |
| 19 | + |
| 20 | +- An adapter wraps one of the objects to hide the complexity of conversion happening behind the scenes. |
| 21 | + |
| 22 | + The wrapped object isn’t even aware of the adapter. For example, you can wrap an object that operates in meters and kilometers with an adapter that converts all of the data to imperial units such as feet and miles. |
| 23 | +- Sometimes it’s even possible to create a two-way adapter that can convert the calls in both directions. |
| 24 | + |
| 25 | +## Sections |
| 26 | + |
| 27 | +- [Types](#Types) |
| 28 | + - [Object Adapter](#Object-Adapter) |
| 29 | + - [Class Adapter](#Class-Adapter) |
| 30 | +- [How to Implement](#How-to-Implement) |
| 31 | +- [Examples](#Examples) |
| 32 | + - [square pegs and round holes Example](#square-pegs-and-round-holes-Example) |
| 33 | + - [Xml and json](#Xml-and-json) |
| 34 | + - [vessel volume Example](#vessel-volume-Example) |
| 35 | + - [Example in Arabic with explanation](#Example-in-Arabic-with-explanation) |
| 36 | + - [TO DO](#TO-DO) |
| 37 | + |
| 38 | + |
| 39 | +## Types |
| 40 | +<center> `Object Adapter && Class Adapter` </center> |
| 41 | + |
| 42 | +<table> |
| 43 | + <tr> |
| 44 | + <th>Object Adapter</th> |
| 45 | + <th>Class Adapter</th> |
| 46 | + </tr> |
| 47 | + <tr> |
| 48 | + <td>This implementation uses the object composition principle: the adapter implements the interface of one object and wraps the other one. It can be implemented in all popular programming languages.</td> |
| 49 | + <td>This implementation uses inheritance: the adapter inherits interfaces from both objects at the same time. Note that this approach can only be implemented in programming languages that support multiple inheritance, such as C++.</td> |
| 50 | + </tr> |
| 51 | + <tr> |
| 52 | + <td><img src="assets/structure-object-adapter.png"></td> |
| 53 | + <td><img src="assets/structure-class-adapter.png"></td> |
| 54 | + </tr> |
| 55 | + <tr> |
| 56 | + <td>The Adapter is a class that’s able to work with both the client and the service: (1-it implements the client interface), (2-while wrapping the service object).The adapter receives calls from the client via the adapter interface and translates them into calls to the wrapped service object in a format it can understand.</td> |
| 57 | + <td>The Class Adapter doesn’t need to wrap any objects because it inherits behaviors from both the client and the service. The adaptation happens within the overridden methods. The resulting adapter can be used in place of an existing client class.</td> |
| 58 | + </tr> |
| 59 | + |
| 60 | +</table> |
| 61 | + |
| 62 | + --- |
| 63 | +## How to Implement |
| 64 | +1. Make sure that you have at least two classes with incompatible interfaces: |
| 65 | + 1. A useful service class, which you can’t change (often 3rd-party, legacy or with lots of existing dependencies). |
| 66 | + 1. One or several client classes that would benefit from using the service class. |
| 67 | + |
| 68 | +1. Declare the client interface and describe how clients communicate with the service. |
| 69 | + |
| 70 | +1. Create the adapter class and make it follow the client interface. Leave all the methods empty for now. |
| 71 | + |
| 72 | +1. Add a field to the adapter class to store a reference to the service object. The common practice is to initialize this field via the constructor, but sometimes it’s more convenient to pass it to the adapter when calling its methods. |
| 73 | + |
| 74 | +1. One by one, implement all methods of the client interface in the adapter class. The adapter should delegate most of the real work to the service object, handling only the interface or data format conversion. |
| 75 | + |
| 76 | +1. Clients should use the adapter via the client interface. This will let you change or extend the adapters without affecting the client code. |
| 77 | + |
| 78 | + |
| 79 | +## Examples |
| 80 | + |
| 81 | +### square pegs and round holes Example |
| 82 | + - Example in dart: <a href="square_pegs_and_round_holes/README.md" target="_blank">square pegs and round holes Example </a> |
| 83 | + - Example Source: <a href="https://refactoring.guru/design-patterns/adapter" target="_blank">refactoring.guru/design-patterns/adapter</a> |
| 84 | + |
| 85 | +### Xml and json |
| 86 | + - Example in dart: <a href="xml_and_json/README.md" target="_blank">Xml and json</a> |
| 87 | +- Example Source: <a href="https://www.c-sharpcorner.com/article/adapter-design-pattern/" target="_blank">https://www.c-sharpcorner.com/article/adapter-design-pattern/</a> |
| 88 | + |
| 89 | +### ToyDuck and Bird |
| 90 | +- Example in dart: <a href="bird_and_toy_duck/README.md" target="_blank">bird and toy duck</a> |
| 91 | +- Example Source: <a href="https://www.geeksforgeeks.org/adapter-pattern/" target="_blank">https://www.geeksforgeeks.org/adapter-pattern/</a> |
| 92 | + |
| 93 | +### Example in Arabic with explanation |
| 94 | +- Source: <a href="https://www.linkedin.com/pulse/design-patterns-arabic-7-adapter-hassan-elseoudy/?originalSubdomain=ae" target="_blank">https://www.linkedin.com/pulse/design-patterns-arabic-7-adapter-hassan-elseoudy/?originalSubdomain=ae</a> |
| 95 | + |
| 96 | + |
| 97 | +## TO DO |
| 98 | + |
| 99 | +- convert sms example to dart |
| 100 | + - Example: <a href="sms_example/README.md" target="_blank">sms example</a> |
| 101 | + - <a href="https://youtu.be/LP0PZ7WPJPo" target="_blank">https://youtu.be/LP0PZ7WPJPo</a> |
0 commit comments