<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blog.dotnetstyling.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>DotNetStyling - .Net World by Armen Ayvazyan : C#, Architecture</title><link>http://blog.dotnetstyling.com/archive/tags/C_2300_/Architecture/default.aspx</link><description>Tags: C#, Architecture</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.2)</generator><item><title>Simplicity and Power of Composite Design Pattern</title><link>http://blog.dotnetstyling.com/archive/2007/04/14/simplicity-and-power-of-composite-design-pattern.aspx</link><pubDate>Sat, 14 Apr 2007 09:37:00 GMT</pubDate><guid isPermaLink="false">5b0fd9f5-e499-434e-81d8-bae286ef57b6:13</guid><dc:creator>admin</dc:creator><slash:comments>3</slash:comments><comments>http://blog.dotnetstyling.com/comments/13.aspx</comments><wfw:commentRss>http://blog.dotnetstyling.com/commentrss.aspx?PostID=13</wfw:commentRss><description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Last couple of days I was working on some small project where I had to deal with nested objects (or tree structure objects) and I immediately decided to use&amp;nbsp;Composite design pattern as it is one of the best solution for&amp;nbsp;dealing with&amp;nbsp;tree structure&amp;nbsp;objects. By nested objects&amp;nbsp;or tree structure objects&amp;nbsp;I mean the case when one object contains instance (or collection of instances)&amp;nbsp;of its own type. For example XmlNode contains instances of more than&amp;nbsp;one XmlNode (XmlNode.ChildNodes), another example is TreeNode, etc. Imagine you have to create your own tree structured custom class and to implement for example functionality of searching an object inside of the tree. 
&lt;P&gt;Composite pattern helps to implement such common functionalities as "Find an object from all tree structure", "Load all nested objects into the one collection", etc very easily. And I'll try to show on examples how to do that in easy (Composite) way. 
&lt;P&gt;First a little bit theory.&amp;nbsp;Detail information about pattern&amp;nbsp;of course you&amp;nbsp;can fine on Gang of Four web site &lt;A href="http://www.dofactory.com/Patterns/PatternComposite.aspx"&gt;Composite Pattern&lt;/A&gt; . 
&lt;P&gt;&lt;IMG height=325 alt=http://www.dofactory.com/Patterns/Diagrams/composite.gif src="http://blog.dotnetstyling.com/blogs/dotnetworld/Images/SimplicityandPowerofCompositeDesignPatte_21D0/clip_image001.gif" width=449 border=0&gt; 
&lt;P&gt;In this UML diagram Component is an abstract class, Leaf is a class which is not include in the tree and is out of “composite” functionality and Composite is actually is our guy, the object which contain instances of its own type. 
&lt;P&gt;Let me simplify this graph a little bit and show it in other way. 
&lt;P&gt;&lt;IMG height=220 alt=MyComposite.JPG src="http://blog.dotnetstyling.com/blogs/dotnetworld/Images/SimplicityandPowerofCompositeDesignPatte_21D0/clip_image002.jpg" width=394 border=0&gt; 
&lt;P&gt;As you see we do not have abstract (Component) class and Leaf and Client class is dealing with Composite class directly. 
&lt;P&gt;&lt;B&gt;So, let’s start coding.&lt;/B&gt; 
&lt;P&gt;Let’s create two classes: first is Composite Class (we will call it MyClass) and its collection container&amp;nbsp;(MyClassCollection). 
&lt;P&gt;&lt;IMG height=186 src="http://blog.dotnetstyling.com/blogs/dotnetworld/Images/SimplicityandPowerofCompositeDesignPatte_21D0/clip_image004.jpg" width=523 border=0&gt; 
&lt;P&gt;MyClass will contain a constructor and two properties as Id (unique identification of class) and instance of MyClassCollection (collection of the same MyClass type belongs to object). 
&lt;P&gt;C# code looks like this: 
&lt;P&gt;&lt;FONT color=#0080ff&gt;public class MyClass&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; private int _id;&lt;/FONT&gt; 
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT color=#0080ff&gt;public int Id&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; get { return _id; }&lt;BR&gt;}&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#0080ff&gt;private MyClassCollection _myClasses = new MyClassCollection();&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#0080ff&gt;public MyClassCollection MyClasses&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; get { return _myClasses; }&lt;BR&gt;}&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#0080ff&gt;public MyClass(int id)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; _id = id;&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT color=#0080ff&gt;}&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#0080ff&gt;public class MyClassCollection : System.Collections.CollectionBase&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public int Add(MyClass instance)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return List.Add(instance);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt; 
&lt;P&gt;Lets now implement “Search object in a tree” functionality. 
&lt;P&gt;Lets create a method SearchMyClass inside of MyClass : 
&lt;P&gt;&lt;FONT color=#0080ff&gt;public MyClass SearchMyClass(int id)&lt;BR&gt;{&lt;/FONT&gt; 
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT color=#0080ff&gt;MyClass mclass = null;&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#0080ff&gt;foreach (MyClass mc in MyClasses)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; mclass = mc.SearchMyClass(id);&lt;BR&gt;}&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#0080ff&gt;return mclass;&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT color=#0080ff&gt;}&lt;/FONT&gt; 
&lt;P&gt;By calling SearchMyClass in a root object in a tree, method will iterate thru all instances of MyClasses collection and will call the same method in each of them, which means SearchMyClass method will be executed in each object in the tree. But what is the point of doing that, as you see it will return null at the end. Well, to get our searched object we have to add two condition statements into owr code: 
&lt;P&gt;&lt;FONT color=#0080ff&gt;public MyClass SearchMyClass(int id)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0080ff&gt;&lt;STRONG&gt;if (this.Id == id)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return this;&lt;/STRONG&gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MyClass mclass = null; &lt;/FONT&gt;
&lt;P&gt;&lt;FONT color=#0080ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (MyClass mc in MyClasses)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mclass = mc.SearchMyClass(id); &lt;/FONT&gt;
&lt;P&gt;&lt;FONT color=#0080ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#0080ff&gt;&lt;STRONG&gt; if (mclass != null)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;/FONT&gt;
&lt;P&gt;&lt;FONT color=#0080ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return mclass;&lt;BR&gt;}&lt;/FONT&gt; 
&lt;P&gt;Do you see magic like I see :). In the moment when method will find our object in the tree it will stop execution,&amp;nbsp;return searched object and break a current&amp;nbsp;foreach&amp;nbsp;iteration. As it will return searched object intrations in all upper level will stop as well. In case if such object does not exist it will iterate thru all objects in the tree and return null. As simple as that. 
&lt;P&gt;What about to grab all MyClass objects from all tree into one collection. Let’s try that.&lt;BR&gt;We will create new method called LoadMyClasses: 
&lt;P&gt;&lt;FONT color=#0080ff&gt;public void LoadMyClasses()&lt;BR&gt;{&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#0080ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (MyClass mc in MyClasses)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mc.LoadMyClasses();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt; 
&lt;P&gt;As you see it is method again iterates thru collection of MyClasses and executes the same method for each of the instances. But where the magic ? 
&lt;P&gt;Look at the method now: 
&lt;P&gt;&lt;FONT color=#0080ff&gt;public void LoadMyClasses(ref MyClassCollection collection)&lt;BR&gt;{&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#0080ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;collection.Add(this);&lt;/STRONG&gt;&lt;/FONT&gt; 
&lt;P&gt;&lt;FONT color=#0080ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (MyClass mc in MyClasses)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mc.LoadMyClasses(ref collection);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt; 
&lt;P&gt;First of all we have to specify the collection which we are planning to fill and the good approach by my opinion is to initialize an empty collection before calling the method and pass as reference with &lt;STRONG&gt;ref&lt;/STRONG&gt; keyword which insures that passed object should be initialized before calling a method and inside of the method you are working directly with initialized object but not with the copy of its pointer.&lt;BR&gt;Next, immediately during method execution we must add the object itself to the collection and then iterate thru its children. As you see all instances of the tree will be added to the collection. As simple as that again :) 
&lt;P&gt;The power of the Composite Design Pattern is get access to all objects in a tree by simple way and as you saw it is really simple. You can write various of methods which will search for specific data in tree, return some property value, etc, and by combining them you will get flexible way to deal with tree structure objects. 
&lt;P&gt;Have a fun. &lt;/P&gt;&lt;img src="http://blog.dotnetstyling.com/aggbug.aspx?PostID=13" width="1" height="1"&gt;</description><category domain="http://blog.dotnetstyling.com/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blog.dotnetstyling.com/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blog.dotnetstyling.com/archive/tags/Design+Patterns/default.aspx">Design Patterns</category><category domain="http://blog.dotnetstyling.com/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://blog.dotnetstyling.com/archive/tags/UML/default.aspx">UML</category></item></channel></rss>