using System.Collections.Generic;
public interface IComponent
public class Leaf : IComponent
public int Price { get; set; }
public string Name { get; set; }
public Leaf(string name, int price)
public void DisplayPrice()
Console.WriteLine(Name + ": " + Price);
public class Composite : IComponent
public string Name { get; set; }
List<IComponent> components = new List<IComponent>();
public Composite(string name)
public void AddComponent(IComponent component)
components.Add(component);
public void DisplayPrice()
foreach (var item in components)
public static void Main(string[] args)
IComponent hardDisk = new Leaf("Hard Disk", 100);
IComponent cpu = new Leaf("CPU", 140);
IComponent ram = new Leaf("RAM", 80);
IComponent mouse = new Leaf("Mouse", 10);
IComponent keyboard = new Leaf("Keyboard", 30);
Composite computer = new Composite("Computer");
Composite cabinet = new Composite("Cabinet has: ");
Composite motherBoard = new Composite("Mother board has:");
Composite peripherals = new Composite("Peripherals");
cabinet.AddComponent(hardDisk);
cabinet.AddComponent(motherBoard);
motherBoard.AddComponent(cpu);
motherBoard.AddComponent(ram);
peripherals.AddComponent(mouse);
peripherals.AddComponent(keyboard);
peripherals.DisplayPrice();