W
werasm
Hi all,
I have had the following problem in the past and am interested in
what
various (other) solutions exist to solve this:
- Code requiring services dictate an interface to the services
required.
- It only requires one instance to provide those services.
- Services are only realized/bound at runtime.
A typical example of this is an AbstractFactory:
A library requires the services of the factory to build the range of
products
that it requires. How is concrete instance of this factory realized?
Conservatively we in the past used this mechanism:
class Factory
{
public:
static void setConcreteFactory( Factory& impl );
static Factory* get(); //or instance(), if you like
virtual std::auto_ptr<X> makeX() const = 0;
//etc...
protected:
~Factory();
private:
static Factory* impl_;
};
Looking at this, I realized it is not far from this:
//NOTE!!! Only one instance of implementation exist. IsA Singleton
interface!
class Factory
{
public:
static Factory* get(); //or instance(), if you like
virtual std::auto_ptr<X> makeX() const = 0;
//etc...
protected:
Factory( Factory& impl );
~Factory();
private:
static Factory* interface_;
};
Where get and the constructor would typically be implemented like
this:
Factory* Factory::get()
{
assert( interface_ ); //Could use less crude mechanism...
return interface_;
}
//We only want one instance
Factory::Factory( Factory & impl )
{
assert( interface_ == 0 ); //Only one instance
interface_ = &impl;
}
Factory::~Factory(){ interface_ = 0; }
This (lets call it) pattern, is easy to generalize like this:
template <class T>
class SingleInterface
{
public:
static T* get();
protected:
SingleInterface( T& impl );
~SingleInterface();
private:
static SingleInterface* interface_;
};
class Factory : public SingleInterface<Factory>
{
public:
virtual std::auto_ptr<X> makeX() const = 0;
protected:
Factory( Factory& ); //Base encapsulate rules and indicates
intent?
~Factory();
};
What other solutions do you have to this, and what is your critic?
Regards,
Werner
I have had the following problem in the past and am interested in
what
various (other) solutions exist to solve this:
- Code requiring services dictate an interface to the services
required.
- It only requires one instance to provide those services.
- Services are only realized/bound at runtime.
A typical example of this is an AbstractFactory:
A library requires the services of the factory to build the range of
products
that it requires. How is concrete instance of this factory realized?
Conservatively we in the past used this mechanism:
class Factory
{
public:
static void setConcreteFactory( Factory& impl );
static Factory* get(); //or instance(), if you like
virtual std::auto_ptr<X> makeX() const = 0;
//etc...
protected:
~Factory();
private:
static Factory* impl_;
};
Looking at this, I realized it is not far from this:
//NOTE!!! Only one instance of implementation exist. IsA Singleton
interface!
class Factory
{
public:
static Factory* get(); //or instance(), if you like
virtual std::auto_ptr<X> makeX() const = 0;
//etc...
protected:
Factory( Factory& impl );
~Factory();
private:
static Factory* interface_;
};
Where get and the constructor would typically be implemented like
this:
Factory* Factory::get()
{
assert( interface_ ); //Could use less crude mechanism...
return interface_;
}
//We only want one instance
Factory::Factory( Factory & impl )
{
assert( interface_ == 0 ); //Only one instance
interface_ = &impl;
}
Factory::~Factory(){ interface_ = 0; }
This (lets call it) pattern, is easy to generalize like this:
template <class T>
class SingleInterface
{
public:
static T* get();
protected:
SingleInterface( T& impl );
~SingleInterface();
private:
static SingleInterface* interface_;
};
class Factory : public SingleInterface<Factory>
{
public:
virtual std::auto_ptr<X> makeX() const = 0;
protected:
Factory( Factory& ); //Base encapsulate rules and indicates
intent?
~Factory();
};
What other solutions do you have to this, and what is your critic?
Regards,
Werner