English 中文(简体)
如何在基类中声明工厂类方法?
原标题:
  • 时间:2009-06-06 07:37:53
  •  标签:

I m 寻求C++级设计问题的解决办法。 我想要实现的是,在基类中采用静态方法,从而回过来的就是后裔。 问题在于,其中一些应为单一州。 我在《维也纳条约法公约》中写道,有可能使用<代码>_properties,但我更喜欢纯C++解决办法。

class Base {
  private:
    static Base *Instance;
  public:
    static Base *New(void);
    virtual bool isSingleton(void) = 0;
}
Base::Instance = NULL;

class First : public Base { // singleton descendant
  public:
    bool isSingleton(void) { return true; }
}

class Second : public Base { // normal descendant
  public:
    bool isSingleton(void) { return false; }
}

Base *Base::New(void) {
  if (isSingleton())
    if (Instance != NULL)
      return Instance = new /* descendant constructor */;
    else
      return Instance;
  else
    return new /* descendant constructor */;
}

产生问题:

  • how to declare static variable Instance, so it would be static in descendant classes
  • how to call descendant constructors in base class

我认为,我计划如何解决这些问题可能是不可能的。 如果是的话,我要就如何以任何其他方式解决这一问题提出一些建议。


<>Edit:代码略有改动。 我错过了其中几个点。

最佳回答

只是为了检查我们有礼堂的定论——在我的书中,工厂班是一种可造成某些其他类别或班级的情况。 选择哪类企业是依据工厂获得的投入,或者至少是工厂可以检查的东西。 这里有一个非常简单的工厂:

class A { ~virtual A() {} };   
class B : public A {};
class C : public A {};

class AFactory {
   public:
      A * Make( char c ) {
         if ( c ==  B  ) {
             return new B;
         }
         else if ( c ==  C  ) { 
            return new C;
         }
         else {
           throw "bad type";
         }
      }
};

如果我是你,我会再次开始,铭记这一榜样和以下因素:

  • factorioes do not have to be singletons
  • factories do not have to be static members
  • factories do not have to be members of the base class for the hierarchies they create
  • factory methods normally return a dynamically created object
  • factory methods normally return a pointer
  • factory methods need a way of deciding which class to create an instance of

我看不出为什么你的工厂需要反思,而C++无论如何都没有得到有意义的支持。

问题回答

根据@Shakedown的答复,I ll make Base , 采用CRTP:

template <class T>
class Base
{
public:
 static std::auto_ptr<Base<T> > construct()
 {
    return new T();
 }
};

class First : public Base<First>
{
};

class Second : public Base<Second>
{
};

这是因为<代码> 施工<>/代码>现在再次成为固定成员。 请将此称为:

std::auto_ptr<First> first(First::construct());
std::auto_ptr<Second> second(Second::construct());

// do something with first and second...

你们可以创建单一州级和非单一州级,使所有后代都继承其中之一。

class Base {
  public:
    static Base *New() = 0;
}

class SingletonDescendant: public Base { 
  public:
    *Base::New() {
      if (Instance != NULL)
        return Instance = new /* descendant constructor */;
      else
        return Instance;
    }
  private:
    static Base *Instance;
}
SingletonDescendant::Instance = NULL;

class NonSingletonDescendant: public Base { 
  public:
    *Base::New() {
      return new;
    }
}

class First : public SingletonDescendant{ // singleton descendant
}

class Second : public NonSingletonDescendant{ // normal descendant
}

它解决了你提出的问题:

  • How to declare static variable Instance, so it would be static in descendant classes: It exists only in the SingletonDescendant class.
  • How to call descendant constructors in base class: Using the New function
  • I have to write construct() method in every descendant; I consider it redundant, as it is obvious what it has to do: Now it is only in SingletonDescendant and NonSingletonDescendant.

如何做这样的事情:

class Base
{
public:
 virtual Base construct() = 0;
};

class First : public Base
{
public:
 Base construct(){ return First(); // or whatever constructor }
};

class Second : public Base
{
public:
 Base construct(){ return Second(...); // constructor }
};




相关问题
热门标签