<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="Module.2004-02-03.3737">
	<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Ballworld, composition-based</name>
	<metadata xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
  <md:version xmlns:bib="http://bibtexml.sf.net/">1.5</md:version>
  <md:created xmlns:bib="http://bibtexml.sf.net/">2004/02/03 10:37:37 US/Central</md:created>
  <md:revised xmlns:bib="http://bibtexml.sf.net/">2008/06/23 16:28:36.474 GMT-5</md:revised>
  <md:authorlist xmlns:bib="http://bibtexml.sf.net/">
      <md:author xmlns:bib="http://bibtexml.sf.net/" id="swong">
      <md:firstname xmlns:bib="http://bibtexml.sf.net/">Stephen</md:firstname>
      
      <md:surname xmlns:bib="http://bibtexml.sf.net/">Wong</md:surname>
      <md:email xmlns:bib="http://bibtexml.sf.net/">swong@rice.edu</md:email>
    </md:author>
      <md:author xmlns:bib="http://bibtexml.sf.net/" id="dxnguyen">
      <md:firstname xmlns:bib="http://bibtexml.sf.net/">Dung</md:firstname>
      <md:othername xmlns:bib="http://bibtexml.sf.net/">X.</md:othername>
      <md:surname xmlns:bib="http://bibtexml.sf.net/">Nguyen</md:surname>
      <md:email xmlns:bib="http://bibtexml.sf.net/">dxnguyen@rice.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist xmlns:bib="http://bibtexml.sf.net/">
    <md:maintainer xmlns:bib="http://bibtexml.sf.net/" id="swong">
      <md:firstname xmlns:bib="http://bibtexml.sf.net/">Stephen</md:firstname>
      
      <md:surname xmlns:bib="http://bibtexml.sf.net/">Wong</md:surname>
      <md:email xmlns:bib="http://bibtexml.sf.net/">swong@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer xmlns:bib="http://bibtexml.sf.net/" id="dxnguyen">
      <md:firstname xmlns:bib="http://bibtexml.sf.net/">Dung</md:firstname>
      <md:othername xmlns:bib="http://bibtexml.sf.net/">X.</md:othername>
      <md:surname xmlns:bib="http://bibtexml.sf.net/">Nguyen</md:surname>
      <md:email xmlns:bib="http://bibtexml.sf.net/">dxnguyen@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist xmlns:bib="http://bibtexml.sf.net/">
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">abstract</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">Ballworld</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">classes</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">composition</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">design</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">inheritance</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">object</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">OOD</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">OOP</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">oriented</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">polymorphism</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">programming</md:keyword>
  </md:keywordlist>

  <md:abstract xmlns:bib="http://bibtexml.sf.net/">The module uses the Ballworld program to demonstrate key ideas in abstract classes, polymorphism, inheritance and other coding techniques.  In particular, this module will contrast a composition-based architecture with an inheritance-based architecture.</md:abstract>
