|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis 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 PatternAs 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 worksThe 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 Code SampleThe 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 classThe 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 classThe pool holds all validator objects in an 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 itIf 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 OptimizationsThere are a few optimizations to this model depending on your implementation:
Copyright 2001 ITG Israel LTD. All Rights Reserved. Enjoy Programming!!!
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||