Приспособленец (Flyweight)
Используйте паттерн Приспособленец, если один экземпляр класса может предоставлять много “виртуальных экземпляров”.
-
Flyweight - абстрактный приспособленец
-
ConcreteFlyweight - конкретный приспособленец
-
UnsharedConcreteFlyweight - неразделяемый конкретный приспособленец
-
FlyweightFactory - фабрика приспособленцев
-
Client - клиент
Абстрактная реализация на C# (GoF)
/// <summary>
/// MainApp startup class for Structural
/// Flyweight Design Pattern.
/// </summary>
internal class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
private static void Main()
{
// Arbitrary extrinsic state
int extrinsicstate = 22;
FlyweightFactory factory = new FlyweightFactory();
// Work with different flyweight instances
Flyweight fx = factory.GetFlyweight("X");
fx.Operation(--extrinsicstate);
Flyweight fy = factory.GetFlyweight("Y");
fy.Operation(--extrinsicstate);
Flyweight fz = factory.GetFlyweight("Z");
fz.Operation(--extrinsicstate);
UnsharedConcreteFlyweight fu = new
UnsharedConcreteFlyweight();
fu.Operation(--extrinsicstate);
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'FlyweightFactory' class
/// </summary>
internal class FlyweightFactory
{
private Hashtable flyweights = new Hashtable();
// Constructor
public FlyweightFactory()
{
flyweights.Add("X", new ConcreteFlyweight());
flyweights.Add("Y", new ConcreteFlyweight());
flyweights.Add("Z", new ConcreteFlyweight());
}
public Flyweight GetFlyweight(string key)
{
return ((Flyweight)flyweights[key]);
}
}
/// <summary>
/// The 'Flyweight' abstract class
/// </summary>
internal abstract class Flyweight
{
public abstract void Operation(int extrinsicstate);
}
/// <summary>
/// The 'ConcreteFlyweight' class
/// </summary>
internal class ConcreteFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("ConcreteFlyweight: " + extrinsicstate);
}
}
/// <summary>
/// The 'UnsharedConcreteFlyweight' class
/// </summary>
internal class UnsharedConcreteFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("UnsharedConcreteFlyweight: " +
extrinsicstate);
}
}
Реальная реализация на C# (GoF)
/// <summary>
/// MainApp startup class for Real-World
/// Flyweight Design Pattern.
/// </summary>
internal class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
private static void Main()
{
// Build a document with text
string document = "AAZZBBZB";
char[] chars = document.ToCharArray();
CharacterFactory factory = new CharacterFactory();
// extrinsic state
int pointSize = 10;
// For each character use a flyweight object
foreach (char c in chars)
{
pointSize++;
Character character = factory.GetCharacter(c);
character.Display(pointSize);
}
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'FlyweightFactory' class
/// </summary>
internal class CharacterFactory
{
private Dictionary<char, Character> characters = new Dictionary<char, Character>();
public Character GetCharacter(char key)
{
// Uses "lazy initialization"
Character character = null;
if (characters.ContainsKey(key))
{
character = characters[key];
}
else
{
switch (key)
{
case 'A': character = new CharacterA(); break;
case 'B': character = new CharacterB(); break;
//...
case 'Z': character = new CharacterZ(); break;
}
characters.Add(key, character);
}
return character;
}
}
/// <summary>
/// The 'Flyweight' abstract class
/// </summary>
internal abstract class Character
{
protected char symbol;
protected int width;
protected int height;
protected int ascent;
protected int descent;
protected int pointSize;
public abstract void Display(int pointSize);
}
/// <summary>
/// A 'ConcreteFlyweight' class
/// </summary>
internal class CharacterA : Character
{
// Constructor
public CharacterA()
{
this.symbol = 'A';
this.height = 100;
this.width = 120;
this.ascent = 70;
this.descent = 0;
}
public override void Display(int pointSize)
{
this.pointSize = pointSize;
Console.WriteLine(this.symbol +
" (pointsize " + this.pointSize + ")");
}
}
/// <summary>
/// A 'ConcreteFlyweight' class
/// </summary>
internal class CharacterB : Character
{
// Constructor
public CharacterB()
{
this.symbol = 'B';
this.height = 100;
this.width = 140;
this.ascent = 72;
this.descent = 0;
}
public override void Display(int pointSize)
{
this.pointSize = pointSize;
Console.WriteLine(this.symbol +
" (pointsize " + this.pointSize + ")");
}
}
// ... C, D, E, etc.
/// <summary>
/// A 'ConcreteFlyweight' class
/// </summary>
internal class CharacterZ : Character
{
// Constructor
public CharacterZ()
{
this.symbol = 'Z';
this.height = 100;
this.width = 100;
this.ascent = 68;
this.descent = 0;
}
public override void Display(int pointSize)
{
this.pointSize = pointSize;
Console.WriteLine(this.symbol +
" (pointsize " + this.pointSize + ")");
}
}
Улучшенная реальная реализация на C# (GoF)
/// <summary>
/// MainApp startup class for .NET optimized
/// Flyweight Design Pattern.
/// </summary>
internal class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
private static void Main()
{
// Build a document with text
string document = "AAZZBBZB";
char[] chars = document.ToCharArray();
var factory = new CharacterFactory();
// extrinsic state
int pointSize = 10;
// For each character use a flyweight object
foreach (char c in chars)
{
var character = factory[c];
character.Display(++pointSize);
}
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'FlyweightFactory' class
/// </summary>
internal class CharacterFactory
{
private Dictionary<char, Character> characters = new Dictionary<char, Character>();
// Character indexer
public Character this[char key]
{
get
{
// Uses "lazy initialization" -- i.e. only create when needed.
Character character = null;
if (characters.ContainsKey(key))
{
character = characters[key];
}
else
{
// Instead of a case statement with 26 cases (characters).
// First, get qualified class name, then dynamically create instance
string name = this.GetType().Namespace + "." + "Character" + key.ToString();
character = (Character)Activator.CreateInstance(Type.GetType(name));
}
return character;
}
}
}
/// <summary>
/// The 'Flyweight' class
/// </summary>
internal class Character
{
protected char symbol;
protected int width;
protected int height;
protected int ascent;
protected int descent;
public void Display(int pointSize)
{
Console.WriteLine(this.symbol +
" (pointsize " + pointSize + ")");
}
}
/// <summary>
/// A 'ConcreteFlyweight' class
/// </summary>
internal class CharacterA : Character
{
// Constructor
public CharacterA()
{
this.symbol = 'A';
this.height = 100;
this.width = 120;
this.ascent = 70;
this.descent = 0;
}
}
/// <summary>
/// A 'ConcreteFlyweight' class
/// </summary>
internal class CharacterB : Character
{
// Constructor
public CharacterB()
{
this.symbol = 'B';
this.height = 100;
this.width = 140;
this.ascent = 72;
this.descent = 0;
}
}
// ... C, D, E, etc.
/// <summary>
/// A 'ConcreteFlyweight' class
/// </summary>
internal class CharacterZ : Character
{
// Constructor
public CharacterZ()
{
this.symbol = 'Z';
this.height = 100;
this.width = 100;
this.ascent = 68;
this.descent = 0;
}
}
Реализация на JAVA
TODO