さらに別の工場

現在のプロジェクトでは、何らかの種類の識別子を使用してさまざまなオブジェクトのセットを構築する必要がしばしば生じています。 あるセット用に作成されたファクトリと、別のファクトリ用のファクトリがあります。 それから、同じことをしているので、何らかの反復可能な解決策が必要であるという理解に至りました。

このプロジェクトはQtに基づいています。Qtは、ご存じのとおり、メタデータを操作するメカニズムを開発しました。 それにもかかわらず、QMetaObjectを介してオブジェクトを構築することは、2つの理由で満足できませんでした.1つ目は、構築されたオブジェクトがQObjectsである必要があり、2つ目は、構築時にQObjectへのポインターを取得します。



タスクの範囲を分析した結果、いくつかの相続人の基本クラスに静的ファクトリーが必要だという結論に達しました。 つまり このようなものを書いてください:



BaseClass * instance = BaseClass::factory()->build("derived_name");
      
      







同時に、毎回多くの統一されたサービスコードを記述したくありません。 はい、私たちは怠け者です。

そしてもちろん、私たちは工場や基本クラスにすべての相続人を知ってほしくありません。



少し考えて、私たちは幅広いタスクを解決する工場を思いつきました。



工場


最初に、コンパクトであることが判明したため、ファクトリコードを提供します。

 template<class Base> class UnifiedFactory { public: UnifiedFactory(){} ~UnifiedFactory(){qDeleteAll(m_builders);} template<class T> void registerClass(const QString& name) { delete m_builders.value(name); m_builders.insert(name, new Builder<T>()); } Base * build(const QString& name) const { BaseBuilder * builder = m_builders.value(name); if(builder) return builder->build(); return 0; } private: class BaseBuilder { public: virtual Base * build() const = 0; }; template<class T> class Builder : public BaseBuilder { public: virtual Base * build() const { return new T(); } }; typedef QHash<QString, BaseBuilder*> Builders; Builders m_builders; };
      
      







ご覧のとおり、このテンプレートファクトリは、共通の基本クラスを持つオブジェクトのみを構築できます.

, template registerClass(const QString& name), T - .

, -, BaseBuilder build() (!) Builder, .

- . , . , , .

.



, - .











#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)








, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,









, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")










, , . - . , .

, Qt- , STL- .

, . , .



PS: ?












.

, template registerClass(const QString& name), T - .

, -, BaseBuilder




build() (!) Builder, .

- . , . , , .

.



, - .











#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)








, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,









, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")










, , . - . , .

, Qt- , STL- .

, . , .



PS: ?








 . 
      

, template registerClass(const QString& name), T - .

, -, BaseBuilder




build() (!) Builder, .

- . , . , , .

.



, - .







#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)



, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,









, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")










, , . - . , .

, Qt- , STL- .

, . , .



PS: ?
















.

, template registerClass(const QString& name), T - .

, -, BaseBuilder




build() (!) Builder, .

- . , . , , .

.



, - .











#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)



, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,









, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")










, , . - . , .

, Qt- , STL- .

, . , .



PS: ?












.

, template registerClass(const QString& name), T - .

, -, BaseBuilder




build() (!) Builder, .

- . , . , , .

.



, - .











#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)








, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,









, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")










, , . - . , .

, Qt- , STL- .

, . , .



PS: ?








.

, template registerClass(const QString& name), T - .

, -, BaseBuilder




build() (!) Builder, .

- . , . , , .

.



, - .











#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)








, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,









, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")










, , . - . , .

, Qt- , STL- .

, . , .



PS: ?








 . 
      

, template registerClass(const QString& name), T - .

, -, BaseBuilder




build() (!) Builder, .

- . , . , , .

.



, - .











#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)








, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,





, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")





, , . - . , .

, Qt- , STL- .

, . , .



PS: ?
















.

, template registerClass(const QString& name), T - .

, -, BaseBuilder




build() (!) Builder, .

- . , . , , .

.



, - .











#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)








, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,









, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")





, , . - . , .

, Qt- , STL- .

, . , .



PS: ?












.

, template registerClass(const QString& name), T - .

, -, BaseBuilder




build() (!) Builder, .

- . , . , , .

.



, - .











#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)








, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,









, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")










, , . - . , .

, Qt- , STL- .

, . , .



PS: ?








.

, template registerClass(const QString& name), T - .

, -, BaseBuilder




build() (!) Builder, .

- . , . , , .

.



, - .











#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)








, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,









, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")










, , . - . , .

, Qt- , STL- .

, . , .



PS: ?








.

, template registerClass(const QString& name), T - .

, -, BaseBuilder




build() (!) Builder, .

- . , . , , .

.



, - .











#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)








, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,









, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")










, , . - . , .

, Qt- , STL- .

, . , .



PS: ?








.

, template registerClass(const QString& name), T - .

, -, BaseBuilder




build() (!) Builder, .

- . , . , , .

.



, - .











#define UNIFIED_FACTORY(BaseType) \ static UnifiedFactory<BaseType>* factory() \ { \ static UnifiedFactory<BaseType> s_factory; \ return &s_factory; \ } \ template<class T> \ class AutoRegistrer \ { \ public: \ AutoRegistrer(const QString& name){factory()->registerClass<T>(name);} \ }; \ #define UF_REGISTER_DERIVED_NAMED(type, name) \ static const type::AutoRegistrer<type> type##Registrator(name); #define UF_REGISTER_DERIVED(type) UF_REGISTER_DERIVED_NAMED(type, #type)








, - template class AutoRegistrer, - - .



UF_REGISTER_DERIVED_NAMED , AutoRegistrer.

,









, :

class Base { publi: UNIFIED_FACTORY(Base) }; class Derived1 : public Base {...} UF_REGISTER_DERIVED(Derived1) class Derived2 : public Base {...} UF_REGISTER_DERIVED(Derived2) .... Base * instance = Base::factory()->build("Derived1")










, , . - . , .

, Qt- , STL- .

, . , .



PS: ?











All Articles