mirror of
https://github.com/farcasclaudiu/design_pattens.git
synced 2026-06-28 21:01:25 +03:00
composite design pattern
This commit is contained in:
+3
-1
@@ -9,6 +9,7 @@ using design_patterns.creational.singleton;
|
|||||||
using design_patterns.creational.prototype;
|
using design_patterns.creational.prototype;
|
||||||
using design_patterns.structural.adapter;
|
using design_patterns.structural.adapter;
|
||||||
using design_patterns.structural.bridge;
|
using design_patterns.structural.bridge;
|
||||||
|
using design_patterns.structural.composite;
|
||||||
|
|
||||||
namespace design_patterns
|
namespace design_patterns
|
||||||
{
|
{
|
||||||
@@ -29,7 +30,8 @@ namespace design_patterns
|
|||||||
|
|
||||||
// structural
|
// structural
|
||||||
// await AdapterSample.Run();
|
// await AdapterSample.Run();
|
||||||
await BridgeSample.Run();
|
// await BridgeSample.Run();
|
||||||
|
await CompositeSample.Run();
|
||||||
}
|
}
|
||||||
catch (System.Exception ex)
|
catch (System.Exception ex)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace design_patterns.structural.composite
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 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.
|
||||||
|
/// </summary>
|
||||||
|
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<List<IPriceComputable>> _components { get; set; } = new Lazy<List<IPriceComputable>>();
|
||||||
|
public List<IPriceComputable> 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";
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user