</metadata>
	<content xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
		<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="p10">In this module we will explore what is gained by modifying the <cnxn xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" document="m11806">inheritance-based Ballworld system</cnxn> into a composition-based system.  </para>
		<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="p20">In the inheritance-based Ballworld system, we were able to generate quite a bit of flexibility and extensibility.  For instance, we could develop new kinds of balls and add them into the system without recompiling the rest of the code.  This was accomplished by having the invariant ball creation and management code deal only with the abstract ball while the variant behaviors were encapsulated into the concrete subclasses.</para>
		<section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10">
			<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">The Problem with Inheritance</name>
			<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10p10">Inheritance seems to work quite well, but suppose we want to do more than just put different kinds of balls on the screen?   What if we wanted to be able to change how a ball behaves, <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">after </emphasis>it has been created?   What if we want create balls that do a multiple of different behaviors, such as change color and radius?  While working solutions using an inheritance-based system do exist,  they are cumbersome, inefficient and most importantly, inconsistent with any sort of clear abstract model of what balls should be like. </para>
			<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10p20">The problem lies in the very nature of inheritance.  When we attempted to separate the variant from the invariant behaviors, we overlooked a crucial aspect of inheritance.   In our model, the superclass represented the invariant behaviors of a ball while the subclasses represented the variant behaviors.   The separation seemed clear enough in the UML diagram, except that when one has an actual object <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">instance</emphasis>, <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">both </emphasis>the superclass and subclass behaviors are bound into a <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">single </emphasis>entity.   A ball <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">object </emphasis>cannot change its variant  <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">updateState </code>behavior because it is inextricably bound with to the invariant behaviors.   A ball object cannot be composed of multiple <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">updateState </code>behaviors because that code cannot be isolated from the rest of the ball's code.   If you want a curving behavior, you have to get it packaged in a whole ball object--you can't get just the behavior.</para>
			<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s10p30">A clear sympton of this problem is the common code to call the superclass constructor found in all the subclasses' constructors.   This tells us that the superclass is really right there in the subclass with everything else.   The fact that the code is repeated from class to class says that it is invariant code in the middle of what we want to be variant code.  <quote xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">The inheritance-based model of Ballworld does not separate the variant and the invariant at the proper place.  There is invariant code mixed together with the variant code. </quote>That's why they can't be separated and the invariant behaviors are dragged along with the variant behaviors.   This is what makes dynamically changing behaviors and multiply composed behaviors so difficult in this system.  </para>
		</section>
		<section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s20">
			<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Pizzas and Shapes</name>
			<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s20p10">To understand what we can do to remedy the problems with our inheritance-based model, let's digress for a bit and consider a simple model of pizzas.    Here, we have a pizza which has a price and <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">has a</emphasis> shape.  A shape, be it a circle, square, rectangle of triangle, is capable of determining its own area.  A pizza, when requested to calculate its price per square inch, simply takes its price and divides it by the area of its shape.   To obtain that area, the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Pizza </code><term xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">delegates </term>to the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">IShape</code>, since it is the shape that knows how to calculate its area, not the pizza. <figure xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s20p10f10">
					<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Pizzas and Shapes</name>
					<media xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="image/png" src="pizza.png"/>
					<caption xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">A pizza <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">has-a</emphasis> shape, which is able to calculate its area.</caption>
				</figure>Delegation is the handing of a calculation off to another object for it process.   Here, the pizza is only interested in the result of the area calculation, not how it is performed. <quote xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block"> To the pizza, the shape represents an abstract algorithm to calculate the area. </quote>  The <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Pizza </code>and the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">IShape </code>classes represent the invariant processes involved with calculating the price per square inch ration, while the concrete <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Circle</code>, <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Square</code>, <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Triangle </code>and <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Rectangle </code>classes represent the variant area calculations for different shapes. What wee see from this example is that <quote xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">objects can be used to represent pure behavior, not just tangible entities. </quote>Interfaces are particularly useful here as  they are expressly designed to represent pure, abstract behavior. </para>
		</section>
		<section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s30">
			<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">From Inheritance to Composition</name>
			<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s30p10">Coming back to Ballworld, we see that the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">updateState </code>method in <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">ABall </code>is an abstract algorithm to update the state of the ball.   So, just as in the pizza example, we can represent this algorithm, <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">and just this algorithm</emphasis>, as an object.   We can say that a ball <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">has an</emphasis> algorithm to update its state.  Another wa of saying this is to say that the ball has a <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">strategy </emphasis>to update its state.  We can represent this by using composition.   Instead of having an abstract method to update the state,  we model a ball as having a reference to an <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">IUpdateStrategy </code>object.   The code for update thus becomes <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">
  	public void update(Observable o, Object g)
  	{
    		_strategy.updateState(this);   // update this ball's state using the strategy
    	 	location.translate (velocity.x, velocity.y);  // move the ball
    		bounce();  // bounce the ball off the wall if necessary
    		paint((Graphics) g); // paint the ball onto the container
 	 }</code>The ball hands a reference to itself, <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">this</code>, to the strategy so that the strategy knows which ball to update.  The variant updating behaviors are now represented by concrete implementations of the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">IUpdateStrategy </code>interface. <figure xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s30f10">
					<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Composition-based Ballworld</name>
					<media xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="image/png" src="uml.png"/>
					<caption xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Ball </code>is now a concrete class and its subclasses have been eliminated in favor of being composed with an abstract strategy.</caption>
				</figure>(Note that the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Randomizer </code>class has been redesigned to eliminate its static methods. One new method has been added as well.)</para>
			<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s30p20">There are a number of very important points to notice about this new formulation:  <list xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s30p20l10">
					<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">The modified <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">ABall </code>class now contains 100% concrete code and thus should not be abstract anymore.   <list xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s30p20l10l10">
							<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/"><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">ABall </code>has been renamed to simply <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Ball</code>.</item>
							<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Accessor methods for the strategy  (<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">getStrategy </code>and <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">setStrategy</code>) have been added.</item>
							<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/"><emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">The <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Ball </code>class is still 100% invariant code.</emphasis></item>
						</list></item>
					<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">The <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">CurveBall</code>, <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">StraightBall</code>, etc. subclasses are no longer needed as their variant behaviors have been moved to the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">IUpdateStrategy </code>subclasses.<list xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s30p20l10l20">
							<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Effectively what has happened is that the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">updateState </code>method has been moved from the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">ABall </code>subclasses and embodied into their own classes. </item>
							<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">The <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">IUpdateStrategy </code>subclasses do not inherit anything from <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Ball</code>, hence they do not contain any invariant code.</item>
							<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/"><emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">The strategies are thus 100% variant code.</emphasis></item>
						</list></item>
					<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">The reference to the ball during the updating process has gone from a peristent communication link (implicitly, <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">this</code>), to a transient communication link (<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">host</code>).</item>
					<item xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">This composition-based model divides the code exactly between the variant and invariant behaviors--this is the key to the power and flexibility it generates.</item>
				</list></para>
			<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s30p30">This new composition-based model of Ballworld is an example of the <link xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" src="http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/StrategyPattern.htm">Strategy Design Pattern</link>.  The strategy design pattern allows us to isolate variant behaviors on a much finer level than simple inheritance models.  <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/"/><!-- Insert module text here --></para>
			<section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s40">
				<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Composing Behaviors</name>
				<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s40p10">So far, all of our redesigning has resulted in a system that behaves exactly as it did when we started.   But what one finds very often in developing systems is that in order to make two steps forward, one must first make one step backwards in order to fundmentally change the direction in which they are going.    So, even though it looks like our system has not progressed because it still does exactly the same thing, we are indeed in a very different position, architecturally.   By freeing the variant behaviors from the invariant ones, we have generated a tremendous amount of flexibility.</para>
				<section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s50">
					<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Balls that change their strategies</name>
					<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s50p10">Let's consider a the notion of a ball that changes its behavior.   Since we have modeled  a ball as <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">having </emphasis>a strategy, we can simply say that in some manner, it is the ball's <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">strategy </emphasis>that changes.   We could say that the ball changes its strategy, but since the ball doesn't know which strategy it has to begin with, it really doesn't know one strategy from another.  One could argue that it therefore can't know when or if it should ever change its strategy.   Therefore, the ball cannot be coded to change its own strategy!  So, whose baliwick is the changing of the strategy?    </para>
					<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s50p20">Since the changing of a strategy is a strategy for updating the ball, it is the strategy that determines the change.   The strategy changes the strategy!  Let's consider the following strategy:<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">
	package ballworld;
	import java.awt.*;

	public class Change1Strategy implements IUpdateStrategy {

  		private int i = 500;  // initial value for i
  
  		public void updateState(Ball context) {
    			if(i==0) context.setStrategy(new CurveStrategy());   // change strategy if i reaches zero
    			else i--;  // not yet zero, so decrement i
  		}
	}
</code>This strategy acts just like a <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">StraightStrategy </code>for 500 updates and then it tells the ball (its <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">context</code>) to switch to a <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">CurveStrategy</code>.  Once the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">CurveStrategy </code>is installed, the ball becomes curving, without the need for any sort of conditionals to decide what it should do.   The context ball fundamentally and permanently <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">becomes </emphasis>curving.  <exercise xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s50e10">
							<problem xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
								<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s50e10p10">What would happen if you had two strategies like the one above, but instead of replacing themselves with <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">CurveStrategy</code>'s , they instead instantiated each other?</para>
							</problem>
							<solution xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
								<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s50e10p20">Try it!</para>
							</solution>
						</exercise></para>
					<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s50p30">A key notion above is that a strategy can contain another strategy.  In the above example, the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Change1Strategy </code>could have easily pre-instantiated the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">CurveStrategy </code>and saved it in a field for use when it was needed.  But the does it matter exactly which concrete strategy is being used?   If not, why not work at a higher abstraction level and let one strategy hold a reference to an <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">abstract </emphasis>strategy?  For instance, consider the following code:  <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">
	package ballworld;
	import java.awt.*;

	public class SwitcherStrategy implements IUpdateStrategy {
  
  		private IUpdateStrategy _strategy = new StraightStrategy();
  
  		public void updateState(Ball context) {
    			_strategy.updateState(context);
  		}
  
  		public void setStrategy(IUpdateStrategy newStrategy) {
    			_strategy = newStrategy;
  		}
	}
