Dec 24, 2008

Abstract factory design pattern case

Abstract factory design pattern case
An example of this would be an abstract factory class DocumentCreator that provides interfaces to create a number of products (eg. createLetter() and createResume()). The system would have any number of derived concrete versions of the DocumentCreator class like FancyDocumentCreator or ModernDocumentCreator, each with a different implementation of createLetter() and createResume() that would create a corresponding object like FancyLetter or ModernResume. Each of these products is derived from a simple abstract class like Letter or Resume of which the client is aware. The client code would get an appropriate instantiation of the DocumentCreator and call its factory methods. Each of the resulting objects would be created from the same DocumentCreator implementation and would share a common theme (they would all be fancy or modern objects). The client would need to know how to handle only the abstract Letter or Resume class, not the specific version that it got from the concrete factory.
Resource :
http://en.wikipedia.org/wiki/Abstract_factory_pattern


class DocumentCreater
{
virtual Letter* createL()=0;
virtual Resume* createR()=0;
virtual ~DocumentCreater();
};

class FancyDocumentCreater: public DocumentCreater
{
virtual FancyLetter* createL();
virtual FancyResume* createR();
};

class MordenDcumentCreater: public DocumentCreater
{
virtual MordenLetter* createL();
virtual MordenResume* createR();
};

class Letter()
{
irtual
};

class Resume()
{
};

class FancyLetter(): public Letter
{
virtual ~Letter();
};

class FancyResume(): public Resume
{
virtual ~Resume();
};

class MordenLetter(): public Letter
{
};

class MordenResume():public Resume
{
};

class AppL//the application to create letter. which type we create based on which factory passed in
{
Letter* AppL(DocumentCreater* factory)
{
return factory->createL();
}
};

class AppR//the application to create resume. which type we create based on which factory passed in
{
Resume* AppR(DocumentCreater* factory)
{
return factory->createR();
}
};


void main(void)
{
DocumentCreater* factory1= new FancyDocumentCreater();
DocumentCreater* factory2= new MordenDocumentCreater();

Letter* L=new AppL(factory1);
Resume* R=new AppR(factory2);


delete factory1, factory2, L, R;

}

No comments:

Post a Comment