﻿<?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>Flyweight 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">Flyweight 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 Flyweight 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>: Use sharing to support large numbers of fine-grained objects efficiently.</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="Flyweight in UML" src="../../spec/gof/uml/Flyweight.png" /> <br />

<p><strong>Participants</strong>: </p>
<ul>
	<li><b>Flyweight</b>: declares an interface through which flyweights can receive and act on extrinsic state.</li>
	<li><b>ConcreteFlyweight</b>: implements the Flyweight interface and adds storage for intrinsic state, if any. A ConcreteFlyweight object must be sharable. Any state it stores must be intrinsic; that is, it must be independent of the ConcreteFlyweight object's context.</li>
	<li><b>UnsharedConcreteFlyweight</b>: not all Flyweight subclasses need to be shared. The Flyweight interface enables sharing; it doesn't enforce it. It's common for UnsharedConcreteFlyweight objects to have ConcreteFlyweight objects as children at some level in the flyweight object structure (as the Row and Column classes have).</li>
	<li><b>FlyweightFactory</b>: creates and manages flyweight objects, ensuring that flyweights are shared properly. When a client requests a flyweight, the FlyweightFactory object supplies an existing instance or creates one, if none exists.</li> 
	<li><b>Client</b>: maintains a reference to flyweight(s), and computes (or stores) their extrinsic state.</li>
</ul>

<p><strong>Collaborations</strong>:</p>
	<ul>
		<li>State that a flyweight needs to function must be characterized as either intrinsic or extrinsic. Intrinsic state is stored in the ConcreteFlyweight object; extrinsic state is stored or computed by Client objects. Clients pass this state to the flyweight when they invoke its operations.</li>
		<li>Clients should not instantiate ConcreteFlyweights directly. Clients must obtain ConcreteFlyweight objects exclusively from the FlyweightFactory object to ensure they are shared properly.</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/Flyweight.xml">Flyweight 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="Flyweight" alt="Flyweight in LePUS3" src="../../spec/gof/lepus3/Flyweight.gif" /></td>
</tr>
<tr>
	<th>Flyweight in LePUS3 (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
<tr>
	<td><doc:include value="../../spec/gof/classz/Flyweight.xml" /></td>
</tr>
<tr>
	<th>Flyweight in Class-Z (<a href="../legend/legend.xml">legend</a>)</th>
</tr>
</table>
</body>
</html>
