Skip to content

Commit a8c4aba

Browse files
committed
add composite pattern example
1 parent 72a1ef0 commit a8c4aba

File tree

8 files changed

+158
-162
lines changed

8 files changed

+158
-162
lines changed

Assets/Structural Patterns/Composite Pattern/Exmaple1.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//-------------------------------------------------------------------------------------
2+
// CompositePatternExample1.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using System;
6+
using UnityEngine;
7+
using System.Collections;
8+
using System.Collections.Generic;
9+
10+
11+
//This real-world code demonstrates the Composite pattern used in building a graphical tree structure made up of primitive nodes(lines, circles, etc) and composite
12+
// nodes(groups of drawing elements that make up more complex elements).
13+
14+
namespace CompositePatternExample1
15+
{
16+
public class CompositePatternExample1 : MonoBehaviour
17+
{
18+
void Start()
19+
{
20+
// Create a tree structure
21+
CompositeElement root =
22+
new CompositeElement("Picture");
23+
root.Add(new PrimitiveElement("Red Line"));
24+
root.Add(new PrimitiveElement("Blue Circle"));
25+
root.Add(new PrimitiveElement("Green Box"));
26+
27+
// Create a branch
28+
CompositeElement comp =
29+
new CompositeElement("Two Circles");
30+
comp.Add(new PrimitiveElement("Black Circle"));
31+
comp.Add(new PrimitiveElement("White Circle"));
32+
root.Add(comp);
33+
34+
// Add and remove a PrimitiveElement
35+
PrimitiveElement pe =
36+
new PrimitiveElement("Yellow Line");
37+
root.Add(pe);
38+
root.Remove(pe);
39+
40+
// Recursively display nodes
41+
root.Display(1);
42+
43+
}
44+
}
45+
46+
47+
/// <summary>
48+
/// The 'Component' Treenode
49+
/// </summary>
50+
abstract class DrawingElement
51+
{
52+
protected string _name;
53+
54+
// Constructor
55+
public DrawingElement(string name)
56+
{
57+
this._name = name;
58+
}
59+
60+
public abstract void Add(DrawingElement d);
61+
public abstract void Remove(DrawingElement d);
62+
public abstract void Display(int indent);
63+
}
64+
65+
/// <summary>
66+
/// The 'Leaf' class
67+
/// </summary>
68+
class PrimitiveElement : DrawingElement
69+
{
70+
// Constructor
71+
public PrimitiveElement(string name)
72+
: base(name)
73+
{
74+
}
75+
76+
public override void Add(DrawingElement c)
77+
{
78+
Debug.Log("Cannot add to a PrimitiveElement");
79+
}
80+
81+
public override void Remove(DrawingElement c)
82+
{
83+
Debug.Log("Cannot remove from a PrimitiveElement");
84+
}
85+
86+
public override void Display(int indent)
87+
{
88+
Debug.Log(new String('-', indent) + " " + _name);
89+
}
90+
}
91+
92+
/// <summary>
93+
/// The 'Composite' class
94+
/// </summary>
95+
class CompositeElement : DrawingElement
96+
{
97+
private List<DrawingElement> elements =
98+
new List<DrawingElement>();
99+
100+
// Constructor
101+
public CompositeElement(string name)
102+
: base(name)
103+
{
104+
}
105+
106+
public override void Add(DrawingElement d)
107+
{
108+
elements.Add(d);
109+
}
110+
111+
public override void Remove(DrawingElement d)
112+
{
113+
elements.Remove(d);
114+
}
115+
116+
public override void Display(int indent)
117+
{
118+
Debug.Log(new String('-', indent) +
119+
"+ " + _name);
120+
121+
// Display each child element on this node
122+
foreach (DrawingElement d in elements)
123+
{
124+
d.Display(indent + 2);
125+
}
126+
}
127+
}
128+
}

Assets/Structural Patterns/Composite Pattern/Exmaple1/CompositePatternExample1.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Structural Patterns/Composite Pattern/Exmaple1/CompositePatternExample1.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Unity-Design-Pattern.CSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<Compile Include="Assets\Structural Patterns\Adapter Pattern\Structure\AdapterStructure.cs" />
135135
<Compile Include="Assets\Structural Patterns\Bridge Pattern\Exmaple1\BridgePatternExample1.cs" />
136136
<Compile Include="Assets\Structural Patterns\Bridge Pattern\Structure\BridgeStructure.cs" />
137+
<Compile Include="Assets\Structural Patterns\Composite Pattern\Exmaple1\CompositePatternExample1.cs" />
137138
<Compile Include="Assets\Structural Patterns\Composite Pattern\Structure\CompositeStructure.cs" />
138139
<Compile Include="Assets\Structural Patterns\Decorator Pattern\Structure\DecoratorStructure.cs" />
139140
<Compile Include="Assets\Structural Patterns\Facade Pattern\Structure\FacadeStructure.cs" />

Unity3D-Design-Patterns.CSharp.csproj

Lines changed: 0 additions & 142 deletions
This file was deleted.

Unity3D-Design-Patterns.sln

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)