Composite pattern

The Composite (English composite or whole- part) is a design pattern in the field of software development and belongs to the category of structural patterns ( structural patterns ). It is a so-called GoF design patterns. The composition pattern (composite pattern ) is used to represent part-whole hierarchies by objects are combined into tree structures. The basic idea of the Composite pattern is to represent both primitive objects and their containers in an abstract class. Thus, both individual objects and their compositions can be treated uniformly.

  • 6.1 C
  • 6.2 Java

Use

  • Implementation of part-whole hierarchies.
  • Hide the differences between individual and composite objects.

A classic example of a composite are hierarchical file systems, in particular their representation within file manager or file browser as directories and files.

A modern example is the class definitions of the graphical user interface of Java. All elements such as buttons and text fields are specializations of the class Component. However, the containers for these elements are also specializations of the same class. In other words, all standard elements are substantially defined by a single ( compound ) class.

UML diagrams

Class Diagram

Object diagram

Components

The component defines a base class the common behavior of all participants. It is generally abstract and, for example, a directory entry.

The leaf represents a single object, it has no child objects and is, for example, in a file directory to a file.

The Composite contains components, such as other composites or leaves, as a child objects and represented as a directory.

Benefits

  • Uniform treatment of primitives and compositions
  • Easy extensibility to new leaf or container classes

Disadvantages

A too common design makes it difficult compositions to certain classes (and thus mostly types) to restrict. The type system of the programming language then provides no help, so that type checking at runtime are required.

Example

Java AWT classes are built according to the Composite pattern. Since all inherit from container, they can each accommodate itself again elements.

The following example consists of a graphics class; a graphic can be an ellipse or a composition of many graphics. Each graph implements a method for printing.

It could, or other methods ( " Rotate " about ) can be implemented even more figures (square, etc.).

C

Class graph { public:    virtual void print () const = 0;    virtual ~ Graph () { } / / Abstract classes, one of which is inherited should always have a virtual destructor };   GrafikKompositum class: public Vector { private:    std :: set children;    typedef std :: set :: const_iterator grIter; public:    void print () const {        for ( it = grIter children.begin (); it = children.end (); it ! ) ( * it ) -> print ();    }      void add ( const Graphics * component) {        children.insert (component );    }      void remove ( const Graphics * component) {        children.erase (component );    } };   Ellipse class: public Vector { public:    void print () const {        std :: cout << " ellipse " << std :: endl;    } };   int main (void ) {     Ellipse ellipse1, ellipse2, ellipse3, ellipse4;       GrafikKompositum grafik1, grafik2, graphic total;       grafik1.add ( & ellipse1 );     grafik1.add ( & ellipse2 );     grafik1.add ( & ellipse3 );     grafik2.add ( & ellipse4 );       grafikGesamt.add ( & grafik1 );     grafikGesamt.add ( & grafik2 );       grafikGesamt.print ();     return 0; } Java

Graphic interface {      public void print (); }   class implements Graphic { GrafikKompositum      final private List children = new ArrayList (10);        @ Override      public void print () {          for ( final graphic graphic: children ) {              grafik.print ();          }      }        public void add ( final graphic component) {          children.add (component );      }        public void remove ( final graphic component) {          children.remove (component );      } }   class Ellipse implements Graphic {      @ Override      public void print () {          System.out.println ( "Ellipse ");      } };   public class Test { graphics      public static void main ( final String ... args) {          Ellipse1 ellipse ellipse = new ()                  ellipse2 = new Ellipse ()                  ellipse3 = new Ellipse ()                  ellipse4 = new Ellipse ();            GrafikKompositum grafik1 = new GrafikKompositum ()                  grafik2 = new GrafikKompositum ()                  graphic total = new GrafikKompositum ();            grafik1.add ( ellipse1 );          grafik1.add ( ellipse2 );          grafik1.add ( ellipse3 );          grafik2.add ( ellipse4 );            grafikGesamt.add ( grafik1 );          grafikGesamt.add ( grafik2 );            grafikGesamt.print ();      }        private video test () {      } } Use in the analysis

A compound is of interest as a pure data pattern without operations are defined in the class, as it can be used for representing a general tree structures. Therefore, this pattern is useful can also be used in the analysis, eg, to represent nested orders or contractor ( with subcontracts / subcontractors), nested processes, hierarchical groups of things ( user groups, e -mail lists, item groups, organizational networks ), etc.. however, care must be taken, whether such hierarchies are actually uniform, or whether the inner planes have different technical meaning. The latter is expressed, for example, is from that terms such as " group " and " group " are technically distinguished.

Related design patterns

  • Visitor
  • Decorator
  • A design based on the Command pattern can contain often usefully also composite commands that are structured according to the Composite pattern.
199373
de