eEcho blog

is een halte van de gedachte

Composite pattern

In de informatica, het samengestelde patroon is een opdeling design patroon. In composiet kan een groep van objecten op dezelfde wijze behandeld worden als een enkel object. De bedoeling van de compositie is “componeren objecten in de boom structuren te vertegenwoordigen deel-geheel hiërarchieën. Composite laat clients behandelen individuele objecten en composities op uniforme wijze.”

Motivation

Bij de behandeling van de boom-gestructureerde gegevens, programmeurs hebben vaak te maken tussen een blad-node en een kantoor. Dit maakt code complexer, en dus kans op fouten. De oplossing is een interface die het mogelijk maakt de behandeling van complexe en primitieve voorwerpen uniform. In een object-georiënteerd programmeren, een samengestelde is een object (bijvoorbeeld een vorm) ontworpen als een samenstelling van een-of meer-vergelijkbare objecten (andere vormen / geometrieën), alle standhouders soortgelijke functionaliteit. Dit staat bekend als een ‘is-een “relatie tussen objecten. De sleutel-concept is dat je kunt manipuleren een enkel geval van het object, net zoals je je een groep van hen. De acties die u kunt uitvoeren op alle samengestelde materialen hebben vaak een gemeenschappelijke noemer minste relatie. Als bijvoorbeeld het definiëren van een systeem een beeld te schetsen gegroepeerd vormen op een scherm, het nuttig zou zijn om het formaat een groep vormen hetzelfde effect te hebben (in zekere zin) als het formaat een enkele vorm.

When to use
Composite can be used when clients should ignore the difference between compositions of objects and individual objects. If programmers find that they are using multiple objects in the same way, and often have nearly identical code to handle each of them, then composite is a good choice; it is less complex in this situation to treat primitives and composites as homogeneous.

800px-Composite_UML_class_diagram.svg.jpg

Component

* is the abstraction for all components, including composite ones
* declares the interface for objects in the composition
* implements default behavior for the interface common to all classes, as appropriate
* declares an interface for accessing and managing its child components
* (optional) defines an interface for accessing a component’s parent in the recursive structure, and implements it if that’s appropriate

Leaf

* represents leaf objects in the composition
* implements all Component methods

Composite

* represents a composite Component (component having children)
* implements methods to manipulate children
* implements all Component methods, generally by delegating them to its children

import java.util.List;
import java.util.ArrayList;

/** "Component" */
interface Graphic {

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

}

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

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

    //Prints the graphic.
    public void print() {
       for (Graphic graphic : mChildGraphics) {
            graphic.print();
       }
    }

    //Adds the graphic to the composition.
    public void add(Graphic graphic) {
       mChildGraphics.add(graphic);
    }

    //Removes the graphic from the composition.
    public void remove(Graphic graphic) {
       mChildGraphics.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();
    }
}

Comments are closed.

Home | info@eecho.info | Voorwaarden | Blog