creativity is that spark of electricity before an explosion.
it allows us, in a single instance to explore all possibilities without hesitation.
Use sharing to support large numbers of fine-grained objects efficiently.
The classes and/or objects participating in this pattern are:
using System;
using System.Collections;
namespace DoFactory.GangOfFour.Flyweight.Structural
{
// MainApp test application
class MainApp
{
static void Main()
{
// Arbitrary extrinsic state
int extrinsicstate = 22;
FlyweightFactory f = new FlyweightFactory();
// Work with different flyweight instances
Flyweight fx = f.GetFlyweight("X");
fx.Operation(--extrinsicstate);
Flyweight fy = f.GetFlyweight("Y");
fy.Operation(--extrinsicstate);
Flyweight fz = f.GetFlyweight("Z");
fz.Operation(--extrinsicstate);
UnsharedConcreteFlyweight uf = new
UnsharedConcreteFlyweight();
uf.Operation(--extrinsicstate);
// Wait for user
Console.Read();
}
}
// "FlyweightFactory"
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]);
}
}
// "Flyweight"
abstract class Flyweight
{
public abstract void Operation(int extrinsicstate);
}
// "ConcreteFlyweight"
class ConcreteFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("ConcreteFlyweight: " + extrinsicstate);
}
}
// "UnsharedConcreteFlyweight"
class UnsharedConcreteFlyweight : Flyweight
{
public override void Operation(int extrinsicstate)
{
Console.WriteLine("UnsharedConcreteFlyweight: " +
extrinsicstate);
}
}
}
Output expected:
ConcreteFlyweight: 21
ConcreteFlyweight: 20
ConcreteFlyweight: 19
UnsharedConcreteFlyweight: 18
| [+] | The Apache Jakarta Project has released JMeter 2.3 |
| [+] | Extensible Markup Language (XML) |
| [+] | Programming - Design patterns in Software |
| [+] | Fujitsu Expands Linux Offering |
| [+] | JavaFX goes head to head against Flash and Silverlight in the RIA stakes |
| [+] | Windows Emulator WINE finally comes of age |
| [+] | Why Optimization matters in Software Development |
| [+] | Programming - Replacing DB applications with high performance XML solutions on Websites |
| [+] | Programming - LINQ Examples |
| [+] | Programming - Design patterns in Software |
| [+] | .NET Enterprise provides a truly robust and scalable platform to build business applications on I.. |
| [+] | Case studies - Software in business |
| [+] | Artificial Neural Networks |
