13 April 2014

Flyweight Design Pattern -- Design Pattern

    Use sharing to support large numbers of fine-grained objects efficiently.


Participants

    The classes and/or objects participating in this pattern are:
  • Flyweight   (Character)
       Declares an interface through which flyweights can receive and act on extrinsic state.
  • Concrete Flyweight(CharacterA, CharacterB... CharacterZ)
        Implements the Flyweight interface and add storage for intrinsic state,
if any. A Concrete Flyweight object must be sharable. Any state it stores must be intrinsic,
that is, it must be independent of the Concrete Flyweight object's context.
  • Un-shared Concrete Flyweight( not used )
       Not all Flyweight sub classes need to be shared. The Flyweight interface enables sharing, but it doesn't enforce it. It is common for Un-shared Concrete Flyweight objects to have Concrete Flyweight objects as children at some level in the flyweight object structure (as the Row and Column classes have).
  • Flyweight Factory(Character Factory)
      Creates and manages flyweight objects  Ensures that flyweight is shared properly.

      When a client requests a flyweight, the Flyweight Factory objects assets an existing instance or creates one, if none exists.
  •      Client(Flyweight App)
     Maintains a reference to flyweight(s).

    Computes or stores the extrinsic state of flyweight(s).

Example for Fly weight Design 

using System;
using System.Collections;

namespace Flyweight.Structural
{
  /// <summary>
  /// MainApp startup class for Structural
  /// Flyweight Design Pattern.
  /// </summary>
  class MainApp
  {
    /// <summary>
    /// Entry point into console application.
    /// </summary>
    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>
  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>
  abstract class Flyweight
  {
    public abstract void Operation(int extrinsicstate);
  }

  /// <summary>
  /// The 'ConcreteFlyweight' class
  /// </summary>
  class ConcreteFlyweight : Flyweight
  {
    public override void Operation(int extrinsicstate)
    {
      Console.WriteLine("ConcreteFlyweight: " + extrinsicstate);
    }
  } 
  /// <summary>
  /// The 'UnsharedConcreteFlyweight' class
  /// </summary>
  class UnsharedConcreteFlyweight : Flyweight
  {
    public override void Operation(int extrinsicstate)
    {
      Console.WriteLine("UnsharedConcreteFlyweight: " +
        extrinsicstate);
    }
  }
}

Output
ConcreteFlyweight: 21
ConcreteFlyweight: 20
ConcreteFlyweight: 19
UnsharedConcreteFlyweight: 18


No comments:

Post a Comment