Flyweight Pattern

 Rate It (1)

The flyweight pattern is used whenever you have large amount of small objects
that share common information.
The use of the pattern reduces the storage of those objects.
The flyweight distinguishes between inner state (sometime called intrinsic state)
and outer state (sometime called extrinsic state) of the object.
The inner state can be shared by the objects and therefore minimize the amount
of storage needed by the objects.
The outer state can be computed or stored outside the objects and are given to
the objects whenever it's needed.
For a UML diagram of the pattern go to dofactory site.

Flyweight C# Example
Lets look at an example for the use of flyweight pattern:

    #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<eNumbers, Number> _numbers =

            new Dictionary<eNumbers, Number>();

 

        #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

The example include 3 parts - the flyweight factory (NumberFactory), the flyweight
itself (Number) and flyweight concretes (One, Two, Three). You can see that lazy loading
is used in the factory. Whenever a number is needed and isn't in the factory I build it and
then return it in the GetNumber method.

Summary
To sum up, use the flyweight when you have a large amount of objects that
consume large amount of memory. Also use the pattern when you have groups of
objects that share common state.
From my experience, the pattern is rarely used. Even though, you should know the
pattern in order to use it when it is needed

Revision number 1, Friday, October 03, 2008 8:21:20 PM by gilfink

Comments

Related Articles

Design Patterns

Design Patterns Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers. Design patterns are used throughout the ASP.NET Framework. The various patterns are commonly divided into several different groups

Prototype Pattern

The prototype is built upon the use of object cloning. The prototype creates new objects by cloning one of its concrete classes. The prototype is used in the following situations: You need to hide the concrete product classes from the client. You want to reduce

Threat Modeling

It's absolutely necessary if you're serious about security. Whitepapers/Books/Blogs Threat Modeling for ASP.NET (PDF) - an excellent white paper from Rüdiger Grimm and Henrik Eichstädt from the University of Kent Threat Modeling book from MSPress

IDisposable Pattern

The IDisposable pattern isn't one of the a classic patterns. It's a pattern suggested in MSDN to implement the IDisposable interface. You should be familiar with the pattern or with the interface because it's a basic thing to know about the .Net

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (5)
  2. SGWellens (4)
  3. maartenba (2)
  4. rami_nassar (2)
  5. stiansol (2)
  6. MisterFantastic (2)
  7. satish1.v (1)
  8. raklos (1)
  9. mosessaur (1)
  10. Jos Branders (1)

Advertise Here

Microsoft Communities
Page view counter