Компоновщик (Composite) — различия между версиями
Материал из Вики ИТ мехмата ЮФУ
Admin (обсуждение | вклад) (→Назначение) |
Admin (обсуждение | вклад) (→Пример) |
||
Строка 15: | Строка 15: | ||
=== Пример === | === Пример === | ||
<source lang="Csharp"> | <source lang="Csharp"> | ||
+ | class MainApp | ||
+ | { | ||
+ | static void Main() | ||
+ | { | ||
+ | Composite root = new Composite("root"); | ||
+ | root.Add(new Leaf("Leaf A")); | ||
+ | root.Add(new Leaf("Leaf B")); | ||
+ | |||
+ | Composite comp = new Composite("Composite X"); | ||
+ | comp.Add(new Leaf("Leaf XA")); | ||
+ | comp.Add(new Leaf("Leaf XB")); | ||
+ | |||
+ | root.Add(comp); | ||
+ | root.Add(new Leaf("Leaf C")); | ||
+ | |||
+ | root.Display(1); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | abstract class Component | ||
+ | { | ||
+ | protected string name; | ||
+ | |||
+ | public Component(string name) | ||
+ | { | ||
+ | this.name = name; | ||
+ | } | ||
+ | |||
+ | public abstract void Add(Component c); | ||
+ | public abstract void Remove(Component c); | ||
+ | public abstract void Display(int depth); | ||
+ | } | ||
+ | |||
+ | class Composite : Component | ||
+ | { | ||
+ | List<Component> children = new List<Component>(); | ||
+ | |||
+ | // Constructor | ||
+ | public Composite(string name): base(name) | ||
+ | { } | ||
+ | |||
+ | public override void Add(Component component) | ||
+ | { | ||
+ | children.Add(component); | ||
+ | } | ||
+ | |||
+ | public override void Remove(Component component) | ||
+ | { | ||
+ | children.Remove(component); | ||
+ | } | ||
+ | |||
+ | public override void Display(int depth) | ||
+ | { | ||
+ | Console.WriteLine(new String('-', depth) + name); | ||
+ | foreach (Component component in children) | ||
+ | component.Display(depth + 2); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | class Leaf : Component | ||
+ | { | ||
+ | public Leaf(string name): base(name) | ||
+ | { } | ||
+ | |||
+ | public override void Add(Component c) | ||
+ | { | ||
+ | throw new Exception("Cannot add to a leaf"); | ||
+ | } | ||
+ | |||
+ | public override void Remove(Component c) | ||
+ | { | ||
+ | throw new Exception("Cannot remove from a leaf"); | ||
+ | } | ||
+ | |||
+ | public override void Display(int depth) | ||
+ | { | ||
+ | Console.WriteLine(new String('-', depth) + name); | ||
+ | } | ||
+ | } | ||
</source> | </source> | ||
Версия 15:12, 3 августа 2014
Назначение
Объединяет объекты в древовидные структуры. Единообразно трактует простые и составные объекты.
Описание
Реализация
Диаграмма классов
Участники
Пример
class MainApp
{
static void Main()
{
Composite root = new Composite("root");
root.Add(new Leaf("Leaf A"));
root.Add(new Leaf("Leaf B"));
Composite comp = new Composite("Composite X");
comp.Add(new Leaf("Leaf XA"));
comp.Add(new Leaf("Leaf XB"));
root.Add(comp);
root.Add(new Leaf("Leaf C"));
root.Display(1);
}
}
abstract class Component
{
protected string name;
public Component(string name)
{
this.name = name;
}
public abstract void Add(Component c);
public abstract void Remove(Component c);
public abstract void Display(int depth);
}
class Composite : Component
{
List<Component> children = new List<Component>();
// Constructor
public Composite(string name): base(name)
{ }
public override void Add(Component component)
{
children.Add(component);
}
public override void Remove(Component component)
{
children.Remove(component);
}
public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name);
foreach (Component component in children)
component.Display(depth + 2);
}
}
class Leaf : Component
{
public Leaf(string name): base(name)
{ }
public override void Add(Component c)
{
throw new Exception("Cannot add to a leaf");
}
public override void Remove(Component c)
{
throw new Exception("Cannot remove from a leaf");
}
public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name);
}
}