{"id":6457,"date":"2022-10-09T04:32:24","date_gmt":"2022-10-09T04:32:24","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/the-adapter-pattern\/"},"modified":"2023-06-26T08:57:46","modified_gmt":"2023-06-26T08:57:46","slug":"the-adapter-pattern","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/the-adapter-pattern\/","title":{"rendered":"The Adapter Pattern"},"content":{"rendered":"<p>The idea of the adapter pattern is straightforward: It converts the interface of a class into another interface.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6385\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/06\/patterns.png\" alt=\"patterns\" width=\"650\" height=\"330\" style=\"display: block; margin-left: auto; margin-right: auto;\" \/>Imagine you have a class that implements the functionality required by the client. There is only one issue: the interface does not follow your company&#8217;s naming policy. Thanks to the Adapter Pattern, you can pretty easily support the required interface using the existing class.<\/p>\n<p>The Adapter Pattern is the only pattern inside the book &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software&#8221;<\/a>&nbsp; (Design Pattern) that is implemented on a class level but also on an object level. Here are the facts before I show you both ways to implement this structural pattern.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"The_Adapter_Pattern\"><\/span>The Adapter Pattern<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Purpose\"><\/span>Purpose<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Translate one interface into another one<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Also_known_as\"><\/span>Also known as<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Wrapper<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Use_Case\"><\/span>Use Case<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>A class does not have the required interface<\/li>\n<li>Definition of a general interface for a set of similar classes<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Example\"><\/span>Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4>Container Adapter<\/h4>\n<p>The container adapters <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/stack\">s<code>td::stack<\/code><\/a><code>, <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/queue\">std::queue<\/a>,<\/code> and<code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/priority_queue\">std::priority_queue<\/a><\/code> provide a different interface for the sequence containers. The following code snippet shows the template signature of the three container adapters:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Container <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>deque<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;&gt;<\/span> \r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">stack<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Container <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>deque<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;&gt;<\/span> \r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">queue<\/span>;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">template<\/span><span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> T, <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Container <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>vector<span style=\"color: #555555;\">&lt;<\/span>T<span style=\"color: #555555;\">&gt;<\/span>, \r\n         <span style=\"color: #006699; font-weight: bold;\">typename<\/span> Compare <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>less<span style=\"color: #555555;\">&lt;<\/span><span style=\"color: #006699; font-weight: bold;\">typename<\/span> Container<span style=\"color: #555555;\">::<\/span>value_type<span style=\"color: #555555;\">&gt;&gt;<\/span> \r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">priority_queue<\/span>;\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>By default,<code> <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/stack\">std::stack<\/a>,<\/code> and <code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/queue\">std::queue<\/a><\/code> use <code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/deque\">std::deque<\/a><\/code> as sequence container, but <code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/priority_queue\">std::priority_queue<\/a><\/code> uses<code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/vector\"> std::vector<\/a><\/code>. Additionally, <code><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/container\/priority_queue\">std::priority_queue<\/a> <\/code>also requires a comparator that is defaulted to <code>std::less<\/code> of the sequence container elements.<\/p>\n<p>&nbsp;C++ has more adapters.<\/p>\n<h4>Iterator Adapter<\/h4>\n<p>C++ supports insert iterators and streams iterators.<\/p>\n<ul>\n<li>Insert Iterators<\/li>\n<\/ul>\n<p>With the three<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/iterator\"> insert iterators<code> std::front_inserter<\/code><\/a>, <code>std::back_inserter<\/code>, and <code>std::inserter,<\/code> you can insert an element into a container at the beginning, at the end or an arbitrary position, respectively.<\/p>\n<ul>\n<li>Stream Iterators<\/li>\n<\/ul>\n<p><a href=\"https:\/\/en.cppreference.com\/w\/cpp\/iterator\">Stream iterator<\/a> adapters can use streams as a data source or data sink. C++ offers two functions to create istream iterators and two to create ostream iterators. The created istream iterators behave like <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/iterator\">input iterators<\/a>, the ostream iterators like<a href=\"https:\/\/en.cppreference.com\/w\/cpp\/iterator\"> insert iterators<\/a>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Structure\"><\/span>Structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The following two class diagrams show the structure of the Adapter Pattern, based on classes or on objects. For short, I call them class adapters and object adapters.<\/p>\n<h4>Class Adapter<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5327\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/10\/AdapterrClass.png\" alt=\"AdapterrClass\" width=\"550\" height=\"361\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/10\/AdapterrClass.png 448w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2017\/10\/AdapterrClass-300x197.png 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<h4>Object Adapter<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6455\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/AdapterObject.png\" alt=\"AdapterObject\" width=\"550\" height=\"226\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/AdapterObject.png 448w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/AdapterObject-300x123.png 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<p><code>Client<\/code><\/p>\n<ul>\n<li>Uses the member function <code>methodA()<\/code> of the <code>Adapter<\/code><\/li>\n<\/ul>\n<p><code>Adaptor<\/code><\/p>\n<ul>\n<li>Class\n<ul>\n<li>Provides the functionality of <code>methodA()<\/code> using multiple inheritances<\/li>\n<li>Is publicly derived from <code>Interface<\/code> and privately from <code>Implementation<\/code><\/li>\n<\/ul>\n<\/li>\n<li>Object\n<ul>\n<li>Delegates the member function call to its member <code>Adaptee<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><code>Adaptee<\/code><\/p>\n<ul>\n<li>Implements the functionality of the client<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>Now, the class or object based Adapter Pattern should be straightforward.<\/p>\n<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Implementation\"><\/span>Implementation<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Class_Adapter\"><\/span>Class Adapter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In the following example, the class <code>RectangleAdapter<\/code> adapts the interface of the<code> LegacyRectangle<\/code>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ adapterClass.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">typedef<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> Coordinate;\r\n<span style=\"color: #006699; font-weight: bold;\">typedef<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> Dimension;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Rectangle<\/span> {\r\n<span style=\"color: #9999ff;\">public:<\/span>\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> draw() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #555555;\">~<\/span>Rectangle() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">LegacyRectangle<\/span> {\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    LegacyRectangle(Coordinate x1, Coordinate y1, Coordinate x2, Coordinate y2) <span style=\"color: #555555;\">:<\/span> x1_(x1), y1_(y1), x2_(x2), y2_(y2){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"LegacyRectangle:  create.  (\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> x1_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\",\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> y1_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\") =&gt; (\"<\/span>\r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> x2_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\",\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> y2_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\")\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> oldDraw() {\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"LegacyRectangle:  oldDraw.  (\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> x1_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\",\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> y1_ \r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\") =&gt; (\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> x2_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\",\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> y2_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\")\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n\r\n <span style=\"color: #9999ff;\">private:<\/span>\r\n    Coordinate x1_;\r\n    Coordinate y1_;\r\n    Coordinate x2_;\r\n    Coordinate y2_;\r\n};\r\n\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">RectangleAdapter<\/span> <span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Rectangle, <span style=\"color: #006699; font-weight: bold;\">private<\/span> LegacyRectangle {\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    RectangleAdapter(Coordinate x, Coordinate y, Dimension w, Dimension h) <span style=\"color: #555555;\">:<\/span> LegacyRectangle(x, y, x <span style=\"color: #555555;\">+<\/span> w, y <span style=\"color: #555555;\">+<\/span> h) { <span style=\"color: #0099ff;\"><em> \/\/ (1)<\/em><\/span>\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"RectangleAdapter: create.  (\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> x <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\",\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> y \r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"), width = \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> w <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", height = \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> h <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> draw() override {\r\n        oldDraw();\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"RectangleAdapter: draw.\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    Rectangle<span style=\"color: #555555;\">*<\/span> r <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> RectangleAdapter(<span style=\"color: #ff6600;\">120<\/span>, <span style=\"color: #ff6600;\">200<\/span>, <span style=\"color: #ff6600;\">60<\/span>, <span style=\"color: #ff6600;\">40<\/span>);\r\n    r<span style=\"color: #555555;\">-&gt;<\/span>draw();\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">delete<\/span> r;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><code>RectangleAdapter<\/code> derived the interface from the <code>Rectangle<\/code>, and the implementation for <code>LegacyRectangle <\/code>using multiple inheritances. Additionally, <code>RectangleAdapter<\/code> adapts the size of the <code>LegacyRectangle<\/code> (line 1).<\/p>\n<p>This implementation of the Adapter Pattern is one of the rare use cases for private inheritance. Let me write a few words about interface inheritance and implementation inheritance.<\/p>\n<ul>\n<li><span id=\"page532R_mcid7\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\"><strong>Interface inheritance<\/strong><\/span><\/span><strong><span id=\"page532R_mcid8\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\"> <\/span><\/span><\/strong><span id=\"page532R_mcid8\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\">uses<\/span><\/span><span id=\"page532R_mcid9\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\"> <\/span><span dir=\"ltr\" role=\"presentation\">public<\/span><\/span><span id=\"page532R_mcid10\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\"> <\/span><span dir=\"ltr\" role=\"presentation\">inheritance. It <\/span><\/span><span id=\"page532R_mcid11\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\">separates users from implementations to allow derived <\/span><\/span><span id=\"page532R_mcid12\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\">classes to be added and changed without affecting the <\/span><\/span><span id=\"page532R_mcid13\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\">users of the base class. Derived classes support the interface of the base class.<br \/><\/span><\/span><\/li>\n<li><strong><span id=\"page532R_mcid15\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\">Implementation inheritance<\/span><\/span><\/strong><span id=\"page532R_mcid16\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\"> <\/span><span dir=\"ltr\" role=\"presentation\">often uses<\/span><\/span><span id=\"page532R_mcid17\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\"> <\/span><span dir=\"ltr\" role=\"presentation\">private <\/span><\/span><span id=\"page532R_mcid18\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\">inheritance. Typically, the derived class provides its<\/span><\/span><span id=\"page532R_mcid19\" class=\"markedContent\"> <\/span><span id=\"page532R_mcid19\" class=\"markedContent\"><span dir=\"ltr\" role=\"presentation\">functionality by adapting functionality from the base class. Derived classes don&#8217;t support the interface of the base class.<br \/><\/span><\/span><\/li>\n<\/ul>\n<p>Finally, here is the output of the program:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6456\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/adapterClass.jpg\" alt=\"adapterClass\" width=\"500\" height=\"168\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/adapterClass.jpg 1632w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/adapterClass-300x101.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/adapterClass-1024x345.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/adapterClass-768x259.jpg 768w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/adapterClass-1536x518.jpg 1536w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Object_Adapter\"><\/span>Object Adapter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>&nbsp;In the following implementation, the <code>RectangleAdapter<\/code> delegates its calls to its adaptee <code>LegacyRectangle<\/code>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ adapterObject.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">typedef<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> Coordinate;\r\n<span style=\"color: #006699; font-weight: bold;\">typedef<\/span> <span style=\"color: #007788; font-weight: bold;\">int<\/span> Dimension;\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">LegacyRectangle<\/span> {\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    LegacyRectangle(Coordinate x1, Coordinate y1, Coordinate x2, Coordinate y2) <span style=\"color: #555555;\">:<\/span> x1_(x1), y1_(y1), x2_(x2), y2_(y2){\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"LegacyRectangle:  create.  (\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> x1_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\",\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> y1_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\") =&gt; (\"<\/span>\r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> x2_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\",\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> y2_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\")\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> oldDraw() {\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"LegacyRectangle:  oldDraw.  (\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> x1_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\",\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> y1_ \r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\") =&gt; (\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> x2_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\",\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> y2_ <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\")\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n\r\n <span style=\"color: #9999ff;\">private:<\/span>\r\n    Coordinate x1_;\r\n    Coordinate y1_;\r\n    Coordinate x2_;\r\n    Coordinate y2_;\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">RectangleAdapter<\/span> {\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    RectangleAdapter(Coordinate x, Coordinate y, Dimension w, Dimension h) <span style=\"color: #555555;\">:<\/span> legacyRectangle{LegacyRectangle(x, y, x <span style=\"color: #555555;\">+<\/span> w, y <span style=\"color: #555555;\">+<\/span> h)} {  <em><span style=\"color: #0099ff;\">\/\/ (1)\r\n        std::cout &lt;&lt; \"RectangleAdapter: create.  (\" &lt;&lt; x &lt;&lt; \",\" &lt;&lt; y <\/span><\/em>\r\n                  <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"), width = \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> w <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\", height = \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> h <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> draw() {\r\n        legacyRectangle.oldDraw();\r\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"RectangleAdapter: draw.\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n    }\r\n <span style=\"color: #9999ff;\">private:<\/span>\r\n     LegacyRectangle legacyRectangle;\r\n};\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    RectangleAdapter r(<span style=\"color: #ff6600;\">120<\/span>, <span style=\"color: #ff6600;\">200<\/span>, <span style=\"color: #ff6600;\">60<\/span>, <span style=\"color: #ff6600;\">40<\/span>);\r\n    r.draw();\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The class <code>RectangleAdapter<\/code> creates it <code>LegacyRectangle<\/code> directly in its constructor (line 1). Another option would be to make <code>LegacyRectangle&nbsp;<\/code> a constructor parameter of<code> RectangleAdapter:<\/code><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">RectangleAdapter<\/span> {\r\n <span style=\"color: #9999ff;\">public:<\/span>\r\n    RectangleAdapter(<span style=\"color: #006699; font-weight: bold;\">const<\/span> LegacyRectangle<span style=\"color: #555555;\">&amp;<\/span> legRec)<span style=\"color: #555555;\">:<\/span> legacyRectangle{legRec} {}\r\n ...\r\n};\r\n\r\n\r\n  \r\n<\/pre>\n<\/div>\n<p>The output of this program is identical to the previous one.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Related_Patterns\"><\/span>Related Patterns<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>The<a href=\"https:\/\/en.wikipedia.org\/wiki\/Bridge_pattern\"> Bridge Pattern<\/a> is similar to the object adapter but has a different intent. The Bridge Pattern&#8217;s purpose is to separate the interface from the implementation, but the adapter&#8217;s purpose is to modify an existing interface.<\/li>\n<li>The<a href=\"https:\/\/en.wikipedia.org\/wiki\/Decorator_pattern\"> Decorator Pattern <\/a>extends an object without changing its interface. Decorators are pluggable but not bridges or adapters.<\/li>\n<li>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Proxy_pattern\">Proxy Pattern<\/a> extends the implementation for the object it stands for but doesn&#8217;t change its interface.<\/li>\n<\/ul>\n<p>You may ask yourself: Should I use a class adapter or an object adapter.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Class_Adapter_versus_Object_Adapter\"><\/span>Class Adapter versus Object Adapter<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4>Class Adapter<\/h4>\n<p>The class adapter applies classes and their subclasses. It uses the separation of interface and implementation and runtime dispatch with virtual function calls. Its functionality is hard-coded and available at compile time. The class adapter provides less flexibility and dynamic behavior, such as the object adapter.<\/p>\n<h4>Object Adapter<\/h4>\n<p>The object adapter uses the relationship of objects.<\/p>\n<p>You build your abstraction by composing objects and delegating their work. This composition can be done at runtime. Consequentially, an object adapter is more flexible and allows it to exchange the delegated object at run time.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Whats_Next\"><\/span>What&#8217;s Next?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bridge_pattern\">Bridge Pattern<\/a> helps to separate the interface from its implementation. Let me introduce it in my next post.<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The idea of the adapter pattern is straightforward: It converts the interface of a class into another interface.<\/p>\n","protected":false},"author":21,"featured_media":6385,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[403],"class_list":["post-6457","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns","tag-multiple-inheritance"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=6457"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6457\/revisions"}],"predecessor-version":[{"id":6656,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6457\/revisions\/6656"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6385"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}