</code>This strategy doesn't look like it does much, but looks are deceiving.  All the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">SwitcherStrategy </code>does is to delegate the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">updateState </code>method to the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">_strategy </code>that it holds.   This does not seem much in of itself, but consider the fact that the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">SwitcherStrategy </code>also has a settor method for <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">_strategy</code>.   This means that the strategy held can be changed at run time!  More importantly, suppose a ball is instantiated with a <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">SwitcherStrategy</code>.  The behavior of the ball would be that of whatever strategy is being held by the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">SwitcherStrategy </code>since the switcher just delegates to the held strategy.   If one were to have a reference to that <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">SwitcherStrategy </code>instance from somewhere else, one could then change the internal strategy.   The ball is none the wiser because all it has is a reference to the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">SwitcherStrategy </code>instance, which hasn't changed at all!    However, since the held strategy is now different, the ball's <emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">behavior </emphasis>has completely changed!   This is an example of the <link xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" src="http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/DecoratorPattern.htm">Decorator Design Pattern</link>, where the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">SwitcherStrategy </code>class is formally called the <term xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">decorator </term>and the held strategy is formally called the <term xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">decoree</term>.  In theoretical terms, the decorator is what is known as an <term xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">indirection layer</term>, which is like a buffer between two enities that enables them to depend on each other but yet still be free to move and change with respect to each other.   A very useful analogy for indirection layers is like the thin layer of oil that will enable two sheets of metal to slide easily past each other.</para>
				</section>
				<section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s60">
					<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Balls with multiple, composite behaviors</name>
					<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s60p10">Now that we can dynamically change a ball's behavior, let's tackle another problem:  <exercise xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s60e10">
							<problem xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
								<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s60e10p10">How can we have balls with multiple behaviors but yet not duplicate code for each one of those behaviors? </para>
							</problem>
							<solution xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
								<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s60e10p20">Use the same techniques as before:  strategies that hold strategies.</para>
							</solution>
						</exercise>Let's start with a very straightforward solution:<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">
	package ballworld;
	import java.awt.*;

	public class DoubleStrategy implements IUpdateStrategy {
  
  		private IUpdateStrategy _s1 = new CurveStrategy();
    		private IUpdateStrategy _s2 = new BreathingStrategy();
  
  		public void updateState(Ball context) {
    			_s1.updateState(context);
    			_s2.updateState(context);
  		}
  	}
