static void Main(string[] args) { /* * More info: https://en.wikipedia.org/wiki/Composite_pattern * What problems can the Composite design pattern solve? * * A part-whole hierarchy should be represented so that clients can treat part and whole objects uniformly. * A part-whole hierarchy should be represented as tree structure. * * When defining (1) Part objects and (2) Whole objects that act as containers for Part objects, * clients must treat them separately, which complicates client code. */ CompositeGraphic root = new CompositeGraphic("root"); root.Add(new Leaf("Leaf A")); root.Add(new Leaf("Leaf B")); CompositeGraphic comp = new CompositeGraphic("Composite X"); comp.AddRange(new Leaf("Leaf XA"), new Leaf("Leaf XB")); root.Add(comp); root.Add(new Leaf("Leaf C")); Leaf leaf = new Leaf("Leaf D"); root.Add(leaf); root.Remove(leaf); root.Display(1); Console.ReadKey(); }
static void Main(string[] args) { var ellipse1 = new Ellipse(); var ellipse2 = new Ellipse(); var ellipse3 = new Ellipse(); var ellipse4 = new Ellipse(); var graphic = new CompositeGraphic(); var graphic1 = new CompositeGraphic(); var graphic2 = new CompositeGraphic(); graphic1.Add(ellipse1); graphic1.Add(ellipse2); graphic1.Add(ellipse3); graphic2.Add(ellipse4); graphic.Add(graphic1); graphic.Add(graphic2); graphic.Print(); Console.ReadKey(); }
private static void Example() { var composite1 = new CompositeGraphic(); composite1.Add(new Rectangle(new Point(3, 4), new Size(6, 1))); composite1.Add(new Ellipse(new Point(7, 8), 1, 1)); // a circle var composite2 = new CompositeGraphic(); composite2.AddRange(new IGraphic[] { new Ellipse(new Point(1, 0), 6, 9), new Ellipse(new Point(0, -4), 3, 7), new Rectangle(new Point(5, 2), new Size(7, 4)) }); var composite3 = new CompositeGraphic(); composite3.AddRange(new IGraphic[] { composite1, composite2 }); composite3.Draw(); }