Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Flyweight Pattern

0.00/5 (No votes)
3 Oct 2008 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

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here