{"id":6460,"date":"2022-10-16T04:20:06","date_gmt":"2022-10-16T04:20:06","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/the-bridge-pattern\/"},"modified":"2023-08-20T14:29:04","modified_gmt":"2023-08-20T14:29:04","slug":"the-bridge-pattern","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/the-bridge-pattern\/","title":{"rendered":"The Bridge Pattern"},"content":{"rendered":"<p>The Bridge Pattern is a structural pattern. It decouples the interface from the implementation. In C++, a simplified version is often used: the Pimpl Idiom.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<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;\" \/><\/p>\n<p>Before I write about the Pimpl Idiom, here are the fact about the Bridge Pattern<\/p>\n<h2><span class=\"ez-toc-section\" id=\"The_Bridge_Pattern\"><\/span>The Bridge 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>Decouples the interface from the implementation<\/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>Handle\/Body<\/li>\n<li>Pimpl (pointer to implementation) Idiom<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Applicability\"><\/span>Applicability<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>The interface and the implementation can be extended<\/li>\n<li>A change of the interface of the implementation does not affect the client<\/li>\n<li>The implementation is hidden<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Structure\"><\/span>Structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6458\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/Bridge.png\" alt=\"Bridge\" width=\"422\" height=\"219\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/Bridge.png 422w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/Bridge-300x156.png 300w\" sizes=\"auto, (max-width: 422px) 100vw, 422px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong><code>Abstraction<\/code><\/strong><\/p>\n<ul>\n<li>Defines the interface of the abstraction<\/li>\n<li>Has an object of type <code>Implementor<\/code><\/li>\n<\/ul>\n<p><strong><code>RedefinedAbstraction<\/code><\/strong><\/p>\n<ul>\n<li>Implements or refines the interface of the <code>Abstraction<\/code><\/li>\n<\/ul>\n<p><strong><code>Implementor<\/code><\/strong><\/p>\n<ul>\n<li>Defines the interface of the implementation<\/li>\n<\/ul>\n<p><strong><code>ConcreteImplementor<\/code><\/strong><\/p>\n<ul>\n<li>Implements the interface of the <code>Implementor<\/code><\/li>\n<\/ul>\n<p>The Bridge Pattern has two hierarchies. One hierarchy for the abstraction (interface), and one for the implementation. The client programs against the abstraction, and the abstraction uses the implementation. Consequently, different implementations of the abstraction interface and implementation interface can be used transparently. The Bridge Pattern provides big flexibility because the abstraction and implementation can be varied and exchanged during the program&#8217;s run time.<\/p>\n<p>The Bridge Pattern is a powerful example exemplifying the combination of inheritance and composition. On one hand, it has two type hierarchies (inheritance); on the other hand, the abstraction has an implementation (composition).<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example\"><\/span>Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The example shows a straightforward implementation of the Bridge Pattern.<\/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;\">\/\/ bridge.cpp<\/span>\n\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\n\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Implementor<\/span> {                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\n<span style=\"color: #9999ff;\">public:<\/span>\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> implementation() <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\n\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #555555;\">~<\/span>Implementor() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\n};\n \n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">ImplementorA<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Implementor {\n<span style=\"color: #9999ff;\">public:<\/span>\n    ImplementorA() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\n \n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> implementation() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"ImplementatorA::implementation\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    }\n};\n \n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">ImplementorB<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Implementor {\n<span style=\"color: #9999ff;\">public:<\/span>\n    ImplementorB() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\n\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> implementation() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"ImplementatorB::implementation\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n    }\n};\n\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Abstraction<\/span> {                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)      <\/span>\n<span style=\"color: #9999ff;\">public:<\/span>\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> function() <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">0<\/span>;\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #555555;\">~<\/span>Abstraction() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\n};\n\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">RefinedAbstraction<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Abstraction {\n<span style=\"color: #9999ff;\">public:<\/span>\n    RefinedAbstraction(Implementor<span style=\"color: #555555;\">&amp;<\/span> impl) <span style=\"color: #555555;\">:<\/span> \n\t\timplementor(impl) {\n    }\n \n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> function() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\n        std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"RefinedAbstraction::function<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>;\n        implementor.implementation();\n    }\n<span style=\"color: #9999ff;\">private:<\/span>\n    Implementor<span style=\"color: #555555;\">&amp;<\/span> implementor;\n};\n \n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    ImplementorA implementorA;\n    ImplementorB implementorB;\n \n    RefinedAbstraction refinedAbstraction1(implementorA);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\n    RefinedAbstraction refinedAbstraction2(implementorB);  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\n\n    Abstraction <span style=\"color: #555555;\">*<\/span>abstraction1 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">&amp;<\/span>refinedAbstraction1;\n    Abstraction <span style=\"color: #555555;\">*<\/span>abstraction2 <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">&amp;<\/span>refinedAbstraction2;\n\n    abstraction1<span style=\"color: #555555;\">-&gt;<\/span>function();\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n    abstraction2<span style=\"color: #555555;\">-&gt;<\/span>function();\n\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\n\n}\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The class <code>Implementor<\/code> (line 1) is the interface for the implementation hierarchy, and the class <code>Abstraction<\/code> (line 2) is the interface of the abstraction. The instances <code>redefinedAbstraction1<\/code> and <code>redefinedAbstraction2<\/code> get their implementation in its constructor (lines 3 and 4).<\/p>\n<p>The following screenshot shows the output of the program.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6459\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/bridgeExample.png\" alt=\"bridgeExample\" width=\"550\" height=\"313\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/bridgeExample.png 630w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2022\/10\/bridgeExample-300x170.png 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/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\/Adapter_pattern\"> Adapter Pattern<\/a> implemented as an object adapter is similar to the Bridge Pattern 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\/Abstract_factory_pattern\"> Abstract Factory<\/a> can create and configure a Bridge.<\/li>\n<\/ul>\n<p>In C++, a simplified version of the Bridge Pattern is often used.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"The_Pimpl_Idiom\"><\/span>The Pimpl Idiom<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The key idea of the Pimpl Idiom is that the implementation of the class is hidden behind a pointer.<\/p>\n<p>Here is a recipe for implementing the Pimpl Idiom:<\/p>\n<ol>\n<li>Move private data and member functions of the class (<strong>public class<\/strong>) to a separate class (<strong>pimpl class<\/strong>).<\/li>\n<li>Forward declare the <strong>pImpl class<\/strong> in the header of the <strong>public class.<\/strong><\/li>\n<li>Declare pointer of type <strong>pimpl class<\/strong> in the <strong>public class.<\/strong><\/li>\n<li>Define the<strong> pimpl class<\/strong> in the source file of the<strong> public class.<\/strong><\/li>\n<li>Instantiate the <strong>pimpl class<\/strong> in the constructor of the <strong>public class.<\/strong><\/li>\n<li>Member functions of the <strong>public class<\/strong> use those of the <strong>pimpl class<\/strong>.<\/li>\n<\/ol>\n<p>Bartlomiej Filipek provides in his blog post &#8220;<a href=\"https:\/\/www.cppstories.com\/2018\/01\/pimpl\/\">The Pimpl Pattern &#8211; what you should know<\/a>&#8221; a nice example of the Pimpl Idiom:<\/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;\">\/\/ class.h<\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyClassImpl<\/span>;\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyClass<\/span>\n{\n<span style=\"color: #9999ff;\">public:<\/span>\n    <span style=\"color: #006699; font-weight: bold;\">explicit<\/span> MyClass();\n    <span style=\"color: #555555;\">~<\/span>MyClass(); \n\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ movable:<\/span>\n    MyClass(MyClass <span style=\"color: #555555;\">&amp;&amp;<\/span> rhs) noexcept;                         <span style=\"color: #0099ff;\"><em> \/\/ (2)<\/em><\/span>\n    MyClass<span style=\"color: #555555;\">&amp;<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">=<\/span>(MyClass <span style=\"color: #555555;\">&amp;&amp;<\/span> rhs) noexcept;              <span style=\"color: #0099ff;\"> <em>\/\/ (3)<\/em>\n<\/span>\n    <span style=\"color: #0099ff; font-style: italic;\">\/\/ and copyable<\/span>\n    MyClass(<span style=\"color: #006699; font-weight: bold;\">const<\/span> MyClass<span style=\"color: #555555;\">&amp;<\/span> rhs);                             <span style=\"color: #0099ff;\">  <em>\/\/ (4)<\/em><\/span>\n    MyClass<span style=\"color: #555555;\">&amp;<\/span> <span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">=<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> MyClass<span style=\"color: #555555;\">&amp;<\/span> rhs);                   <span style=\"color: #0099ff;\"><em> \/\/ (5)<\/em><\/span>\n\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">DoSth<\/span>();\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> DoConst() <span style=\"color: #006699; font-weight: bold;\">const<\/span>;\n\n<span style=\"color: #9999ff;\">private:<\/span>\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> MyClassImpl<span style=\"color: #555555;\">*<\/span> <span style=\"color: #cc00ff;\">Pimpl<\/span>() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { <span style=\"color: #006699; font-weight: bold;\">return<\/span> m_pImpl.get(); }  <em><span style=\"color: #0099ff;\">\/\/ (6)<\/span><\/em>\n    MyClassImpl<span style=\"color: #555555;\">*<\/span> Pimpl() { <span style=\"color: #006699; font-weight: bold;\">return<\/span> m_pImpl.get(); }             <em> <span style=\"color: #0099ff;\">\/\/ (7)<\/span><\/em>\n\n    std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>MyClassImpl<span style=\"color: #555555;\">&gt;<\/span> m_pImpl;                      <span style=\"color: #0099ff;\"><em> \/\/ (1)<\/em><\/span>\n};\n\n<span style=\"color: #0099ff; font-style: italic;\">\/\/ class.cpp<\/span>\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">MyClassImpl<\/span>\n{\n<span style=\"color: #9999ff;\">public:<\/span>\n    <span style=\"color: #555555;\">~<\/span>MyClassImpl() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\n\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">DoSth<\/span>() { }\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> DoConst() <span style=\"color: #006699; font-weight: bold;\">const<\/span> { }\n};\n\nMyClass<span style=\"color: #555555;\">::<\/span>MyClass() <span style=\"color: #555555;\">:<\/span> m_pImpl(<span style=\"color: #006699; font-weight: bold;\">new<\/span> MyClassImpl()) \n{\n\n}\n\nMyClass<span style=\"color: #555555;\">::~<\/span>MyClass() <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\nMyClass<span style=\"color: #555555;\">::<\/span>MyClass(MyClass <span style=\"color: #555555;\">&amp;&amp;<\/span>) noexcept <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\nMyClass<span style=\"color: #555555;\">&amp;<\/span> MyClass<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">=<\/span>(MyClass <span style=\"color: #555555;\">&amp;&amp;<\/span>) noexcept <span style=\"color: #555555;\">=<\/span> <span style=\"color: #006699; font-weight: bold;\">default<\/span>;\n\nMyClass<span style=\"color: #555555;\">::<\/span>MyClass(<span style=\"color: #006699; font-weight: bold;\">const<\/span> MyClass<span style=\"color: #555555;\">&amp;<\/span> rhs)\n    <span style=\"color: #555555;\">:<\/span> m_pImpl(<span style=\"color: #006699; font-weight: bold;\">new<\/span> MyClassImpl(<span style=\"color: #555555;\">*<\/span>rhs.m_pImpl))\n{}\n\nMyClass<span style=\"color: #555555;\">&amp;<\/span> MyClass<span style=\"color: #555555;\">::<\/span><span style=\"color: #006699; font-weight: bold;\">operator<\/span><span style=\"color: #555555;\">=<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> MyClass<span style=\"color: #555555;\">&amp;<\/span> rhs) {\n    <span style=\"color: #006699; font-weight: bold;\">if<\/span> (<span style=\"color: #006699; font-weight: bold;\">this<\/span> <span style=\"color: #555555;\">!=<\/span> <span style=\"color: #555555;\">&amp;<\/span>rhs) \n        m_pImpl.reset(<span style=\"color: #006699; font-weight: bold;\">new<\/span> MyClassImpl(<span style=\"color: #555555;\">*<\/span>rhs.m_pImpl));\n\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>;\n}\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> MyClass<span style=\"color: #555555;\">::<\/span>DoSth()\n{\n    Pimpl()<span style=\"color: #555555;\">-&gt;<\/span>DoSth();\n}\n\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> MyClass<span style=\"color: #555555;\">::<\/span>DoConst() <span style=\"color: #006699; font-weight: bold;\">const<\/span>\n{\n    Pimpl()<span style=\"color: #555555;\">-&gt;<\/span>DoConst();\n}\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Here are the key ideas of his implementation. I added a few line markers:<\/p>\n<ul>\n<li>The pimpl is a <code>std::unique_ptr&lt;MyClassImpl&gt; (line 1)<br \/>\n<\/code><\/li>\n<li>The class support copy semantics and move semantics (lines 2 &#8211; 5)<\/li>\n<li>The private <code>Pimp()<\/code> member functions of <code>MyClass<\/code> return a <code>const<\/code> and a non-<code>const<\/code> <code>MyClassImpl <\/code>pointers (lines 6 and 7)<\/li>\n<\/ul>\n<p>You may ask yourself. What are the benefits of the Pimpl Idiom? It would be easier to merge the implementation <code>MyClassImpl<\/code> into the abstraction <code>MyClass.<\/code><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Pros_and_Cons\"><\/span>Pros and Cons<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Let me start with the pros<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Pros\"><\/span>Pros<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><strong>Binary Compatibility<\/strong>: When you change the implementation, this would not break the interface for the client using the abstraction.<\/li>\n<li><strong>Compile Time<\/strong>: Changing the implementation does not require a recompilation of the client using the abstraction. Due to this fact, the Pimpl Idiom is often called <strong>a compilation firewall<\/strong>. This benefit is obsolete with modules in C++20<\/li>\n<li><strong>Extensibility<\/strong>: It is pretty easy to exchange the implementation during run time. In general, there is no need for virtuality.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Cons\"><\/span>Cons<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><strong>Performance<\/strong>: The pointer indirection causes extra run-time costs.<\/li>\n<li><strong>The Bix Six<\/strong>: You have to think about the Big Six (Read my post previous post the &#8220;<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-constructors-assignments-and-desctructors\">C++ Core Guidelines: The Rule of Zero, Five, and Six<\/a>&#8221; to get more details about the Big Six.). In particular, due to the fact that the abstraction has a <code>std::unique_pt<\/code>r, it supports no copy semantics. This means in the concrete case.\n<ul>\n<li>You have to implement copy semantics if you need it.<\/li>\n<li>When you implement copy semantics, you don&#8217;t automatically get move semantics and a default constructor.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Memory Allocation<\/strong>: The Pimpl Idiom requires a memory allocation. This memory allocation may not be possible in embedded systems and may cause memory fragmentation.<\/li>\n<\/ul>\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>Additionally, the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Decorator_pattern\">Decorator Pattern<\/a> is an often-used structural pattern from the book &#8220;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Design_Patterns\">Design Patterns: Elements of Reusable Object-Oriented Software&#8221;<\/a>. Its job is to extend an object with responsibilities dynamically. Let me present the Decorator in my next post.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Bridge Pattern is a structural pattern. It decouples the interface from the implementation. In C++, a simplified version is often used: the Pimpl Idiom.<\/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":[398,399,401],"class_list":["post-6460","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns","tag-pimpl","tag-smart-pointers","tag-unique_ptr"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6460","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=6460"}],"version-history":[{"count":2,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6460\/revisions"}],"predecessor-version":[{"id":8030,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6460\/revisions\/8030"}],"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=6460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}