The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object.
The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly. An XmlDocument,TreeView are example of composite pattern.
public class TreeNode
{
priavte String m_sName = null;
priavte Arraylist m_arlstChilds = null;
public TreeNode()
{
m_arlstChilds=new Arraylist();
}
public String Name
{
get
{
return m_sName;
}
set
{
m_sName=value;
}
}
private String m_sValue = null;
public String Value
{
get
{
return m_sValue;
}
set
{
m_sValue=value;
}
}
public void AddNode(TreeNode objTreeNode)
{
m_arlsChilds.Add(objTreeNode);
}
}
public class CreateTree
{
TreeNode BookNode = new TreeNode(“Bible”,”Book”);
TreeNode ChapterNode1 = new TreeNode(“Chapter1”,”Chapter1”);
TreeNode ChapterNode2 = new TreeNode(“Chapter2”,”Chapter2”);
TreeNode ChapterNode3 = new TreeNode(“Chapter3”,”Chapter3”);
TreeNode SectionNode1 = new TreeNode(“Section1”,”Section1”);
TreeNode SectionNode2 = new TreeNode(“Section2”,”Section2”);
BookNode.Add(ChapterNode1);
BookNode.Add(ChapterNode2);
BookNode.Add(ChapterNode3);
ChapterNode1.Add(SectionNode1);
ChapterNode1.Add(SectionNode2);
}
Thus the Composite pattern allows you to create a tree like structure for simple and complex objects so they appear the same to the client.
The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly. An XmlDocument,TreeView are example of composite pattern.
public class TreeNode
{
priavte String m_sName = null;
priavte Arraylist m_arlstChilds = null;
public TreeNode()
{
m_arlstChilds=new Arraylist();
}
public String Name
{
get
{
return m_sName;
}
set
{
m_sName=value;
}
}
private String m_sValue = null;
public String Value
{
get
{
return m_sValue;
}
set
{
m_sValue=value;
}
}
public void AddNode(TreeNode objTreeNode)
{
m_arlsChilds.Add(objTreeNode);
}
}
public class CreateTree
{
TreeNode BookNode = new TreeNode(“Bible”,”Book”);
TreeNode ChapterNode1 = new TreeNode(“Chapter1”,”Chapter1”);
TreeNode ChapterNode2 = new TreeNode(“Chapter2”,”Chapter2”);
TreeNode ChapterNode3 = new TreeNode(“Chapter3”,”Chapter3”);
TreeNode SectionNode1 = new TreeNode(“Section1”,”Section1”);
TreeNode SectionNode2 = new TreeNode(“Section2”,”Section2”);
BookNode.Add(ChapterNode1);
BookNode.Add(ChapterNode2);
BookNode.Add(ChapterNode3);
ChapterNode1.Add(SectionNode1);
ChapterNode1.Add(SectionNode2);
}
Thus the Composite pattern allows you to create a tree like structure for simple and complex objects so they appear the same to the client.
No comments:
Post a Comment