As Gamma et al. point out
Consider a user interface toolkit that supports multiple look-and-feel standards, such as Motif and Presentation Manager. Different look-and-feels define different appearances and behaviors for user interface "widgets" like scroll bars, windows, and buttons. To be portable across look-and-feel standards, an application should not hard-code its widgets for a particular look and feel. Instantiating look-and-feel-specific classes of widgets throughout the application makes it hard to change the look and feel later.One aspect of this separation is that objects in the class must be created without the main programs knowledge of their exact nature. The Abstract factory is used for this purpose.
This hierarchy of classes was constructed in much the same was as the hierarchy of Employees in the second grocery list problem. The member data items shared by all classes were moved to the base class. Access to these variables is through the shared operations of the hierarchy. The functions that return String types are needed to send messages to the interface. When the user adds a new account, the member variables will be initialized, so the last two parameters of the abstract factory create are the variables needed to initalize each instance. Notice that clone also has these parameters. You will us a TComboBox to list types of accounts in the account dialog. TComboBoxes can be initialized by setting their Item->Text property to a string. That string is returned by listAcountTypes.
If the actual Account classes were defined in the header, all of this exercise would be overkill, since every time the list of classes changed the whole project would have to be recompiled. To encapsulate the actual classes they are defined in the cpp file .
class Account
{
public:
enum {SAVINGS,CHECKING};
static Account* create(int,int=0,double=0);
static String listAccountTypes();
virtual void read(istream &);
virtual void write(ostream & );
virtual void print(ostream &);
virtual void deposit(double);
virtual void withdraw(double);
String getId();
String getBalance();
protected:
Account();
Account(int id,double amount);
double balance();
private:
virtual Account* clone(int=0,double=0)=0;
int _acctNumber;
double _balance;
};
typedef RcPointer< Account> AcctPtr;
// All actual Account classes are defined in the cpp file so the encapsulation is complete.
class AccountArray
{
public:
AccountArray();
void read();
void write();
AcctPtr& operator[] (int);
void print(ostream&);
void insertItem(AcctPtr);
void insertItemAt(AcctPtr,int);
void deleteItemAt(int);
private:
Array< AcctPtr> accts;
};
To see the cpp file for this class click here .