﻿<?xml-stylesheet type="text/xsl" href="../../templates/doc.xsl"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
<html 
	xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:doc="http://www.lepus.org.uk/doc" 
	xmlns:classz="http://www.lepus.org.uk/classz" 
	xmlns:fopl="http://www.lepus.org.uk/fopl" 
	xml:lang="EN-GB" 
	xmlns:v="urn:schemas-microsoft-com:vml" 
	xmlns:o="urn:schemas-microsoft-com:office:office">

<head>
	<link rel="stylesheet" type="text/css" href="../../templates/doc.css" />
	<title>Composite pattern: The LePUS3 &amp; Class-Z companion to the GoF</title>
	<meta http-equiv="Content-Style-Type" content="text/css" />
	<meta name="Author" content="Amnon H Eden" />
</head>


<body>


<h5><a href="./index.xml">The 'Gang of Four' Companion:</a></h5>

<p class="pagetitle">Composite Design Pattern</p>

<h5><a href="./index.xml">Formal specification of design patterns in LePUS3 and Class-Z</a></h5>

<p><a href="companion.pdf" style="border:0px">
	<img alt="Print this document" src="../../site/print.gif" /></a></p>

<div class="abstract">

	<p>This page is part of the <a href="./index.xml">The 'Gang of Four' Companion</a> 
	dedicated to the formal specification in LePUS3 and Class-Z of patterns from the 'Gang of Four' catalogue [<a href="http://www.lepus.org.uk/ref/index.xml#bib">Gamma 
	et al 1995</a>].</p>

</div> <!-- Abstract-->

<doc:toc />

<h3>Links</h3>

<div class="abstract">

<ul>
		<li><a href="../legend/legend.xml">Legend: Key to LePUS3 and Class-Z Symbols</a></li>
		<li><a href="ref/refman/refman.xml">Reference Manual for LePUS3 &amp; Class-Z </a></li>
		<li><a href="faq.xml">Modelling Design Patterns: Frequently-Asked Questions</a></li>
	</ul>

	<p>Download:</p>
	
	<ul>
		<li><a href="../../spec/gof/lepus3/gof.vsd">The 'gang of four' patterns in LePUS3 (Visio 2003 format)</a></li>
		<li><a href="../../spec/gof/uml/gof.vsd">The 'gang of four' patterns in UML (Visio 2003 format)</a></li>
	</ul>
	
</div> <!-- Links -->


<h1>The Composite design motif</h1>

<p>The informal description: Excerpts from [<a href="../index.xml#bib">Gamma et al. 1995</a>] (adapted for 
	this purpose):</p>

<p><strong>Intent</strong>: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.</p>
	
<p><strong>Structure</strong>: Original OMT diagram converted to UML (<a href="faq.xml#Why_did_you_convert_the_original_diagrams_to_UML_and_how?">Why 
	and How?</a>):</p>

<img alt="Composite in UML" src="../../spec/gof/uml/Composite.png" /> <br />

<p><strong>Participants</strong>: </p>
<ul>	
	<li><b>Component</b> (Graphic):
		<ul>
			<li>Declares the interface for objects in the composition.</li>
			<li>Implements default behavior for the interface common to all classes, as appropriate.</li>
			<li>Declares an interface for accessing and managing its child components.</li>
		</ul>
	</li>
	<li><b>Leaf</b> (Rectangle, Line, Text, etc.):
		<ul>
			<li>Represents leaf objects in the composition. A leaf has no children.</li>
			<li>Defines behavior for primitive objects in the composition.</li>
		</ul>
	</li>
	<li><b>Composite</b> (Picture):
		<ul>
			<li>Defines behavior for components having children.</li>
			<li>Stores child components.</li>
			<li>Implements child-related operations in the Component interface.</li>
		</ul>
	</li>
	<li><b>Client</b> (Client): manipulates objects in the composition through the Component interface.</li>
</ul>

<p><strong>Collaborations</strong>: Clients use the Component class interface to interact with objects in the composite structure. If the recipient is a Leaf, then the request is handled directly. If the recipient is a Composite, then it usually forwards requests to its child components, possibly performing additional operations before and/or after forwarding.</p>

<p>
	<strong><a name="sample-imp">Sample implementation</a></strong>: The following UML Collaboration Diagram illustrates how the 
	objects in the AWT implementation of the Composite pattern are organized into a tree structure, 
	and how the message numListening can is passed down objects in this tree:
</p>

