﻿<?xml-stylesheet type="text/xsl" href="../../templates/doc.xsl"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
<html 
	xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:doc="http://www.lepus.org.uk/doc" 
	xmlns:classz="http://www.lepus.org.uk/classz" 
	xmlns:fopl="http://www.lepus.org.uk/fopl" 
	xml:lang="EN-GB" 
	xmlns:v="urn:schemas-microsoft-com:vml" 
	xmlns:o="urn:schemas-microsoft-com:office:office">

<head>
	<link rel="stylesheet" type="text/css" href="../../templates/doc.css" />
	<title>Strategy pattern: The LePUS3 &amp; Class-Z companion to the GoF</title>
	<meta http-equiv="Content-Style-Type" content="text/css" />
	<meta name="Author" content="Amnon H Eden" />
</head>


<body>


<h5><a href="./index.xml">The 'Gang of Four' Companion:</a></h5>

<p class="pagetitle">Strategy Design Pattern</p>

<h5><a href="./index.xml">Formal specification of design patterns in LePUS3 and Class-Z</a></h5>

<p><a href="companion.pdf" style="border:0px">
	<img alt="Print this document" src="../../site/print.gif" /></a></p>

<div class="abstract">

	<p>This page is part of the <a href="./index.xml">The 'Gang of Four' Companion</a> 
	dedicated to the formal specification in LePUS3 and Class-Z of patterns from the 'Gang of Four' catalogue [<a href="http://www.lepus.org.uk/ref/index.xml#bib">Gamma 
	et al 1995</a>].</p>

</div> <!-- Abstract-->

<doc:toc />

<h3>Links</h3>

<div class="abstract">

<ul>
		<li><a href="../legend/legend.xml">Legend: Key to LePUS3 and Class-Z Symbols</a></li>
		<li><a href="ref/refman/refman.xml">Reference Manual for LePUS3 &amp; Class-Z </a></li>
		<li><a href="faq.xml">Modelling Design Patterns: Frequently-Asked Questions</a></li>
	</ul>

	<p>Download:</p>
	
	<ul>
		<li><a href="../../spec/gof/lepus3/gof.vsd">The 'gang of four' patterns in LePUS3 (Visio 2003 format)</a></li>
		<li><a href="../../spec/gof/uml/gof.vsd">The 'gang of four' patterns in UML (Visio 2003 format)</a></li>
	</ul>
	
</div> <!-- Links -->


<h1>The Strategy design motif</h1>

<p>The informal description: Excerpts from [<a href="../index.xml#bib">Gamma et al. 1995</a>] (adapted for 
	this purpose):</p>

<p><strong>Intent</strong>: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.</p>
	
<p><strong>Structure</strong>: Original OMT diagram converted to UML (<a href="faq.xml#Why_did_you_convert_the_original_diagrams_to_UML_and_how?">Why 
	and How?</a>):</p>

<img alt="Strategy in UML" src="../../spec/gof/uml/Strategy.png" /> <br />

<p><strong>Participants</strong>: </p>
<ul>
	<li><b>Strategy</b>: declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.</li>
	<li><b>ConcreteStrategy</b>: implements the algorithm using the Strategy interface.</li>
	<li><b>Context</b>:
		<ul>
			<li>Is configured with a ConcreteStrategy object.</li>
			<li>Maintains a reference to a Strategy object.</li>
			<li>May define an interface that lets Strategy access its data.</li>
		</ul>
	</li>
</ul>

<p><strong>Collaborations</strong>:</p>
<ul>
	<li>Strategy and Context interact to implement the chosen algorithm. A context may pass all data required by the algorithm to the strategy when the algorithm is called. Alternatively, the context can pass itself as an argument to Strategy operations. That lets the strategy call back on the context as required.</li>
	<li>A context forwards requests from its clients to its strategy. Clients usually create and pass a ConcreteStrategy object to the context; thereafter, clients interact with the context exclusively. There is often a family of ConcreteStrategy classes for a client to choose from.</li>
</ul>

<h1>Formal specification</h1>
<p>See also:</p>
<ul>
		<li><a href="../legend/legend.xml">Legend: Key to LePUS3 and Class-Z Symbols</a></li>
		<li><a href="ref/refman/refman.xml">Reference Manual for LePUS3 &amp; Class-Z </a></li>
		<li><a href="../../spec/gof/classz/Strategy.xml">Strategy in XML</a></li>
		<li><a href="faq.xml">Modelling Design Patterns: Frequently-Asked Questions</a></li>
	</ul>

<table class="chartandschema">
<tr>
	<td>
	<img class="chart" id="Strategy" alt="Strategy in LePUS3" src="../../spec/gof/lepus3/Strategy.gif" /></td>
</tr>
<tr>
	<th>Strategy in LePUS3 (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
<tr>
	<td><doc:include value="../../spec/gof/classz/Strategy.xml" /></td>
</tr>
<tr>
	<th>Strategy in Class-Z (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
</table>
</body>
</html>
