Question regarding factory template

I have a factory class to create objects of base class B. The object (D) that this factory uses gets a list of strings representing the actual types. What is the correct implementation:

  • factory gets Enum (and uses the switch inside the Create function), and D is responsible for converting the string to Enum.
  • factory gets the string and checks if it matches the set of valid strings (using ifs)
  • Another implementation that I did not think about.
+3
source share
5 answers

enum . btw. .. - D, factory.

D , , , D factory. (Btw factory , ).

: ( , D factory)? , , , (.. - ++ ) , ). ( IDE , , - ):

// Function type returning a pointer to B
typedef (B*)(*func)() StaticConstructor;

// Function creating instances of subclass E
B* createSubclassE() {
    return new E(...);
}

// Function creating instances of subclass F
B* createSubclassF() {
    return new F(...);
}

// Mapping from strings to constructor methods creating specific subclasses of B
map<string, StaticConstructor> factoryMap;
factoryMap["E"] = &createSubclassE;
factoryMap["F"] = &createSubclassF;

, - , , auto_ptr. , , . , ...

+1

, , ifs/switches.

0

VC++/Qt XML , , Enum.

, Enum QString < > Enum:

enum DataColumnTypeEnum
{
    DataColumnTypeNotSet,
    ColumnBinary,
    ColumnBoolean,
    ColumnDate,
    ColumnDateTime,
    ColumnNumber,
    ColumnFloat,
    ColumnPrimary,
    ColumnString,
    ColumnText,
};

class DataColumnType
{
public:
    DataColumnType();
    DataColumnType(DataColumnTypeEnum);
    DataColumnType(const QString&);

    DataColumnType& operator = (DataColumnTypeEnum);
    DataColumnType& operator = (const QString&);

    operator DataColumnTypeEnum() const;
    operator QString() const;
private:
    DataColumnTypeEnum type;
};

DataColumnType& DataColumnType::operator = (const QString& str)
{
    str.toLower();
    if(str.isEmpty()) type = DataColumnTypeNotSet;
    else if(str == "binary") type = ColumnBinary;
    else if(str == "bool") type = ColumnBoolean;
    else if(str == "date") type = ColumnDate;
    else if(str == "datetime") type = ColumnDateTime;
    else if(str == "number") type = ColumnNumber;
    else if(str == "float") type = ColumnFloat;
    else if(str == "primary") type = ColumnPrimary;
    else if(str == "string") type = ColumnString;
    else if(str == "text") type = ColumnText;
    return *this;
}

.

- .

0

, factory . , B, factory . . factory - . ..... .

0

I personally use extended enumeration because I have always found that the C ++ enumeration is missing: messages like are Type 3 - method -beginnot very informative.

For this, I use a simple template class:

template <class Holder>
class Enum
{
public:
  typedef typename Holder::type enum_type;

  Enum(): mValue(Invalid()) {}
  Enum(enum_type i): mValue(Get(i)) {}
  explicit Enum(const std::string& s): mValue(Get(s)) {}

  bool isValid() const { return mValue != Invalid(); }
  enum_type getValue() const { return mValue->first; }

private:
  typedef typename Holder::mapping_type mapping_type;
  typedef typename mapping_type::const_iterator iterator;
  static const mapping_type& Mapping() { static mapping_type MMap = Holder::Initialize(); return MMap; }

  static iterator Invalid() { return Mapping().end(); }
  static iterator Get(enum_type i) { // search }
  static iterator Get(const std::string& s) { // search }

  iterator mValue;
};

You define Holderas follows:

struct Example
{
  typedef enum {
    Value1,
    Value2,
    Value3
  } type;

  typedef std::vector< std::pair< type, std::string > > mapping_type;

  static mapping_type Initialize() {
    return builder<mapping_type>()(Value1,"Value1")(Value2,"Value2")(Value3,"Value3");
  }
};

You can define a macro for it:

DEFINE_ENUM(Example, (Value1)(Value2)(Value3))

But I allowed the implementation as an exercise ( Boost.Preprocessor- your friend).

The most interesting thing is to use it!

int main(int argc, char* argv[])
{
  std::string s;
  std::cin >> s;
  Enum<Example> e(s);

  switch(e.getValue())
  {
  case Example::Value1:
  case Example::Value2:
    ++e;
  case Example::Value3:
    std::cout << e << std::endl;
  default:
  }
}
0
source

Source: https://habr.com/ru/post/1736782/


All Articles