<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ActionScript 3.0 Design Patterns &#187; Composite</title>
	<atom:link href="http://www.as3dp.com/category/design-patterns/composite-pattern/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.as3dp.com</link>
	<description>OOP Techniques for Flash and Flex Developers</description>
	<lastBuildDate>Sun, 29 Jan 2012 17:00:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Composite Pattern: Extending the Book to include Composite Pages (Part II)</title>
		<link>http://www.as3dp.com/2007/08/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/</link>
		<comments>http://www.as3dp.com/2007/08/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/#comments</comments>
		<pubDate>Sun, 05 Aug 2007 14:20:27 +0000</pubDate>
		<dc:creator>Chandima Cumaranatunge</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Composite]]></category>
		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2007/08/06/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/</guid>
		<description><![CDATA[This is part two of a two part series on developing the structure of a book using the composite pattern. In part 1, we treated pages as primitive component objects. However, pages can be further decomposed to contain text blocks and images. This example illustrates the utility of the composite pattern by demonstrating how easy [...]]]></description>
			<content:encoded><![CDATA[<p>This is part two of a two part series on developing the structure of a book using the composite pattern. In <a href="http://www.as3dp.com/2007/05/26/composite-pattern-book-part-1/">part 1</a>, we treated pages as primitive component objects. However, pages can be further decomposed to contain text blocks and images. This example illustrates the utility of the composite pattern by demonstrating how easy it is to extend an application that implements the composite pattern by adding new composite and component classes that extend existing ones.<br />
<span id="more-31"></span></p>
<p>I must reiterate that this is a conceptual example. To keep things simple, the images in our composite page will be drawn as <a href="http://en.wikipedia.org/wiki/ASCII_art" target="_blank">ASCII art</a>. We will extend the previous book application (from <a href="http://www.as3dp.com/2007/05/26/composite-pattern-book-part-1/">part 1</a>) by adding three new classes called: <code>CompositePage</code>, <code>TextBlock</code> and <code>Figure</code>.<br />
The recommended way to add multiple composite classes is to extend a generic composite class such as <code> BookComposite</code>. Composite classes implement the housekeeping methods required of composite nodes such as <code>add</code> and <code>getChild</code>. Therefore, it makes sense to inherit the implementations for these methods and override the operations, such as <code>print</code>, that require custom functionality. The <code>CompositePage.as</code> file contains the <code>CompositePage</code> class that extends the <code>BookComposite</code> class defined previously.<br />
<strong>CompositePage.as</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p31code6'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p316"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p31code6"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CompositePage <span style="color: #0066CC;">extends</span> BookComposite <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> CompositePage<span style="color: #66cc66;">&#40;</span>sName:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">super</span><span style="color: #66cc66;">&#40;</span>sName<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">sName</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">super</span>.<span style="color: #0066CC;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--------------------&quot;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Note that the <code>CompositePage</code> class overrides the <code>print()</code> method and prints a dividing line to separate one page from the next. This is the only unique feature in this <code>CompositePage</code> composite class when compared to the <code>BookComposite</code> class. All the functionality required by a composite object such as adding, removing and accessing children are inherited.<br />
Both the <code>TextBlock</code> and <code>Figure</code> classes are primitive component objects that extend the <code>BookComponent</code> abstract class.</p>
<p><strong>TextBlock.as</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p31code7'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p317"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p31code7"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TextBlock <span style="color: #0066CC;">extends</span> BookComponent <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sText:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> TextBlock<span style="color: #66cc66;">&#40;</span>sText:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">sText</span> = sText;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">sText</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The <code>TextBlock</code> class extends the <code>BookComponent</code> abstract class. Text blocks are primitive objects that can be added to a composite page.</p>
<p><strong>Figure.as</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p31code8'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p318"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code" id="p31code8"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Figure <span style="color: #0066CC;">extends</span> BookComponent <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sName:<span style="color: #0066CC;">String</span>;
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> textFigure:<span style="color: #0066CC;">String</span>;
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Figure<span style="color: #66cc66;">&#40;</span>sName:<span style="color: #0066CC;">String</span>, textFigure:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">sName</span> = sName;
			<span style="color: #0066CC;">this</span>.<span style="color: #006600;">textFigure</span> = textFigure;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">textFigure</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">sName</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Similarly, the <code>Figure</code> class extends the <code>BookComponent</code> abstract class.  It declares a property called <code>sName</code> that holds the name of the figure. Another property called <code>textFigure</code> holds the figure. For simplicity, the figures in the book will be drawn with text as <a href="http://en.wikipedia.org/wiki/ASCII_art" target="_blank">ASCII art</a>. As before, figures are primitive objects that can be added to a composite page.</p>
<p><strong>Creating a book with composite pages</strong><br />
Lets create a new book using the new composite page class. The following statements should be run from the <em>client</em>. The client can be the document class in a Flash document (.fla) that builds a composite structure. Note that, in addition to the three new classes defined in this article, the following statements also use classes defined in <a href="http://www.as3dp.com/2007/05/26/composite-pattern-book-part-1/">part 1</a>.</p>
<p><strong>Main.as</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p31code9'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p319"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
</pre></td><td class="code" id="p31code9"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">/**
	*	Main Class
	*	@ purpose:		Document class for movie
	*/</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> Sprite <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// create book</span>
			<span style="color: #000000; font-weight: bold;">var</span> book:BookComposite = <span style="color: #000000; font-weight: bold;">new</span> BookComposite<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;book&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// create table of contents page</span>
			book.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> CompositePage<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Table of contents:&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add TOC</span>
			<span style="color: #000000; font-weight: bold;">var</span> t:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;Chapter 1 ... page 1<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>;
			t += <span style="color: #ff0000;">&quot;Chapter 2 ... page 4&quot;</span>;
			book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> TextBlock<span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// create a chapter</span>
			book.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Chapter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;chapter 1&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Chapter 1: Farm Animals&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// create a new composite page</span>
			<span style="color: #000000; font-weight: bold;">var</span> p2:CompositePage = <span style="color: #000000; font-weight: bold;">new</span> CompositePage<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;page 2&quot;</span><span style="color: #66cc66;">&#41;</span>;
			t = <span style="color: #ff0000;">&quot;         (__)<span style="color: #000099; font-weight: bold;">\r</span>         (oo)<span style="color: #000099; font-weight: bold;">\r</span>  /-------<span style="color: #000099; font-weight: bold;">\\</span>/<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>;
			t += <span style="color: #ff0000;">&quot; / |     ||<span style="color: #000099; font-weight: bold;">\r</span>*  ||----||<span style="color: #000099; font-weight: bold;">\r</span>   ~~    ~~&quot;</span>;
			p2.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Figure<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fig 1: cow&quot;</span>, t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			t = <span style="color: #ff0000;">&quot;Cows have one<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>;
			t += <span style="color: #ff0000;">&quot;stomach, with four<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>;
			t += <span style="color: #ff0000;">&quot;compartments.&quot;</span>;
			p2.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> TextBlock<span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>p2<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add to chapter 1</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// create another composite page</span>
			<span style="color: #000000; font-weight: bold;">var</span> p3:CompositePage = <span style="color: #000000; font-weight: bold;">new</span> CompositePage<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;page 3&quot;</span><span style="color: #66cc66;">&#41;</span>;
			p3.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Figure<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;fig 2: duck&quot;</span>, <span style="color: #ff0000;">&quot;&gt;(o)__<span style="color: #000099; font-weight: bold;">\r</span> (_~_/<span style="color: #000099; font-weight: bold;">\r</span> ~~~~~~~&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			t = <span style="color: #ff0000;">&quot;Ducks are aquatic<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>;
			t += <span style="color: #ff0000;">&quot;birds, smaller than<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>;
			t += <span style="color: #ff0000;">&quot;their relatives the<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span>;
			t += <span style="color: #ff0000;">&quot;swans.&quot;</span>;
			p3.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> TextBlock<span style="color: #66cc66;">&#40;</span>t<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>p3<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add to chapter 1</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// create another chapter</span>
			book.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Chapter<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;chapter 2&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Chapter 2: Work in Progress&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add a child Page</span>
			book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;page 5&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add a child Page</span>
			book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;page 6&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add a child Page</span>
			book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;page 7&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add a child Page</span>
&nbsp;
			<span style="color: #808080; font-style: italic;">// add an appendix</span>
			book.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;appendix&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// add an index</span>
			book.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;index&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;print book&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;==========&quot;</span><span style="color: #66cc66;">&#41;</span>;
			book.<span style="color: #0066CC;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The client first adds a new composite page for the table of contents (line 17). It then adds a new text block to the table of contents page (line 20). A new chapter is created next (line 23) with a chapter title page added to it (line 24). Two composite pages are created (lines 27 and 38) with both text blocks and figure are added to them. Both composite pages are then added to the chapter (lines 35 and 45). Calling the <code>print()</code> operation from the root node results in the following output.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p31code10'); return false;">View Code</a> TEXT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3110"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code" id="p31code10"><pre class="text" style="font-family:monospace;">print book
==========
Table of contents:
Chapter 1 ... page 1
Chapter 2 ... page 4
--------------------
Chapter 1: Farm Animals
--------------------
page 2
         (__)
         (oo)
  /-------\/
 / |     ||
*  ||----||
   ~~    ~~
fig 1: cow
Cows have one
stomach, with four
compartments.
--------------------
page 3
&gt;(o)__
 (_~_/
 ~~~~~~~
fig 2: duck
Ducks are aquatic
birds, smaller than
their relatives the
swans.
--------------------
Chapter 2: Work in Progress
--------------------
page 5
--------------------
page 6
--------------------
page 7
--------------------
appendix
--------------------
index
--------------------</pre></td></tr></table></div>

<p>Our simple book contains ASCII art and prints using a monospaced front to the <em>Output Panel</em>  (like the old dot-matrix printers that take roll paper). However, we can modify the <code>Figure</code> class to display actual images, and create new text block components with different formatting for headers and lists etc. We can also initiate a real print job using the <code>PrintJob</code> class to make this a more functional book.</p>
<p>Changing requirements in software design is a certainty (coupled with death and taxes). OOP and design patterns allow anticipated and unanticipated changes to be managed effectively. As we have seen in this example, extending complex hierarchical structures is easy when they implemented using the composite pattern. The ease is due very much to the common interface shared by both primitive and composite nodes. Adding, deleting and otherwise manipulating the structure is simple &#8211; no matter whether you are dealing with component or composite objects as both <em>types</em> can be treated the same way. Only, unique functionality in the new objects need to be implemented while the others can be inherited.</p>
<p>Download the source: <a href="http://www.as3dp.com/wp-content/uploads/2007/08/book-part-ii.zip">Book-part_II.zip</a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2007%2F08%2Fcomposite-pattern-extending-the-book-to-add-composite-pages-part-ii%2F&amp;title=Composite%20Pattern%3A%20Extending%20the%20Book%20to%20include%20Composite%20Pages%20%28Part%20II%29" id="wpa2a_2"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2007/08/composite-pattern-extending-the-book-to-add-composite-pages-part-ii/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Composite Pattern: Book (Part 1)</title>
		<link>http://www.as3dp.com/2007/05/composite-pattern-book-part-1/</link>
		<comments>http://www.as3dp.com/2007/05/composite-pattern-book-part-1/#comments</comments>
		<pubDate>Sat, 26 May 2007 10:37:07 +0000</pubDate>
		<dc:creator>Chandima Cumaranatunge</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Composite]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://www.as3dp.com/2007/05/26/composite-pattern-book-part-1/</guid>
		<description><![CDATA[This is part 1 of a two part composite pattern example implemented in ActionScript 3. The composite pattern comes in handy to develop complex structures consisting of simpler parts or components. The components can be primitive objects or they can be containers that hold other components. An example will help explain this better. Think of [...]]]></description>
			<content:encoded><![CDATA[<p>This is part 1 of a two part composite pattern example implemented in ActionScript 3. The composite pattern comes in handy to develop complex structures consisting of simpler parts or components. The components can be primitive objects or they can be containers that hold other components. An example will help explain this better. Think of a house as a composite object. A house consists of a roof and several rooms (living spaces). Each room can contain windows, chairs etc. The house and rooms are composite objects as they contain other components. However, we can consider the roof, windows, beds and chairs as primitive objects. The composite pattern makes building, accessing, and manipulating composite structures simple by enabling the client to treat both composite objects (that contain other objects) and components (primitive objects) the same way through a common interface.</p>
<p>The following example develops a book in Flash. A book is a composite object as it contains several pages. This is by no means a functional book, but a conceptual example to illustrate the utility of the composite pattern. One of the big advantages of design patterns is extensibility: allowing the application to expand and add new features without breaking existing functionality. We will extend this example in part 2 by adding composite pages that can contain text and images.<br />
<span id="more-19"></span></p>
<p><img src="http://www.as3dp.com/wp-content/uploads/2007/05/bookuml-2.jpg" height="251" width="527" border="0" hspace="4" vspace="4" alt="Class Diagram" title="Class Diagram" /><br />
The class diagram of the Book example shows the <code>BookComponent</code> class that should behave as an abstract class. ActionScript 3 does not have abstract classes. See a previous post on <a href="http://www.as3dp.com/2007/05/06/runtime-checks-for-abstract-classes-and-methods-in-actionscript-30/">Runtime Checks for Abstract Classes and Methods in ActionScript 3.</a> I&#8217;m not implementing the runtime checks in this example, but noting that some concrete classes need to be treated as abstract. <code>BookComponent</code> is the abstract interface that defines the default behavior for both primitive and composite objects. It defines the default implementations for the <code>add</code>, <code>getChild</code>, and <code>remove</code> methods used to build and manipulate composite structures. The print method is an operation that is unique to the book.<br />
A book is a good example of a composite pattern. It contains chapters that can be considered composite objects that hold collections of pages. Pages can be considered indivisible primitives. We will first implement a very basic book as a composite tree and then extend it to demonstrate the power of the composite pattern.<br />
The <code>BookComponent.as</code> file contains the <code>BookComponent</code> abstract class that defines the interface for both primitive objects such as pages and composite objects such as chapters. The <code>Page.as</code> file contains the <code>Page</code> class, and the <code>BookComposite.as</code> file contains the <code>BookComposite</code> class. Both <code>Page</code> and <code>BookComposite</code> classes extend the <code>BookComponent</code> class and provide necessary implementations. The <code>Main.as</code> file is contains the client class <code>Main</code> (also the document class for the Flash document).<br />
<strong>BookComponent.as</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code16'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1916"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code" id="p19code16"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">errors</span>.<span style="color: #006600;">IllegalOperationError</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// ABSTRACT Class (should be subclassed and not instantiated)</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BookComponent <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>c:BookComponent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;add operation not supported&quot;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> remove<span style="color: #66cc66;">&#40;</span>c:BookComponent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;remove operation not supported&quot;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getChild<span style="color: #66cc66;">&#40;</span>n:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:BookComponent <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> IllegalOperationError<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;getChild operation not supported&quot;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// ABSTRACT Method (must be overridden in a subclass)</span>
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>In addition to defining the default implementations for the <code>add</code>, <code>remove</code>, and <code>getChild</code> methods, the <code>BookComponent</code> class defines the abstract method <code>print</code> that can print the book component.<br />
<strong>BookComposite.as</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code17'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1917"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code" id="p19code17"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BookComposite <span style="color: #0066CC;">extends</span> BookComponent <span style="color: #66cc66;">&#123;</span>
&nbsp;
        internal <span style="color: #000000; font-weight: bold;">var</span> sName:<span style="color: #0066CC;">String</span>;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> aChildren:<span style="color: #0066CC;">Array</span>;
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> BookComposite<span style="color: #66cc66;">&#40;</span>sName:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">this</span>.<span style="color: #006600;">sName</span> = sName;
            <span style="color: #0066CC;">this</span>.<span style="color: #006600;">aChildren</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span>c:BookComponent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            aChildren.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getChild<span style="color: #66cc66;">&#40;</span>n:<span style="color: #0066CC;">int</span><span style="color: #66cc66;">&#41;</span>:BookComponent <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>n <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">&#40;</span>n <span style="color: #66cc66;">&lt;</span>= aChildren.<span style="color: #0066CC;">length</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #b1b100;">return</span> aChildren<span style="color: #66cc66;">&#91;</span>n-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>;
            <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> c:BookComponent <span style="color: #b1b100;">in</span> aChildren<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                c.<span style="color: #0066CC;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The <code>BookComposite</code> class extends the <code>BookComponent</code> class. Whenever a <code>BookComponent</code> is added to a composite node via the <code>add</code> method it is pushed into the <code>aChildren</code> array. The <code> BookComposite </code> class implements the abstract method <code>print</code> that prints all child nodes by recursively traversing the whole composite tree represented by the book. The <code>remove</code> method has not been implemented, in order to keep things simple.<br />
<strong>Page.as</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code18'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1918"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p19code18"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Page <span style="color: #0066CC;">extends</span> BookComponent <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> sName:<span style="color: #0066CC;">String</span>;
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Page<span style="color: #66cc66;">&#40;</span>sName:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">this</span>.<span style="color: #006600;">sName</span> = sName;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        override <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>.<span style="color: #006600;">sName</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The <code>Page</code> class represents a page of the book and implements the <code>print</code> method by simply tracing the name of the page passed to it in the constructor.<br />
<strong>Main.as</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code19'); return false;">View Code</a> ACTIONSCRIPT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1919"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code" id="p19code19"><pre class="actionscript" style="font-family:monospace;">package <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #0066CC;">import</span> flash.<span style="color: #006600;">display</span>.<span style="color: #006600;">Sprite</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">/**
    *   Main Class
    *   @ purpose:      Document class for movie
    */</span>
    <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Main <span style="color: #0066CC;">extends</span> Sprite <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Main<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// create book</span>
            <span style="color: #000000; font-weight: bold;">var</span> book:BookComposite = <span style="color: #000000; font-weight: bold;">new</span> BookComposite<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;book&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">// add the table of contents</span>
            book.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;table of contents&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">// create a chapter</span>
            book.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> BookComposite<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;chapter 1&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add chapter to book</span>
            book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Chapter 1 title page&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add a Page</span>
            book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;page 2&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add a Page</span>
            book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;page 3&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add a Page</span>
&nbsp;
            <span style="color: #808080; font-style: italic;">// create another chapter</span>
            book.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> BookComposite<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;chapter 2&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add chapter to book</span>
            book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Chapter 2 title page&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add a Page</span>
            book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;page 5&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add a Page</span>
            book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;page 6&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add a Page</span>
            book.<span style="color: #006600;">getChild</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;page 7&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// add a Page</span>
&nbsp;
            <span style="color: #808080; font-style: italic;">// add an appendix</span>
            book.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;appendix&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #808080; font-style: italic;">// add an index</span>
            book.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Page<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;index&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;print book&quot;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;==========&quot;</span><span style="color: #66cc66;">&#41;</span>;
            book.<span style="color: #0066CC;">print</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>The client class <code>Main</code> builds the book by first creating a root composite node that will contain the entire book. It then adds a table of contents page, two chapters that contain several pages, an appendix and index page. Finally it prints the whole book by calling the <code>print</code> operation on the root node.<br />
Note how component and composite nodes are added to the book through a single interface using the <code>add</code> method. The client does not need to worry about the type of node as the composite pattern treats both composite and component nodes the same way. The following output is generated by the <code>print</code> operation.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p19code20'); return false;">View Code</a> PLAINTEXT</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1920"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p19code20"><pre class="plaintext" style="font-family:monospace;">print book
==========
table of contents
Chapter 1 title page
page 2
page 3
Chapter 2 title page
page 5
page 6
page 7
appendix
index</pre></td></tr></table></div>

<p>Download the source: <a href="http://www.as3dp.com/wp-content/uploads/2007/12/book_p1.zip">book_p1.zip</a>. Watch for part 2 of this article, where the book will be extended to incorporate composite pages that can contain both text and images.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.as3dp.com%2F2007%2F05%2Fcomposite-pattern-book-part-1%2F&amp;title=Composite%20Pattern%3A%20Book%20%28Part%201%29" id="wpa2a_4"><img src="http://www.as3dp.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.as3dp.com/2007/05/composite-pattern-book-part-1/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

