factory

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的区别和联系了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值