Hi my name is Kaur, I have studied c++ for about 1.5 months. I just finished experimenting with factory pattern and this is the code i came up with.
Is it rational, or maybe i didn't get the concept of factory pattern right?
I got 2 abstract classes AbstractComputer and AbstractFactor just for the interface for concrete classes. There are 3 different types of computers PC MAC and SPARC and a factory for everyone of them. ComputerAssamble creates Computer objects through factories. MainClass does the input check and decides which factory to feed ComputerAssamble.
#include <iostream>
#include <vector>
usingnamespace std;
class AbstractComputer{
public:
virtual string getCompleteDescription()=0;
static AbstractComputer* object();
};
class AbstractFactory{
public:
virtual AbstractComputer* produce()=0;
};
class PC : public AbstractComputer{
PC(){};
public:
static AbstractComputer* object(){returnnew PC;}
string getCompleteDescription(){ return"PC HAS BEEN PRODUCED\n";}
};
class MAC : public AbstractComputer{
MAC(){};
public:
static AbstractComputer* object(){returnnew MAC;}
string getCompleteDescription(){ return"MAC HAS BEEN PRODUCED\n";}
};
class SPARC : public AbstractComputer{
SPARC(){};
public:
static AbstractComputer* object(){returnnew SPARC;}
string getCompleteDescription(){ return"SPARC HAS BEEN PRODUCED\n";}
};
class PCFactory : public AbstractFactory{
public:
AbstractComputer* produce(){ return PC::object();}
};
class MACFactory : public AbstractFactory{
public:
AbstractComputer* produce(){ return MAC::object();}
};
class SPARCFactory : public AbstractFactory{
public:
AbstractComputer* produce(){ return SPARC::object();}
};
class ComputerAssambler{
public:
staticvoid AssambleComputer(AbstractFactory* factory){
if(factory == NULL)
;
else{
AbstractComputer* computer = factory->produce();
cout<<computer->getCompleteDescription();
}
}
};
class MainClass{
public:
staticvoid Main(string type){
AbstractFactory* factory=NULL;
if(type.compare("PC") == 0)
factory = new PCFactory;
elseif(type.compare("MAC") == 0)
factory = new MACFactory;
elseif(type.compare("SPARC") == 0)
factory = new SPARCFactory;
else
cout<<"This pc is not in production"<<endl;
ComputerAssambler::AssambleComputer(factory);
}
};
int main()
{
cout<<"Type the name of the computer you would like produced: PC, MAC, SPARC"<<endl;
char answer[4];
while(1==1)
{
scanf("%s",answer);
MainClass::Main(answer);
}
system("pause");
return 0;
};
The first thing wondering why you need a seperate factory for each type of computer - after all they inherit from AbstractComputer - so they can all be 'made' by the same factory.
Also the MainClass factory creates a specific factory such as SPARCFactory - but this specific factory does not actually create the product - it delegates it to a static function of the final product itself.
Have to agree with you. I took Design Patterns by GoF , didnt have the time to read it though, will do so tomorrow. I will post a new code if i manage to write something more rational.
I have to agree. I have seen a lot of over-engineering with classes that wrap and wrap each other layer by layer until the main picture is lost due to that in my work-place. Sometimes I wonder, why do developers over-engineer? To me the appropriate level of engineering is good enough. We do not have to carry it to the extreme.
I guess GoF has some blame to share. They created a big hoo-ha in the IT circles and some adopters are so keen that they implement it in so many different ways when at the end of the day what business users want is a software that run fast and accurately for their daily operations.