diff --git a/DesignPatternGuru/Composite/Client.cs b/DesignPatternGuru/Composite/Client.cs new file mode 100644 index 0000000..3bc4b78 --- /dev/null +++ b/DesignPatternGuru/Composite/Client.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Composite +{ + internal class Client + { + public void ClientCode(Component leaf) + { + Console.WriteLine($"RESULT: {leaf.Operation()}"); + } + + public void ClientCode2(Component component1, Component component2) + { + if (component1.IsComposite()) + component1.Add(component2); + + Console.WriteLine($"RESULT: {component1.Operation()}"); + } + } +} diff --git a/DesignPatternGuru/Composite/Component.cs b/DesignPatternGuru/Composite/Component.cs new file mode 100644 index 0000000..1b10a41 --- /dev/null +++ b/DesignPatternGuru/Composite/Component.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Composite +{ + abstract class Component + { + public abstract string Operation(); + + public virtual void Add(Component component) + { + throw new NotImplementedException(); + } + + public virtual void Remove(Component component) + { + throw new NotImplementedException(); + } + + public virtual bool IsComposite() + { + return true; + } + } +} diff --git a/DesignPatternGuru/Composite/Composite.cs b/DesignPatternGuru/Composite/Composite.cs new file mode 100644 index 0000000..fd21163 --- /dev/null +++ b/DesignPatternGuru/Composite/Composite.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Composite +{ + internal class Composite : Component + { + protected List _children = new List(); + + public override void Add(Component component) + { + _children.Add(component); + } + + public override void Remove(Component component) + { + _children.Remove(component); + } + + public override string Operation() + { + int i = 0; + StringBuilder sb = new StringBuilder(); + sb.Append("Branch("); + + foreach (Component component in _children) + { + sb.Append(component.Operation()); + if (i != _children.Count - 1) + sb.Append("+"); + + i++; + } + + sb.Append(")"); + return sb.ToString(); + } + } +} diff --git a/DesignPatternGuru/Composite/Composite.csproj b/DesignPatternGuru/Composite/Composite.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/DesignPatternGuru/Composite/Composite.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/DesignPatternGuru/Composite/Leaf.cs b/DesignPatternGuru/Composite/Leaf.cs new file mode 100644 index 0000000..0f48fdb --- /dev/null +++ b/DesignPatternGuru/Composite/Leaf.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Composite +{ + internal class Leaf : Component + { + public override string Operation() + { + return "Leaf"; + } + + public override bool IsComposite() + { + return false; + } + } +} diff --git a/DesignPatternGuru/Composite/Program.cs b/DesignPatternGuru/Composite/Program.cs new file mode 100644 index 0000000..0a48dc3 --- /dev/null +++ b/DesignPatternGuru/Composite/Program.cs @@ -0,0 +1,36 @@ +namespace Composite +{ + internal class Program + { + static void Main(string[] args) + { + Client client = new Client(); + + // simple leaf + Leaf leaf = new Leaf(); + Console.WriteLine("Client: I get a simple component:"); + client.ClientCode(leaf); + Console.WriteLine(""); + + // complex composites + Composite tree = new Composite(); + + Composite branch1 = new Composite(); + branch1.Add(new Leaf()); + branch1.Add(new Leaf()); + + Composite branch2 = new Composite(); + branch2.Add(new Leaf()); + + tree.Add(branch1); + tree.Add(branch2); + Console.WriteLine("Client: Now I've got a composite tree:"); + client.ClientCode(tree); + Console.WriteLine(""); + + + Console.WriteLine("Client: I don't need to check the component classes even when managing the tree:"); + client.ClientCode2(tree, leaf); + } + } +} \ No newline at end of file diff --git a/DesignPatternGuru/DesignPatternGuru.sln b/DesignPatternGuru/DesignPatternGuru.sln index e85b917..c7b9a4d 100644 --- a/DesignPatternGuru/DesignPatternGuru.sln +++ b/DesignPatternGuru/DesignPatternGuru.sln @@ -17,9 +17,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton", "Singleton\Sing EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "StructuralPatterns", "StructuralPatterns", "{0B9333EC-FB73-4FEE-B600-9ECCD5A356B1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Adapter", "Adapter\Adapter.csproj", "{4E8B1160-0BD2-42FD-9DB8-991CAA88578A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adapter", "Adapter\Adapter.csproj", "{4E8B1160-0BD2-42FD-9DB8-991CAA88578A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bridge", "Bridge\Bridge.csproj", "{60D1EE6E-282D-4613-8B1D-DE355768DCBA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bridge", "Bridge\Bridge.csproj", "{60D1EE6E-282D-4613-8B1D-DE355768DCBA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Composite", "Composite\Composite.csproj", "{8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -55,6 +57,10 @@ Global {60D1EE6E-282D-4613-8B1D-DE355768DCBA}.Debug|Any CPU.Build.0 = Debug|Any CPU {60D1EE6E-282D-4613-8B1D-DE355768DCBA}.Release|Any CPU.ActiveCfg = Release|Any CPU {60D1EE6E-282D-4613-8B1D-DE355768DCBA}.Release|Any CPU.Build.0 = Release|Any CPU + {8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -67,6 +73,7 @@ Global {79AC3F3A-CE95-40C4-94E6-31CC85494E41} = {C470237E-3C3B-49E3-BA4C-86884EC28413} {4E8B1160-0BD2-42FD-9DB8-991CAA88578A} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} {60D1EE6E-282D-4613-8B1D-DE355768DCBA} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} + {8EAAC7FD-ED5E-4567-B1C1-F79AC79C9820} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC}