DCSIMG
Flyweight Pattern - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Flyweight Pattern

Flyweight Pattern

In the last post about structural patterns I'm going to explain how
and when you should use the flyweight pattern.
You can read my previous posts on structural patterns here:
Decorator pattern 
Proxy pattern
Facade pattern
Adapter pattern
Composite pattern
Bridge pattern

What is the Flyweight Pattern?
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 the post, 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.
In the next posts I'm going to start explaining about the creational patterns.

Comments

Maor David-Pur said:

גיל, גמו כל הפוסטים, גם זה מצטרף לנבחרת הנכבדה שלך.  אני מציע לך להשתמש באחד התוספים של  ה live writer כדי שהקוד שאתה משלב בפוסט יראה טוב. תוכל להשתמש ב copy as html או insert code.

blogs.microsoft.co.il/.../copy-source-as-html-copysourceashtml-for-visual-studio-2008-rtm.aspx

או

blogs.microsoft.co.il/.../Add-Code-To-Your-Blog-Posts.aspx

מאור

# April 20, 2008 8:33 PM

Gil Fink said:

תודה מאור

# April 20, 2008 9:00 PM

Gil Fink Blog said:

The first pattern in the creational pattern series is the singleton pattern. You can read my previous

# April 23, 2008 9:35 AM

Gil Fink on .Net said:

The post describe the chain of responsibility pattern and shows an example of how to use it in C#.

# July 12, 2008 1:19 PM