Click here to Skip to main content
Licence 
First Posted 4 Oct 2001
Views 79,313
Bookmarked 25 times

The Flyweight Pattern

By | 7 Oct 2001 | Article
This article discusses the Structural Pattern Flyweight, using a Visual C++ example.

Introduction

This article gives a brief description of the Flyweight pattern through the use of a data validator for user interfaces. The function implementation of the validator is not relevant and is not demonstrated here. The sample code with three validators given here was compiled in VC6 with SP5 in NT 4.0.

Flyweight Pattern

As described in the book Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma et al. ( Addison-Wesley, 1995 ) at page 195 (Also called GoF: Gang of Four): "Use sharing to support large numbers of fine grained objects efficiently".

Let's say you are developing UIs with lots of data validations (a good example is an editable List Control or a Grid) and you need to validate data entered by the user. In MFC applications you would override the command handler fired after the user ends data entry and validate the data for each UI element separately. Using the Flyweight pattern you write the validators once and you can easily add more validators through the development of your code.

How it works

The Flyweight has a pool of objects (also called a factory) and a function that returns a pointer to one of these objects when requested to do so. The way to request an object is through a key (in this example the getValidator function) which receives the key of the object as a string and returns a pointer to it. That's it!!!

Code Sample

The purpose of the code in the demo project is to demonstrate the Flyweight pattern, therefore the implementation might not be exactly brilliant. All function implementations as well as class declarations are written together inside file DataValidator.h.

The CDataValidator base class

The CDataValidator class is the base class for all validators. Notice that the ValidateString function forces a validation algorithm by using two virtual functions CheckMinus and CheckStringOrder. This is called a Template Method. The description of this pattern as described in GoF at page 325 is: "Define the skeleton of an algorithm in an operation, deferring some steps to subclasses."

class CDataValidator
{
public:
    CDataValidator(){}
    virtual bool ValidateString(const CString& sToValidate)
    {
        ////Template Method
        if(!CheckMinus(sToValidate))
            return false;

        if(!CheckStringOrder(sToValidate))
            return false;
	////End Template Method
        return true;

    }

	//For edit boxes
    virtual bool ValidateChar  (const CString& cToValidate)=0;

protected:
	//////Checks for the right order of characters in a double
    bool CheckDoublesOrder(const CString& sToValidate)
    {
        if(sToValidate.IsEmpty())
            return false;

        if(sToValidate[0]=='-'&& sToValidate.GetLength()>2)
        {
            if(sToValidate[1]=='0' && sToValidate[2]!='.')
                return false;
        }
        else
        {
			if(sToValidate[0]=='0' && sToValidate.GetLength()>1)
			{
				if(sToValidate[1]!='.')
				return false;
			}
			return true;
        }
        return true;

    }
	//////Checks for the right order of characters in an int
    bool CheckIntsOrder(const CString& sToValidate)
    {
        if(sToValidate.IsEmpty())
            return false;

        if(sToValidate[0]=='-' && sToValidate.GetLength()>1)
        {
            if(sToValidate[1]=='0')
                return false;
        }
        else
        {
            if(sToValidate[0]=='0' && sToValidate.GetLength()>1)
                return false;
        }
        return true;
    }

	//checks for the right positioning of the minus character
    virtual bool CheckMinus(const CString& sToValidate)
    {
        int index = sToValidate.Find("-");
        if(index >0)
            return false;
        return true;
    }

    virtual bool CheckStringOrder(const CString& sToValidate)=0;

};

The CDataValidatorPool class

The pool holds all validator objects in an std::map (I used and STL map because it makes more sense to me). If an object does not exist the pointer returned by the getValidator function is NULL.

class CDataValidatorPool
{
public:
    CDataValidatorPool()
    {
        m_ValidatorPool["INT"] = new CIntValidator;
        m_ValidatorPool["DBL"] = new CDoubleValidator;
        m_ValidatorPool["STR"] = new CStringValidator;
    }

    ~CDataValidatorPool()
    {
        std::map<CString,CDataValidator* >::iterator Iter;

        for(Iter=m_ValidatorPool.begin();Iter!=m_ValidatorPool.end();++Iter)
        {
            delete m_ValidatorPool[Iter->first];
        }

        m_ValidatorPool.clear();
    }

    CDataValidator* getValidator(const CString& sValidatorName)
    {
		//Can be optimized to create objects only when asked for.
		//Objects can also be singletons or even reference counted

        if(m_ValidatorPool.find(sValidatorName)!=m_ValidatorPool.end())
            return m_ValidatorPool[sValidatorName];
        else
            return NULL;
    }

private:
      std::map<CString,CDataValidator* > m_ValidatorPool;
};

How to use it

If you want to use the code demonstrated here in your project insert the DataValidator.h file in your project, create a member variable of the class CDataValidatorPool and of you go.

Optimizations

There are a few optimizations to this model depending on your implementation:

  1. You can make the pool object a Singleton (GoF page 127 as well as The Singleton Pattern in this site).
  2. You can create the objects on demand at the getValidator function instead of up front in the constructor.
  3. Each object can be a Singleton.
  4. You can add a reference count to each object.

Copyright 2001 ITG Israel LTD. All Rights Reserved.

Enjoy Programming!!!

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

About the Author

Alberto Bar-Noy

Team Leader
www.unitronics.com
Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks for the Israeli Pattern PinmemberJiggawha13:11 26 May '10  
Generalneed help. Pinmemberusafz6:35 26 Oct '06  
GeneralRe: need help. PinmemberAlberto Bar-Noy8:11 26 Oct '06  
GeneralRe: need help. Pinmemberusafz8:37 26 Oct '06  
GeneralRe: need help. Pinmemberusafz6:23 13 Nov '06  
can u provide the structure / design of what u you suggesting me ... ??
GeneralRe: need help. PinmemberAlberto Bar-Noy4:16 14 Nov '06  
GeneralRe: need help. Pinmemberusafz4:25 14 Nov '06  
GeneralSome doubts PinmemberTomasz Sowinski2:20 6 Oct '01  
GeneralRe: Some doubts PinmemberAlberto Gattegno21:45 6 Oct '01  
GeneralRe: Some doubts PinmemberTomasz Sowinski1:23 8 Oct '01  
GeneralRe: Some doubts PinmemberNice Scroll Effect20:50 14 Oct '01  
GeneralImplementing validators PinmemberRavi Bhavnani3:50 5 Oct '01  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 8 Oct 2001
Article Copyright 2001 by Alberto Bar-Noy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid