一个单例类可以被继承吗? 如果可以, 那么如何实现?
**编辑:***我是想说,如果我们有一个使用单例设计模式的类,它可以被继承吗?*
一个单例类可以被继承吗? 如果可以, 那么如何实现?
**编辑:***我是想说,如果我们有一个使用单例设计模式的类,它可以被继承吗?*
单例模式有私有构造函数,因此不能继承。此外,单例模式具有静态方法来实例化私有实例成员,由于无法覆盖静态方法,因此从单例继承将毫无意义。
这取决于你对设计模式的实现方式。最简单的形式是创建一个像这样的类:
class MySingleton
{
public:
static MySingleton &getInstance()
{
static MySingleton instance;
return instance;
}
private:
MySingleton();
~MySingleton();
};
单例模式类是为了被继承而设计的。如果没有继承,单例模式就没有什么价值。
instance()
成员函数。instance()
函数,在运行时决定实例化和返回哪个类。我有一个单例类,我在许多实例中继承它。
以下是该单例类:
template <class Target>
class Singleton_Shared_Ptr
{
//---------------------------------------------------------------------
// Public Constructors & Destructors
//---------------------------------------------------------------------
public:
//! Destructor.
virtual ~Singleton_Shared_Ptr();
//---------------------------------------------------------------------
// Public methods
//---------------------------------------------------------------------
public:
//! Returns a pointer to the instance.
static boost::shared_ptr<Target> ptr(void);
//! Returns a reference to the instance.
static Target & ref(void);
//---------------------------------------------------------------------
// Protected methods
//---------------------------------------------------------------------
protected:
//! Default constructor.
Singleton_Shared_Ptr();
//---------------------------------------------------------------------
// Private methods
//---------------------------------------------------------------------
private:
//! Copy constructor, not implemented.
/*! The copy constructor is declared so that the compiler will not
* automatically generate one.
*/
Singleton_Shared_Ptr(const Singleton_Shared_Ptr& s);
//! Assignment operator, declared but not defined.
/*! The assignment operator is declared so that the compiler will not
* automatically generate one.
*/
Singleton_Shared_Ptr& operator=(const Singleton_Shared_Ptr& s);
//---------------------------------------------------------------------
// Private members
//---------------------------------------------------------------------
private:
static wxMutex m_instance_mutex;
};
template<class Target>
wxMutex Singleton_Shared_Ptr<Target>::m_instance_mutex;
//-------------------------------------------------------------------------
// Singleton_Shared_Ptr Constructors & Destructors
//-------------------------------------------------------------------------
template <class Target>
inline
Singleton_Shared_Ptr<Target> ::
Singleton_Shared_Ptr()
{
}
template <class Target>
inline
Singleton_Shared_Ptr<Target> ::
~Singleton_Shared_Ptr()
{
}
//-------------------------------------------------------------------------
// Singleton_Shared_Ptr methods in alphabetical order
//-------------------------------------------------------------------------
template <class Target>
boost::shared_ptr<Target>
Singleton_Shared_Ptr<Target> ::
ptr(void)
{
static boost::shared_ptr<Target> p_instance;
if (p_instance.get() == NULL)
{
wxMutexLocker lock(m_instance_mutex);
if (!p_instance)
{
p_instance.reset(new Target);
}
}
return p_instance;
}
template <class Target>
Target &
Singleton_Shared_Ptr<Target> ::
ref(void)
{
return *(ptr());
}
这里是单例模式的用法:
class Manager
: public Singleton_Shared_Ptr<Manager>
{
//---------------------------------------------------------------------
// Friends
//---------------------------------------------------------------------
friend class Common::Singleton_Shared_Ptr<Manager>;
//---------------------------------------------------------------------
// Public Constructors and Destructors
//---------------------------------------------------------------------
public:
//! destructor
virtual ~Manager();
//---------------------------------------------------------------------
// Protected Methods
//---------------------------------------------------------------------
protected:
//! Constructor
Manager();
//! Copy constructor -- declared but not implemented.
Manager(const Manager& m);
//! Assignment operator -- declared but not implemented.
Manager& operator= (const Manager& m);
};