Composite Pattern - Example

Example

The following example, written in Java, implements a graphic class, which can be either an ellipse or a composition of several graphics. Every graphic can be printed. In algebraic form,

Graphic = ellipse | GraphicList GraphicList = empty | Graphic GraphicList

It could be extended to implement several other shapes (rectangle, etc.) and methods (translate, etc.).

/** "Component" */ interface Graphic { //Prints the graphic. public void print; } /** "Composite" */ import java.util.List; import java.util.ArrayList; class CompositeGraphic implements Graphic { //Collection of child graphics. private List childGraphics = new ArrayList; //Prints the graphic. public void print { for (Graphic graphic : childGraphics) { graphic.print; } } //Adds the graphic to the composition. public void add(Graphic graphic) { childGraphics.add(graphic); } //Removes the graphic from the composition. public void remove(Graphic graphic) { childGraphics.remove(graphic); } } /** "Leaf" */ class Ellipse implements Graphic { //Prints the graphic. public void print { System.out.println("Ellipse"); } } /** Client */ public class Program { public static void main(String args) { //Initialize four ellipses Ellipse ellipse1 = new Ellipse; Ellipse ellipse2 = new Ellipse; Ellipse ellipse3 = new Ellipse; Ellipse ellipse4 = new Ellipse; //Initialize three composite graphics CompositeGraphic graphic = new CompositeGraphic; CompositeGraphic graphic1 = new CompositeGraphic; CompositeGraphic graphic2 = new CompositeGraphic; //Composes the graphics graphic1.add(ellipse1); graphic1.add(ellipse2); graphic1.add(ellipse3); graphic2.add(ellipse4); graphic.add(graphic1); graphic.add(graphic2); //Prints the complete graphic (four times the string "Ellipse"). graphic.print; } }

The following example, written in C#.

namespace CompositePattern { using System; using System.Collections.Generic; using System.Linq; //Client class Program { static void Main(string args) { // initialize variables var compositeGraphic = new CompositeGraphic; var compositeGraphic1 = new CompositeGraphic; var compositeGraphic2 = new CompositeGraphic; //Add 1 Graphic to compositeGraphic1 compositeGraphic1.Add(new Ellipse); //Add 2 Graphic to compositeGraphic2 compositeGraphic2.AddRange(new Ellipse, new Ellipse); /*Add 1 Graphic, compositeGraphic1, and compositeGraphic2 to compositeGraphic */ compositeGraphic.AddRange(new Ellipse, compositeGraphic1, compositeGraphic2); /*Prints the complete graphic (four times the string "Ellipse").*/ compositeGraphic.Print; Console.ReadLine; } } //Component public interface IGraphic { void Print; } //Leaf public class Ellipse : IGraphic { //Prints the graphic public void Print { Console.WriteLine("Ellipse"); } } //Composite public class CompositeGraphic : IGraphic { //Collection of Graphics. private readonly List _Graphics; //Constructor public CompositeGraphic { //initialize generic Colleciton(Composition) _Graphics = new List; } //Adds the graphic to the composition public void Add(IGraphic graphic) { _Graphics.Add(graphic); } //Adds multiple graphics to the composition public void AddRange(params IGraphic graphic) { _Graphics.AddRange(graphic); } //Removes the graphic from the composition public void Delete(IGraphic graphic) { _Graphics.Remove(graphic); } //Prints the graphic. public void Print { foreach (var childGraphic in _Graphics) { childGraphic.Print; } } } }

Read more about this topic:  Composite Pattern

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)