</code>Ta da!  No problem.  The <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">DoubleStrategy </code>simply holds two strategies and delegates to each of them in turn when asked to <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">updateState</code>.    So why stop here?<code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">
	package ballworld;
	import java.awt.*;

	public class TripleStrategy implements IUpdateStrategy {
  
  		private IUpdateStrategy _s1 = new CurveStrategy();
    		private IUpdateStrategy _s2 = new BreathingStrategy();
    		private IUpdateStrategy _s3 = new BreathingStrategy();
 
  		public void updateState(Ball context) {
    			_s1.updateState(context);
    			_s2.updateState(context);
    			_s3.updateState(context);
  		}
  	}
</code>We're on a roll now!  We could go on and on, making as complex a strategy as we'd like, making a new class for each combination we want.   But somewhere around the 439'th combination, we get mightly tired of writing classes.   Isn't there an easier way? </para>
					<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s60p20">Abstraction is the key here.   We want to write code that represents that abstraction of multiple, composite strategies.  Does what we were doing above depend on the particular concrete strategies that we were using?   No?  Then we should eliminate the concrete classes, raise the abstraction level and use the abstract superclass (interface) instead.   For a combination of two behaviors, we end up with the following: <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">
	package ballworld;
	import java.awt.*;

	public class MultiStrategy implements IUpdateStrategy {

  		private IUpdateStrategy _s1;
  		private IUpdateStrategy _s2;

  		public MultiStrategy(IUpdateStrategy s1, IUpdateStrategy s2) {
    			_s1 = s1;
    			_s2 = s2;
  		}
  
  		public void updateState(Ball context) {
    			_s1.updateState(context);
    			_s2.updateState(context);
  		}
	}
