• Deutsch (Deutschland)
    • English (United States)
    • العربية (مصر)
    • Español (España, alfabetización internacional)
    • français (France)
    • हिंदी (भारत)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • русский (Россия)
    • ไทย (ไทย)
    • Türkçe (Türkiye)
    • Tiếng Việt (Việt Nam)
    • 中文(中华人民共和国)
    • 中文(香港特別行政區)
  • Anmeldung
  • Registrieren
DotNetAge - Mvc & jQuery CMS
Seitenleiste ausblenden

Flyweight


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
creates and manages flyweight objects

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
maintains a reference to flyweight(s).

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

 


    Average:
  • Liest
    (724)
  • Permalink
Zu teilen:

Tag cloud

Anything in here will be replaced on browsers that support the canvas element