<img alt="Composite implementation in AWT, UML Collaboration Diagram" src="../../spec/gof/uml/Composite-awt-collab.png" /> <br />

<h1>Formal specification</h1>

<p>See also:</p>
<ul>
		<li><a href="../legend/legend.xml">Legend: Key to LePUS3 and Class-Z Symbols</a></li>
		<li><a href="ref/refman/refman.xml">Reference Manual for LePUS3 &amp; Class-Z </a></li>
		<li><a href="../../spec/gof/classz/Composite.xml">Composite in XML</a></li>
		<li><a href="faq.xml">Modelling Design Patterns: Frequently-Asked Questions</a></li>
	</ul>

<table class="chartandschema">
<tr>
	<td>
	<img class="chart" id="Composite" alt="Composite in LePUS3" src="../../spec/gof/lepus3/Composite.png"/></td>
</tr>
<tr>
	<th>Composite pattern in LePUS3 (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
<tr>
	<td><doc:include value="../../spec/gof/classz/Composite.xml" /></td>
</tr>
<tr>
	<th>Composite pattern in Class-Z (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
</table>


<h1>Sample Implementations</h1>

<h2>Informal description: Composite Graphics</h2>
<p>From [Gamma et. al 1995]:</p>

<blockquote>Graphics applications like drawing editors and schematic capture systems let users build complex diagrams out of simple components. The user can group components to form larger components, which in turn can be grouped to form still larger components. A simple implementation could define classes for graphical primitives such as Text and Lines plus other classes that act as containers for these primitives.</blockquote>

<blockquote>But there's a problem with this approach: Code that uses these classes must treat primitive and container objects differently, even if most of the time the user treats them identically. Having to distinguish these objects makes the application more complex. The Composite pattern describes how to use recursive composition so that clients don't have to make this distinction.</blockquote>

<blockquote>The key to the Composite pattern is an abstract class that 
	represents both primitives and their containers. For the graphics system, 
	this class is Graphic. Graphic declares operations like Draw that are 
	specific to graphical objects.</blockquote>

<blockquote>The subclasses Line, Rectangle, and Text (see preceding class diagram) define primitive graphical objects. These classes implement Draw to draw lines, rectangles, and text, respectively. Since primitive graphics have no child graphics, none of these subclasses implements child-related operations.</blockquote>

<blockquote>The Picture class defines an aggregate of Graphic objects. Picture implements Draw to call Draw on its children, and it implements child-related operations accordingly. Because the Picture interface conforms to the Graphic interface, Picture objects can compose other Pictures recursively.</blockquote>

<p>This design pattern, as informally described above, can be seen implemented 
within the Java Foundation Classes. Specifically within the Java Abstract Window 
Toolkit (AWT). How this is done is presented below, and unless otherwise stated 
all classes reside within the package <code>java.awt</code>.</p>
<h2>Formal specification: Composite Graphics in <code>java.awt</code></h2>

<p>Specified in LePUS3 and Class-Z (see <a href="../legend/legend.xml">legend</a>)</p>

<table class="chartandschema">
<tr>
	<td><img class="chart" alt="Composite implementation" src="../../spec/closed/CompositeImp/awt1dim.gif" longdesc="Composite implementation modelled in LePUS3 (1-dimensional)" style="width: 333px; height: 299px" /></td>
</tr>
<tr>
	<th>Composite implementation modelled in LePUS3 <br />
			using 0 and 1-dimensional class (signature) constants (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
<tr>
	<td><img class="chart" alt="Composite implementotion" longdesc="Composite implementation modelled in LePUS3 (0 dimensional)" src="../../spec/closed/CompositeImp/awt1dimSig.gif" style="width: 553px; height: 364px" /></td>
</tr>
<tr>
	<th>Composite implementation modelled in LePUS3 <br />
			using 0-dimensional class and 1-dimensional signature constants (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
<tr>
	<td><img class="chart" alt="Composite implementotion" longdesc="Composite implementation modelled in LePUS3 (0 dimensional)" src="../../spec/closed/CompositeImp/awt0dim.gif" style="width: 705px; height: 653px" /></td>
</tr>
<tr>
	<th>Composite implementation modelled in LePUS3 <br />
			using 0-dimensional class and signature constants (<a href="../legend/legend.xml">legend</a>)<br />
	[1-dimensional signature constant CompositeOps left unexpanded for 
	simplicity]</th>
</tr>
</table>


</body>
</html>