</code>Notice how we have added a constructor that enables us to initialize the two abstract strategy fields.  All we have to do is to construct a <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">MultiStrategy </code>object with the two desired strategies, and we're good to go!  </para>
					<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s60p30"><exercise xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s60p30e10">
							<problem xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
								<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s60p30e10p10">So if we want three behaviors, all we have to do is to make the same sort of thing but with 3 abstract strategy fields, right?  </para>
							</problem>
							<solution xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
								<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s60p30e10p20">But isn't a <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">MultiStrategy </code>an <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">IUpdateStrategy</code> iteself?   That is, since a <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">MultiStrategy </code>holds <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">IUpdateStrategy</code>'s, couldn't a <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Multistrategy </code>hold a <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Multistrategy </code>, which is holding a <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Multistrategy </code>(or two) which is hold a <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Multistrategy </code>, which is holding.....?   </para>
							</solution>
						</exercise><emphasis xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Thus, with just a <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Multistrategy </code> we are capable of composing arbitrarily complex behaviors! </emphasis></para>
				</section>
				<section xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s70">
					<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Composite Patterns</name>
					<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s70p10">So what have we wrought here?  Let's take a look at the UML diagram of our to most abstract strategies.<figure xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s70p10f10">
							<name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">SwitcherStrategy and MultiStrategy</name>
							<media xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="image/pn" src="strategy.png"/>
							<caption xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Note that the subclasses hold references to their own superclasses!</caption>
						</figure></para>
					<para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="s60p40">The key to the power that lies in the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">SwitcherStrategy </code>and the <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">MultiStrategy </code>lies in the fact that they hold references to their own superclass, <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">IUpdateStrategy</code>.   This is what enables them to be create any behavior they want, including combinations of behaviors and dynamic modifications of behaviors.   This self-referential class structure is  known as the <link xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" src="http://www.exciton.cs.rice.edu/JavaResources/DesignPatterns/composite.htm">Composite Design Pattern</link> (The Decorator pattern can be considered to be specialized form of the Composite pattern).   The massive power, flexibility and extensiblility that this pattern generates warrants further, more formal study, which is where we're heading next.   Stay tuned!   </para>
				</section>
			</section>
		</section>
	</content>
</document>
