composite pattern

main
syneffort 2 years ago
parent 6f69b357dd
commit ab7df4e5c4
  1. 24
      DesignPatternGuru/Composite/Client.cs
  2. 28
      DesignPatternGuru/Composite/Component.cs
  3. 42
      DesignPatternGuru/Composite/Composite.cs
  4. 10
      DesignPatternGuru/Composite/Composite.csproj
  5. 21
      DesignPatternGuru/Composite/Leaf.cs
  6. 36
      DesignPatternGuru/Composite/Program.cs
  7. 11
      DesignPatternGuru/DesignPatternGuru.sln

@ -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()}");
}
}
}

@ -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;
}
}
}

@ -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<Component> _children = new List<Component>();
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();
}
}
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -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;
}
}
}

@ -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);
}
}
}

@ -17,9 +17,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton", "Singleton\Sing
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "StructuralPatterns", "StructuralPatterns", "{0B9333EC-FB73-4FEE-B600-9ECCD5A356B1}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "StructuralPatterns", "StructuralPatterns", "{0B9333EC-FB73-4FEE-B600-9ECCD5A356B1}"
EndProject 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 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 EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution 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}.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.ActiveCfg = Release|Any CPU
{60D1EE6E-282D-4613-8B1D-DE355768DCBA}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -67,6 +73,7 @@ Global
{79AC3F3A-CE95-40C4-94E6-31CC85494E41} = {C470237E-3C3B-49E3-BA4C-86884EC28413} {79AC3F3A-CE95-40C4-94E6-31CC85494E41} = {C470237E-3C3B-49E3-BA4C-86884EC28413}
{4E8B1160-0BD2-42FD-9DB8-991CAA88578A} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1} {4E8B1160-0BD2-42FD-9DB8-991CAA88578A} = {0B9333EC-FB73-4FEE-B600-9ECCD5A356B1}
{60D1EE6E-282D-4613-8B1D-DE355768DCBA} = {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 EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC} SolutionGuid = {3E5A9C6B-2E8A-466E-B5E0-4379902EFFAC}

Loading…
Cancel
Save