Home > C#, Design Pattern, Programming > Flyweight

Flyweight

Frequency of Use
Low

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

Structure

20140531-125430-46470258.jpg

Applicability
The Flyweight pattern’s effectiveness depends heavily on how and where it’s used. Apply the Flyweight pattern when all of the following are true:

  • An application uses a large number of objects.
  • Storage costs are high because of the sheer quantity of objects.
  • Most object state can be made extrinsic.
  • Many groups of objects may be replaced by relatively few shared objects once extrinsic state is removed.
  • The application doesn’t depend on object identity. Since flyweight objects may be shared, identity tests will return true for conceptually distinct objects.

Code Implementatio  (C#)

using System;
using System.Collections;

namespace Flyweight
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            FlyweightFactory ff = new FlyweightFactory();

            int state = 20;
            Flyweight fx = ff.GetFlyweight("X");
            fx.Operation(state);

            Flyweight fy = ff.GetFlyweight("Y");
            fy.Operation(state);

            Flyweight fz = ff.GetFlyweight("Z");
            fz.Operation(state);

            UnsharedConcreteFlyweight fu = new UnsharedConcreteFlyweight();
            fu.Operation(state);

        }
    }

    class FlyweightFactory
    {
        private Hashtable _flyweights = new Hashtable();

        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]);
        }
    }

    abstract class Flyweight
    {
        public abstract void Operation(int extrinsicstate);
    }

    class ConcreteFlyweight : Flyweight
    {
        public override void Operation(int extrinsicstate)
        {
            Console.WriteLine("ConcreteFlyweight:" + extrinsicstate);
        }
    }

    class UnsharedConcreteFlyweight : Flyweight
    {
        public override void Operation(int extrinsicstate)
        {
            Console.WriteLine("UnsharedConcreteFlyweight:" + extrinsicstate);
        }
    }

}

Code Output

ConcreteFlyweight:20
ConcreteFlyweight:20
ConcreteFlyweight:20
 
UnsharedConcreteFlyweight:20
Advertisements
Categories: C#, Design Pattern, Programming
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: