Abstract Factory Design Pattern
Indent
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
A hierarchy that encasuplates: many possible “plateforms”, and the construction of a suite of “products”
The new operator considerd harmful
#include <iostream>
class Shape
{
public:
Shape() { _id = _total++; }
virtual void draw()=0;
protected:
int _id;
static int _total;
};
int Shape::_total = 0;
class Circle : public Shape { public: void draw() { std::cout << "Circle" << _id << std::endl; }};
class Square : public Shape { public: void draw() { std::cout << "Square" << _id << std::endl; }};
class Eclipse: public Shape { public: void draw() { std::cout << "Eclipse"<< _id << std::endl; }};
class Factory
{
public:
virtual Shape* createCurvedInstance()=0;
virtual Shape* createStraightInstance()=0;
};
class SimpleShapeFactory : public Factory
{
public:
Shape* createCurvedInstance() { return new Circle(); }
Shape* createStraightInstance() { return new Square(); }
};
class RobustShapeFactory : public Factory
{
public:
Shape* createCurvedInstance() { return new Eclipse(); }
Shape* createStraightInstance() { return new Square(); }
};
int main()
{
Factory* factory = new SimpleShapeFactory();
Shape* shapes[3];
shapes[0] = factory->createCurvedInstance();
shapes[1] = factory->createStraightInstance();
shapes[2] = factory->createStraightInstance();
for(int i = 0; i < 3; ++i)
{
shapes[i]->draw();
}
return 0;
}
Factory Method
Intent
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Meathods lets a class defer instantiation to subclasses.
Define a “virtual” constructor.
The new operator considered harmful
Discussion
Factorty Metod is to create objects as Template methods.
#include <iostream>
#include <cstring>
/* Abstratct base class declared by framework */
class Document
{
public:
Document(char const*fn)
{
strcpy(name, fn);
}
virtual void Open() = 0;
virtual void Close() = 0;
char * GetName() { return name; }
private:
char name[20];
};
/* Concrete derived class defined by client */
class MyDocument: public Document
{
public:
MyDocument(char const *fn): Document(fn) {}
void Open() { std::cout << " MyDocument: Open()" << std::endl; }
void Close() { std::cout << " MyDocument: Close()" << std::endl; }
};
class Application
{
public:
Application():_index(0) { std::cout << "Application: ctor" << std::endl; }
void NewDocument(char const* name)
{
std::cout<< "Application: NewDocument()" << std::endl;
/* Framework calls the 'hole' reserved for client custmization */
_docs[_index] = CreateDocument(name);
_docs[_index++]->Open();
}
void OpenDocument();
void ReportDocs() { std::cout<< _index + 1 << std::endl; };
/* Framework uses Document's base class */
virtual Document *CreateDocument(char const*)=0;
private:
int _index;
Document *_docs[10];
};
class MyApplication: public Application
{
public:
MyApplication() { std::cout << "MyApplication: ctor" << std::endl; }
Document *CreateDocument(char const* fn)
{
std::cout << " MyApplication: CreateDocument()" << std::endl;
return new MyDocument(fn);
}
};
int main()
{
MyApplication myApp;
myApp.NewDocument("foo");
myApp.NewDocument("bar");
myApp.ReportDocs();
return 0;
}
把这个代码写一遍就知道factory method 和abstract factory的区别和联系了