๐๐ข๐ญ๐ฅ๐๐๐๐๐๐ฉ๐ญ๐๐ซ ๐๐๐ฌ๐ข๐ ๐ง ๐๐๐ญ๐ญ๐๐ซ๐ง
โ๏ธ ๐๐ง๐ญ๐๐ง๐ญ
Allows classes work together although they have incompatible interfaces.
๐คฏ๐๐ซ๐จ๐๐ฅ๐๐ฆ
Have you ever tried to integrate two existing systemโฆbut they just donโt fit together?
๐ ๐๐๐๐ฅ-๐๐จ๐ซ๐ฅ๐ ๐๐๐๐ง๐๐ซ๐ข๐จ
For example a drawing editor provides users actions such as: draw and arrange of graphical elements (lines, text, etc.). This editor provides an abstract class named Shape. Classes for elementary shapes like LineShape and PolygonShape are easy to implement, but TextShape is not because it displays and edits text. Meanwhile, there is a toolkit class which already implements displaying and editing text named TextView, but it was not designed to work with Shape classes. Therefore, we cannot use TextView and Shape interchangeably. How can we cope with this issue?
๐ฏ ๐๐จ๐ฅ๐ฎ๐ญ๐ข๐จ๐ง โ ๐๐๐๐ฉ๐ญ๐๐ซ ๐๐๐ญ๐ญ๐๐ซ๐ง
We can define TextShape so that it adapts the TextView to Shape. We can do it in one of two ways:
1๏ธโฃ๐๐ง๐ก๐๐ซ๐ข๐ญ๐๐ง๐๐
2๏ธโฃ๐๐จ๐ฆ๐ฉ๐จ๐ฌ๐ข๐ญ๐ข๐จ๐ง
๐๐๐๐ฅ๐๐ญ๐๐ ๐๐๐ญ๐ญ๐๐ซ๐ง๐ฌ
Bridge has a structure similar to an object adapter, but Bridge has a different intent: It is meant to separate an interface from its implementation so that they can be varied independently. An Adapter is meant to change the interface of an existing object. Decorator enhances another object without changing its interface. A decorator is thus more transparent to the application than an adapter is. As a consequence, Decorator supports recursive composition, which isn't possible with pure adapters.
๐ ๐๐จ๐๐ ๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐
See it in action:
๐ GitHub - (https://github.com/shirin-monzavi/AdapterSample)
โ๐๐ก๐ข๐๐ก ๐๐๐ฌ๐ข๐ ๐ง ๐ฉ๐๐ญ๐ญ๐๐ซ๐ง ๐๐จ ๐ฒ๐จ๐ฎ ๐ฌ๐ญ๐ซ๐ฎ๐ ๐ ๐ฅ๐ ๐ฐ๐ข๐ญ๐ก ๐ญ๐ก๐ ๐ฆ๐จ๐ฌ๐ญ?
๐ Iโd love to hear your thoughts!