#include <iostream>
using namespace std;
class DisplayDriver {
public:
virtual void display() {}
};
class LowDisplayDriver : public DisplayDriver {
public:
virtual void display() { cout << "low display" << endl;}
};
class HighDisplayDriver : public DisplayDriver {
public:
virtual void display() { cout << "high display" << endl; }
};
class PrintDriver {
public:
virtual void print(){}
};
class LowPrintDriver : public PrintDriver {
public:
virtual void print() { cout << "low print" << endl; }
};
class HighPrintDriver : public PrintDriver {
public:
virtual void print() { cout << "high print" << endl; }
};
class Factory {
public:
virtual DisplayDriver *getDispDrvr() = 0;
virtual PrintDriver *getPrtDrvr() = 0;
};
class LowFactory : public Factory {
public:
DisplayDriver *getDispDrvr() { return new LowDisplayDriver; }
PrintDriver *getPrtDrvr() { return new LowPrintDriver; }
};
/*
// singleton version
class LowFactorySingleton : public Factory {
public:
static LowFactorySingleton* Instance()
{
if (_instance == 0)
_instance = new LowFactorySingleton;
return _instance;
}
PrintDriver *getPrtDrvr() { return new LowPrintDriver; }
DisplayDriver *getDispDrvr() { return new LowDisplayDriver; }
protected:
LowFactorySingleton() { }
private:
static LowFactorySingleton* _instance;
};
LowFactorySingleton* LowFactorySingleton::_instance = 0;
*/
class MyDriver {
public:
DisplayDriver *CreateDispDrvr(Factory *factory)
{
return factory->getDispDrvr();
}
PrintDriver *CreatePrtDrvr(Factory *factory)
{
return factory->getPrtDrvr();
}
};
int
main()
{
MyDriver *myDr = new MyDriver;
Factory *mylowFac = new LowFactory;
DisplayDriver *myDispDr = myDr->CreateDispDrvr(mylowFac);
PrintDriver *myPrtDr = myDr->CreatePrtDrvr(mylowFac);
myDispDr->display();
myPrtDr->print();
delete myDr;
delete mylowFac;
delete myDispDr;
delete myPrtDr;
return 0;
}