Definition
Use sharing to support large numbers of fine-grained objects efficiently.
UML class diagram
Participants
The classes and/or objects participating in this pattern are:
- Flyweight - declares an interface through which flyweights can receive and act on extrinsic state.
- ConcreteFlyweight - 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.
- UnsharedConcreteFlyweight - not all Flyweight subclasses need to be shared. The Flyweight interface enables sharing, but it doesn't enforce it. It is 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).
- FlyweightFactory
ensures that flyweight are shared properly. When a client requests a flyweight, the FlyweightFactory objects assets an existing instance or creates one, if none exists.
- Client
computes or stores the extrinsic state of flyweight(s).
Sample code in C#
#region Helper
public enum eNumbers
{
One,
Two,
Three,
// .. and so on with every number
}
#endregion
#region The Flyweight Factory
public class NumbersFactory
{
#region Members
private Dictionary_numbers =
new Dictionary();
#endregion
#region Methods
public Number GetNumber(eNumbers number)
{
if (!_numbers.ContainsKey(number))
{
switch (number)
{
case (eNumbers.One):
{
_numbers.Add(number, new One());
break;
}
case (eNumbers.Two):
{
_numbers.Add(number, new Two());
break;
}
case (eNumbers.Three):
{
_numbers.Add(number, new Three());
break;
}
// ... and so on with every number
default:
{
break;
}
}
}
return _numbers[number];
}
#endregion
}
#endregion
#region The Flyweight
public abstract class Number
{
#region Members
protected int _number;
protected string _numberName;
protected int _numberSize;
#endregion
#region Methods
public abstract void WriteNumber(int numberSize);
#endregion
}
#endregion
#region The Flyweight Concrete
public class One : Number
{
#region Ctor
public One()
{
this._number = 1;
this._numberName = "One";
}
#endregion
#region Methods
public override void WriteNumber(int numberSize)
{
this._numberSize = numberSize;
Console.WriteLine(string.Format("{0} is size {1}",
_numberName, _numberSize));
}
#endregion
}
public class Two : Number
{
#region Ctor
public Two()
{
this._number = 2;
this._numberName = "Two";
}
#endregion
#region Methods
public override void WriteNumber(int numberSize)
{
this._numberSize = numberSize;
Console.WriteLine(string.Format("{0} is size {1}",
_numberName, _numberSize));
}
#endregion
}
public class Three : Number
{
#region Ctor
public Three()
{
this._number = 3;
this._numberName = "Three";
}
#endregion
#region Methods
public override void WriteNumber(int numberSize)
{
this._numberSize = numberSize;
Console.WriteLine(string.Format("{0} is size {1}",
_numberName, _numberSize));
}
#endregion
}
// ... Four, Five and so on
#endregion
-
-
Liest(724)
-
Permalink