Wednesday, December 25, 2024
Google search engine
HomeGuest BlogsComposite Design Pattern in C++

Composite Design Pattern in C++

Prerequisite : Composite Design Pattern
Composite pattern is one of the most widely used patterns in the industry and addresses a very significant and subtle problem. It is used whenever the user wants to treat the individual object in the same way as the collection of those individual objects for e.g you might want to consider a page from the copy as same as the whole copy which is basically a collection of the pages or if you want to create a hierarchy of something where you might want to consider the whole thing as the object . 
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
In the case of photoshop where we draw many individual objects and then those objects compose a whole unique object and you might want to apply some operation on the whole object instead of the each of the individual objects. 
 

Here in this diagram, as you can see both composite and Leaf implements Component diagram, thus allowing the same operation on both objects but the important part is Composite Class which also contain the Component Objects which is symbolized by the black diamond indicating composition relationship between Composite and Component class.
Then how to design our classes to accommodate such scenarios. We will try to understand it by implementing our copy example. Say you have to create a page which has operations like add, delete, remove and also a copy which will have the same operations as the individual pages.
Such situation is the best dealt with the composite pattern. 
 

CPP




<div id="highlighter_323292" class="syntaxhighlighter nogutter  "><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td class="code"><div class="container"><div class="line number1 index0 alt2"><code class="comments">// CPP program to illustrate</code></div><div class="line number2 index1 alt1"><code class="comments">// Composite design pattern</code></div><div class="line number3 index2 alt2"><code class="preprocessor">#include <iostream></code></div><div class="line number4 index3 alt1"><code class="preprocessor">#include <vector></code></div><div class="line number5 index4 alt2"><code class="keyword bold">using</code> <code class="keyword bold">namespace</code> <code class="plain">std;</code></div><div class="line number6 index5 alt1"> </div><div class="line number7 index6 alt2"><code class="keyword bold">class</code> <code class="plain">PageObject {</code></div><div class="line number8 index7 alt1"><code class="keyword bold">public</code><code class="plain">:</code></div><div class="line number9 index8 alt2"><code class="undefined spaces">    </code><code class="keyword bold">virtual</code> <code class="keyword bold">void</code> <code class="plain">Add(PageObject a)</code></div><div class="line number10 index9 alt1"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number11 index10 alt2"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number12 index11 alt1"><code class="undefined spaces">    </code><code class="keyword bold">virtual</code> <code class="keyword bold">void</code> <code class="plain">Remove()</code></div><div class="line number13 index12 alt2"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number14 index13 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number15 index14 alt2"><code class="undefined spaces">    </code><code class="keyword bold">virtual</code> <code class="keyword bold">void</code> <code class="plain">Delete(PageObject a)</code></div><div class="line number16 index15 alt1"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number17 index16 alt2"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number18 index17 alt1"><code class="plain">};</code></div><div class="line number19 index18 alt2"> </div><div class="line number20 index19 alt1"><code class="keyword bold">class</code> <code class="plain">Page : </code><code class="keyword bold">public</code> <code class="plain">PageObject {</code></div><div class="line number21 index20 alt2"><code class="keyword bold">public</code><code class="plain">:</code></div><div class="line number22 index21 alt1"><code class="undefined spaces">    </code><code class="keyword bold">void</code> <code class="plain">Add(PageObject a)</code></div><div class="line number23 index22 alt2"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number24 index23 alt1"><code class="undefined spaces">        </code><code class="plain">cout << </code><code class="string">"something is added to the page"</code> <code class="plain"><< endl;</code></div><div class="line number25 index24 alt2"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number26 index25 alt1"><code class="undefined spaces">    </code><code class="keyword bold">void</code> <code class="plain">Remove()</code></div><div class="line number27 index26 alt2"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number28 index27 alt1"><code class="undefined spaces">        </code><code class="plain">cout << </code><code class="string">"something is removed from the page"</code> <code class="plain"><< endl;</code></div><div class="line number29 index28 alt2"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number30 index29 alt1"><code class="undefined spaces">    </code><code class="keyword bold">void</code> <code class="plain">Delete(PageObject a)</code></div><div class="line number31 index30 alt2"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number32 index31 alt1"><code class="undefined spaces">        </code><code class="plain">cout << </code><code class="string">"something is deleted from page "</code> <code class="plain"><< endl;</code></div><div class="line number33 index32 alt2"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number34 index33 alt1"><code class="plain">};</code></div><div class="line number35 index34 alt2"> </div><div class="line number36 index35 alt1"><code class="keyword bold">class</code> <code class="plain">Copy : </code><code class="keyword bold">public</code> <code class="plain">PageObject {</code></div><div class="line number37 index36 alt2"><code class="undefined spaces">    </code><code class="plain">vector<PageObject> copyPages;</code></div><div class="line number38 index37 alt1"> </div><div class="line number39 index38 alt2"><code class="keyword bold">public</code><code class="plain">:</code></div><div class="line number40 index39 alt1"><code class="undefined spaces">    </code><code class="keyword bold">void</code> <code class="plain">AddElement(PageObject a)</code></div><div class="line number41 index40 alt2"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number42 index41 alt1"><code class="undefined spaces">        </code><code class="plain">copyPages.push_back(a);</code></div><div class="line number43 index42 alt2"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number44 index43 alt1"> </div><div class="line number45 index44 alt2"><code class="undefined spaces">    </code><code class="keyword bold">void</code> <code class="plain">Add(PageObject a)</code></div><div class="line number46 index45 alt1"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number47 index46 alt2"><code class="undefined spaces">        </code><code class="plain">cout << </code><code class="string">"something is added to the copy"</code> <code class="plain"><< endl;</code></div><div class="line number48 index47 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number49 index48 alt2"><code class="undefined spaces">    </code><code class="keyword bold">void</code> <code class="plain">Remove()</code></div><div class="line number50 index49 alt1"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number51 index50 alt2"><code class="undefined spaces">        </code><code class="plain">cout << </code><code class="string">"something is removed from the copy"</code> <code class="plain"><< endl;</code></div><div class="line number52 index51 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number53 index52 alt2"><code class="undefined spaces">    </code><code class="keyword bold">void</code> <code class="plain">Delete(PageObject a)</code></div><div class="line number54 index53 alt1"><code class="undefined spaces">    </code><code class="plain">{</code></div><div class="line number55 index54 alt2"><code class="undefined spaces">        </code><code class="plain">cout << </code><code class="string">"something is deleted from the copy"</code><code class="plain">;</code></div><div class="line number56 index55 alt1"><code class="undefined spaces">    </code><code class="plain">}</code></div><div class="line number57 index56 alt2"><code class="plain">};</code></div><div class="line number58 index57 alt1"> </div><div class="line number59 index58 alt2"><code class="color1 bold">int</code> <code class="plain">main()</code></div><div class="line number60 index59 alt1"><code class="plain">{</code></div><div class="line number61 index60 alt2"><code class="undefined spaces">    </code><code class="plain">Page a;</code></div><div class="line number62 index61 alt1"><code class="undefined spaces">    </code><code class="plain">Page b;</code></div><div class="line number63 index62 alt2"><code class="undefined spaces">    </code><code class="plain">Copy allcopy;</code></div><div class="line number64 index63 alt1"><code class="undefined spaces">    </code><code class="plain">allcopy.AddElement(a);</code></div><div class="line number65 index64 alt2"><code class="undefined spaces">    </code><code class="plain">allcopy.AddElement(b);</code></div><div class="line number66 index65 alt1"> </div><div class="line number67 index66 alt2"><code class="undefined spaces">    </code><code class="plain">allcopy.Add(a);</code></div><div class="line number68 index67 alt1"><code class="undefined spaces">    </code><code class="plain">a.Add(b);</code></div><div class="line number69 index68 alt2"> </div><div class="line number70 index69 alt1"><code class="undefined spaces">    </code><code class="plain">allcopy.Remove();</code></div><div class="line number71 index70 alt2"><code class="undefined spaces">    </code><code class="plain">b.Remove();</code></div><div class="line number72 index71 alt1"> </div><div class="line number73 index72 alt2"><code class="undefined spaces">    </code><code class="keyword bold">return</code> <code class="plain">0;</code></div><div class="line number74 index73 alt1"><code class="plain">}</code></div></div></td></tr></tbody></table></div>


something is added to the copy
something is added to the page
something is removed from the copy
something is removed from the page

Time complexity : O(1) 
Auxiliary Space : O(1)

Now the same operation that can be applied to an individual object and can also be applied to the collection of those individual object makes it very easy to work with a larger object which is made of the smaller independent objects. 
The most notable example of the composite pattern is in any UI toolkit. Consider the case of UI elements where each UI top-level UI element is composed of many smaller independent lower-level UI elements and the both the top and lower level UI element respond to same events and actions.
References : 
1. Composite Pattern c# 
2. Composite Pattern


 

Last Updated :
31 Oct, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments