Composite Pattern Eugene Jon Intent Utilize a tree

  • Slides: 14
Download presentation
Composite Pattern Eugene Jon

Composite Pattern Eugene Jon

Intent Utilize a tree structure to represent partwhole hierarchies. Why? Because the composite pattern

Intent Utilize a tree structure to represent partwhole hierarchies. Why? Because the composite pattern lets clients treat individual objects and compositions of objects identically

Motivation Graphics applications like drawing editors allow users to create complex diagrams out of

Motivation Graphics applications like drawing editors allow users to create complex diagrams out of simple components. In other words, the user can use multiple single components to create a large component that is composed of the smaller components. The user can then use this larger derived diagram as a component of an even more complicated diagram. For example, a user can create a stick figure object with a circle objects and line objects.

Motivation (continued) Such a graphics program can have classes defined for graphical primitives like

Motivation (continued) Such a graphics program can have classes defined for graphical primitives like Lines and Text, and other classes that act as containers for these primitives. Problem: The user must differentiate between primitive and container objects. This issue makes the application more complex and not so user-friendly.

Motivation (continued) The Composition pattern addresses this issue by introducing an abstract class that

Motivation (continued) The Composition pattern addresses this issue by introducing an abstract class that represents both primitives and their containers (compositions of primitives). By doing so, the user can treat primitives and containers equally without making a distinction between the two.

In this example, Graphic is an abstract class that represents both primitives and containers.

In this example, Graphic is an abstract class that represents both primitives and containers.

Structure

Structure

Typical Composite object structure a. Composite is an object of recursively composed Component objects

Typical Composite object structure a. Composite is an object of recursively composed Component objects

Applicability We use the Composite pattern when we want to show part-whole hierarchies the

Applicability We use the Composite pattern when we want to show part-whole hierarchies the client wants to treat the composite objects and individual objects uniformly

How It Works Component declares the interface for objects in the composition, and declares

How It Works Component declares the interface for objects in the composition, and declares an interface for accessing and managing child components implements default behavior for the interface common to all classes Leaf defines behavior for primitive objects in the composition

 Composite defines behavior for components having children stores child components implements child-related operations

Composite defines behavior for components having children stores child components implements child-related operations in the Component interface Client manipulates objects in the composition through the Component interface

Java example A Graphic class from Wikipedia: import java. util. List; import java. util.

Java example A Graphic class from Wikipedia: import java. util. List; import java. util. Array. List; interface Graphic { //Prints the graphic. public void print(); } class Composite. Graphic implements Graphic { //Collection of child graphics. private List<Graphic> m. Child. Graphics = new Array. List<Graphic>(); //Prints the graphic. public void print() { for (Graphic graphic : m. Child. Graphics) { graphic. print(); } }

//Adds the graphic to the composition. public void add(Graphic graphic) { m. Child. Graphics.

//Adds the graphic to the composition. public void add(Graphic graphic) { m. Child. Graphics. add(graphic); } //Removes the graphic from the composition. public void remove(Graphic graphic) { m. Child. Graphics. remove(graphic); } } class Ellipse implements Graphic { //Prints the graphic. public void print() { System. out. println("Ellipse"); } }

public class Program { public static void main(String[] args) { //Initialize four ellipses Ellipse

public class Program { public static void main(String[] args) { //Initialize four ellipses Ellipse ellipse 1 = new Ellipse(); Ellipse ellipse 2 = new Ellipse(); Ellipse ellipse 3 = new Ellipse(); Ellipse ellipse 4 = new Ellipse(); //Initialize three composite graphics Composite. Graphic graphic = new Composite. Graphic(); Composite. Graphic graphic 1 = new Composite. Graphic(); Composite. Graphic graphic 2 = new Composite. Graphic(); //Composes the graphics graphic 1. add(ellipse 1); graphic 1. add(ellipse 2); graphic 1. add(ellipse 3); graphic 2. add(ellipse 4); graphic. add(graphic 1); graphic. add(graphic 2); } } //Prints the complete graphic (four times the string "Ellipse"). graphic. print();