Compound word (draft pattern)

from Wikipedia, the free encyclopedia

The composite ( English composite or whole-part ) is a design pattern from the field of software development , which belongs to the category of structural patterns (English structural patterns ). It is what is known as a GoF design pattern . The composite pattern is used to represent part-whole hierarchies by assembling objects into tree structures. The basic idea of ​​the composition pattern is to represent both primitive objects and their containers in an abstract class. This means that both individual objects and their compositions can be treated uniformly.

use

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

A typical example of a compound are hierarchical file systems , in particular their representation within file managers or file browsers as directories and files.

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

UML diagrams

Class diagram

Class diagram

Object diagram

Object diagram

Components

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

This Blattrepresents a single object, it has no child objects and is, for example, a file in a file directory .

This Kompositumcontains components, i.e. additional compounds or sheets, as child objects and represents a directory, for example.

advantages

  • uniform treatment of primitives and compositions
  • easy expandability to include new sheet or container classes

disadvantage

A design that is too general makes it difficult to restrict compositions to certain classes (and thus mostly types). The type system of the programming language then no longer offers any help, so that type checks are necessary at runtime .

example

Java's AWT classes are built according to the compound phrase. Since they all inherit from containers, they can each accept elements again themselves.

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

Further figures (rectangle etc.) or further methods (such as "rotate") could be implemented.

C ++

class Grafik {
public:
    virtual void print() const = 0;
    virtual ~Grafik() {} // Abstrakte Klassen, von denen geerbt wird, sollten immer einen virtuellen Destruktor haben
};

class GrafikKompositum : public Grafik {
    std::set<Grafik const*> children;
    typedef std::set<Grafik const*>::const_iterator grIter;
public:
    void print() const {
        for (grIter it = children.begin(); it != children.end(); it++) (*it)->print();
    }

    void add(Grafik const* component) {
        children.insert(component);
    }

    void remove(Grafik const* component) {
        children.erase(component);
    }
};

class Ellipse: public Grafik {
public:
    void print() const {
        std::cout << "Ellipse" << std::endl;
    }
};

int main() {
    Ellipse ellipse1, ellipse2, ellipse3, ellipse4;

    GrafikKompositum grafik1, grafik2, grafikGesamt;

    grafik1.add(&ellipse1);
    grafik1.add(&ellipse2);
    grafik1.add(&ellipse3);
    grafik2.add(&ellipse4);

    grafikGesamt.add(&grafik1);
    grafikGesamt.add(&grafik2);

    grafikGesamt.print();
}

Java

/** "Komponente" */
interface Graphic {

    //Prints the graphic.
     void print();
}

/** "Komposition" */
class CompositeGraphic implements Graphic {

    //Collection of child graphics.
    private List<Graphic> childGraphics = new ArrayList<Graphic>();

    //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();
    }
}

Use in analysis

A compound is also interesting as a pure data pattern without operations being defined in the classes, since it can be used to represent general tree structures. Therefore, this pattern can also be used in the analysis, e.g. B. to display nested orders or contractors (with subcontractors / subcontractors), nested processes, hierarchical groups of things (user groups, e-mail lists, article groups, organizational groups) etc. However, it must be ensured whether such hierarchies are actually uniform are, or whether the inner levels have different technical meanings. The latter is expressed e.g. B. in that terms such as "group" and "subgroup" are technically differentiated.

Related design patterns

  • Visitor
  • Decorator
  • A design based on the command pattern can often also usefully contain compound commands that are structured according to the compound phrase.

Web links

Individual evidence

  1. Erich Gamma , Richard Helm , Ralph Johnson , John Vlissides : Design pattern . 5th edition. Addison-Wesley , 1996, ISBN 3-8273-1862-9 , pp. 239 .