Начал изучать паттерны и первый который мне встретился это паттерн Компоновщик (Composite). Особенности паттерна относится к структурным паттернам, применяется для группировки объектов в древовидные структуры, позволяет клиентам работать с группой так же как и с одним. Ниже приведу реализацию на С++ и С#.
#include <iostream> #include <vector> using namespace std; class Component { public: virtual void traverse() = 0; }; class Leaf : public Component { int value; public: Leaf( int val ) { value = val; } void traverse() { cout << value << ' '; } }; class Composite : public Component { vector<Component*> children; public: void add( Component* ele ) { children.push_back( ele ); } void traverse() { for (int i=0; i < children.size(); i++) // Use polymorphism to delegate to children children[i]->traverse(); } }; void main( void ) { Composite containers[4]; //включаем листья в контейнер for (int i=0; i < 4; i++) for (int j=0; j < 3; j++) containers[i].add( new Leaf( i * 3 + j )); //containers[0]={0,1,2} //containers[1]={3,4,5} //containers[2]={6,7,8} //containers[3]={9,10,11} //включаем контейнеры в контейнер for (int i=1; i < 4; i++) containers[0].add( &(containers[i]) ); //containers[0]={0,1,2,3,4,5,6,7,8,9,10,11} for (int i=0; i < 4; i++) { containers[i].traverse(); cout << endl; } Composite cont[3]; for (int i=0; i < 4; i++){cont[0].add( new Leaf(i));} for (int i=4; i < 8; i++){cont[1].add( new Leaf(i));} for (int i=8; i < 14; i++){cont[2].add( new Leaf(i));} for (int i=0; i < 3; i++) { cont[i].traverse(); cout << endl; } } // 0 1 2 3 4 5 6 7 8 9 10 11 // 3 4 5 // 6 7 8 // 9 10 11
Этот код не мой, я его взял с RSDN когда гуглил на тему этого паттерна. Ниже код на шарпе, уже сделан по аналогии, с тем что представлен на C++
public interface Component { void Traverse(string pref); Component GetChild(int i); } class Composite : Component { private List<Component> children = new List<Component>(); public void Add(Component comp) { this.children.Add(comp); } public Component GetChild(int i) { return this.children[i]; } public void SetChild(int i,Component item) { this.children.Insert(i, item); } public void Traverse(string pref) { pref += "+"; Console.Write(pref); for (int i = 0; i < this.children.Count; i++) children[i].Traverse(pref); Console.WriteLine(); } } class Leaf : Component { private int val; public Leaf(int value) { this.val = value; } public void Traverse(string pref) { Console.Write(val.ToString() + " "); } public Component GetChild(int i) { return this; } } class Program { static void Main(string[] args) { Composite comp = new Composite(); comp.Add(new Leaf(10)); comp.Add(new Leaf(11)); comp.Add(new Leaf(12)); comp.Add(new Leaf(13)); comp.Add(new Leaf(14)); comp.Traverse(""); Composite comp2 = new Composite(); comp2.Add(comp); comp2.Add(comp); comp2.Add(new Leaf(99)); Console.WriteLine(); Console.WriteLine("---"); comp2.Traverse(""); Console.WriteLine(); Console.WriteLine("---"); Composite comp3 = new Composite(); comp3.Add(comp2); comp3.Traverse(""); Console.WriteLine(); Console.WriteLine("---"); Composite comp4 = new Composite(); comp4.Add(new Leaf(19)); comp.SetChild(2, comp4); comp.Traverse(""); Console.ReadLine(); }
Оставить комментарий