Welcome to the Design Patterns for Beginners article aeries. In this article we will be learning how to implement a Composite Design Pattern in applications. From the past few articles I have been discussing various design patterns. If you are rely passionate about learning design pattern then please visit my previous articles:
- Design Pattern For Beginners - Part 1: Singleton Design Pattern
- Design Pattern For Beginners - Part 2: Factory Design Pattern
- Design Pattern For Beginners - Part 3: Prototype Design Pattern
- Design Pattern For Beginners - Part 4: Decorator Design Pattern
- Design Pattern For Beginners - Part 5: Composite Design Pattern
- Design Pattern For Beginners - Part 6: Adaptor Design Pattern
- Design Pattern For Beginners - Part 7: Bridge Design Pattern
- Design Pattern For Beginners - Part 8: memento Design Pattern
- Design Pattern for Beginners - Part-9: Strategy Design Pattern
- Design Pattern for Beginners - Part-10: Observer Design Pattern
- Design Pattern For Beginners - Part 11: Implement Decouple Classes in Application
In today's session we will learn about the Composite Design Pattern and we will discuss a few scenarios where the Composite Design Pattern may rescue you. At first we will discuss why the Composite Design Pattern is needed.
What the Composite Design Pattern Is
The Composite Design Pattern always forms a tree structure with the objects, where each root element contains sub nodes called child nodes and child nodes contain leaf nodes (last label node). Leaf nodes do not contain any elements. Now, in this object hierarchy we can point any node and from this node we can traverse all other nodes.
For example, think about an employee stricture of an organization. We can easily represent it by a tree structure and if we start from the CEO (the root of the organization) then we can reach leaf employees by traversing through the middle-level managers.
Take another example of a Composite Design Pattern. Think about the folder structure of your computer. Each drive contains many folders and each folder contains many subfolders and each subfolder contains many files. Here the files are nothing but the leaf nodes. A node from any drive partition you may traverse down to any file by a couple of mouse clicks.
Why composite pattern?
The Composite Design Pattern is useful when individual objects as well as a group of those kinds of objects are treated uniformly.
Hmm.., bookish definition. Let's think of a scenario where we want to draw a few shapes in the same canvas. And we will draw all the components in the same way (by calling a respective function). We will try to implement it with a small working example. Have a look at the following code:
- using System;
- using System.Collections;
- using System.Globalization;
- using System.Data.SqlClient;
- using System.Data;
- namespace Test1
- {
- interface IDraw
- {
- void Draw();
- }
- class Circle : IDraw
- {
- public void Draw()
- {
- Console.WriteLine("I am Circl");
- }
- }
- class Square: IDraw
- {
- public void Draw()
- {
- Console.WriteLine("I am Square");
- }
- }
- class Oval : IDraw
- {
- public void Draw()
- {
- Console.WriteLine("I am Oval");
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- ArrayList objList = new ArrayList();
- IDraw objcircl = new Circle();
- IDraw objSquare = new Square();
- IDraw objOval = new Oval();
- objList.Add(objcircl);
- objList.Add(objSquare);
- objList.Add(objOval);
- foreach (IDraw obj in objList)
- {
- obj.Draw();
- }
- Console.ReadLine();
- }
- }
- }




Join the conversation! Your thoughts help the community grow.