An Introduction to Design Patterns, part II
Publication: C++ Builder Developer's Journal Issue: Volume 8, Number 9, September 2004
In this article I extended the TFileObject class into 2 subclasses; TVCLStringList and TSTLStringList. I talk about exceptions and consistency in behaviour across the sibling classes and set the framework for more advanced discussions into individual design patterns.
Last month, I presented an overview of design patterns and we created an abstract base class, TFileObject, to serve as part of the set of core classes for exploring various design-pattern themes. This month, I’ll demonstrate how to use the Template Method design pattern; specifically, we’ll create two concrete TFileObject descendant classes.
//---------------------------------------------------------------------------
class TFileObject //: public TObject
{
private:
const unsigned int MAX_ITEMS;
public:
virtual int __fastcall Count() = 0;
virtual AnsiString __fastcall GetItem(unsigned int index = 0) = 0;
virtual void __fastcall SetItem(AnsiString item) = 0;
virtual void __fastcall Clear() = 0;
__property unsigned int MaxItems = { read=MAX_ITEMS } ;
__fastcall TFileObject(unsigned int ItemCount = 0);
virtual __fastcall ~TFileObject();
};
//---------------------------------------------------------------------------
class TVCLStringList : public TFileObject
{
private:
TStringList *MenuList;
public:
int __fastcall Count();
AnsiString __fastcall GetItem(unsigned int index = 0);
void __fastcall SetItem(AnsiString item);
void __fastcall Clear();
__fastcall TVCLStringList(unsigned int ItemCount = 0);
virtual __fastcall ~TVCLStringList() {delete MenuList;};
};
//---------------------------------------------------------------------------
class TSTLList : public TFileObject
{
private:
//Example only today, until I get the vector documentation...
typedef std::list<AnsiString> flistStrings;
flistStrings MenuList;
public:
int __fastcall Count();
AnsiString __fastcall GetItem(unsigned int index = 0);
void __fastcall SetItem(AnsiString item);
void __fastcall Clear();
__fastcall TSTLList(unsigned int ItemCount = 0);
virtual __fastcall ~TSTLList();
};