diff --git a/Program.cs b/Program.cs
index 16ec312..ad420cf 100644
--- a/Program.cs
+++ b/Program.cs
@@ -9,6 +9,7 @@ using design_patterns.creational.singleton;
using design_patterns.creational.prototype;
using design_patterns.structural.adapter;
using design_patterns.structural.bridge;
+using design_patterns.structural.composite;
namespace design_patterns
{
@@ -29,7 +30,8 @@ namespace design_patterns
// structural
// await AdapterSample.Run();
- await BridgeSample.Run();
+ // await BridgeSample.Run();
+ await CompositeSample.Run();
}
catch (System.Exception ex)
{
diff --git a/structural/composite/CompositeSample.cs b/structural/composite/CompositeSample.cs
new file mode 100644
index 0000000..de87658
--- /dev/null
+++ b/structural/composite/CompositeSample.cs
@@ -0,0 +1,97 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace design_patterns.structural.composite
+{
+ ///
+ /// Composite is a structural design pattern that lets you
+ /// compose objects into tree structures and then
+ /// work with these structures as if they were individual objects.
+ ///
+ /// - makes sense only when the core model of your app
+ /// can be represented as a tree.
+ ///
+ public class CompositeSample
+ {
+ public static async Task Run()
+ {
+ Console.WriteLine("Structural - Composite");
+
+ var computer = new Computer("IMB PC") {
+ Components = {
+ new ComponentGroup {
+ Name = "disk drives",
+ Components = {
+ new Component("HDD1", "Primary hard drive", 50.44m),
+ new Component("HDD2", "Secondary hard drive", 33.10m)
+ }
+ },
+ new Component ("CPU", "Pentium i9", 120m),
+ new GraphicCardNvidiaComponent(160m)
+ }
+ };
+
+ System.Console.WriteLine($"computer price is : {computer.GetPrice()}");
+
+ }
+
+ public interface IPriceComputable
+ {
+ public decimal GetPrice();
+ }
+ public class Computer : ComponentGroup
+ {
+ public Computer(string computerName)
+ {
+ this.Name = computerName;
+ }
+ }
+
+ public class Component : IPriceComputable
+ {
+ public Component(decimal price)
+ {
+ this.Price = price;
+ }
+ public Component(string code, string name, decimal price)
+ {
+ this.Code = code;
+ this.Name = name;
+ this.Price = price;
+ }
+ public virtual string Code { get; set; }
+ public virtual string Name { get; set; }
+ public virtual decimal Price { get; set; }
+
+ public decimal GetPrice()
+ {
+ return this.Price;
+ }
+ }
+
+ public class ComponentGroup : IPriceComputable
+ {
+ public virtual string Name { get; set; }
+
+ private Lazy> _components { get; set; } = new Lazy>();
+ public List Components { get { return _components.Value; } }
+
+ public decimal GetPrice()
+ {
+ return Components.Select(p=>p.GetPrice()).Sum();
+ }
+ }
+
+ public class GraphicCardNvidiaComponent : Component
+ {
+ public GraphicCardNvidiaComponent(decimal price) : base(price)
+ {
+ }
+ public override string Code { get; set; } = "GPU_Nvidia";
+ public override string Name { get; set; } = "RTX 3080";
+
+ }
+ }
+}
\ No newline at end of file