单例模式,智能指针实现

单例模式: 

//单例模式:
//加锁:
class Singleton
{
public:
    static Singleton* getIntance() 
    {
        if (pobject == NULL)//懒加载,只有在使用时才生成
        {
            pthread_mutex_lock(&mutex);//多线程线程安全问题
            if (pobject == NULL)//单线程时效率问题
            {
                pobject = new Singleton();
            }
            pthread_mutex_lock(&mutex);
        }
        return pobject;
    }
private:
    Singleton(){}
    static Singleton *pobject;
};
Singleton* Singleton::pobject = NULL; 
//懒汉模式
class Singleton {
    //懒汉式单例模式
    //比较懒,在类加载时,不创建实例,因此类加载速度快,但运行时获取对象的速度慢 
private :
	Singleton() {}
static Singleton* intance;
public:
	static Singleton* getInstance()  
    {
        if(intance == NULL)
        {
            intance = new Singleton();
        }
        return intance;
    }
};
Singleton* Singleton::intance = NULL;
//饿汉模式
class Singleton {
    //饿汉单例模式
    //在类加载时就完成了初始化,所以类加载较慢,但获取对象的速度快
private :
	Singleton() {}
static Singleton* intance;
public :
	static Singleton* getInstance()    //静态,不用同步(类加载时已初始化,不会有多线程的问题)
    {
        return instance;
    }
};
Singleton* Singleton::instance = new Singleton();

 

智能指针:

/*
		1.引用计数--
	*/
	void delRef(void* ptr)
	{
		cout << "delRef" << endl;
		if (ptr != NULL)
		{
			int index = find(ptr);
			if (index < 0)
			{
				throw exception("addr not exsit!");
			}
			else
			{
				if (node[index].refcount != 0)
				{
					node[index].refcount--;
				}
				cout << "	addr:  " << ptr << "  refcount:  " << node[index].refcount << endl;
			}
		}
	}
	/*
		1.addr  
			不存在  -1
			NULL   0
			存在   引用计数
	*/
	int getRef(void *ptr)
	{
		if (ptr == NULL)
		{
			return 0;
		}
		int index = find(ptr);
		if (index < 0)
		{
			return -1;
		}
		return node[index].refcount;
	}
private:
	int find(void* ptr)
	{
		for (int i = 0; i < length; ++i)
		{
			if (node[i].refaddr == ptr)
			{
				return i;
			}
		}
		return -1;
	}
	class Node
	{
	public:
		Node(void* p = NULL) :refaddr(p), refcount(0){}
		void* refaddr;
		int refcount;
	};
	Node node[10];
	int length;//记录当前数组的有效长度
	static RefManage rm;
};	
RefManage RefManage::rm;

template
class Shared_ptr
{
public:
	Shared_ptr(T* p = NULL)
	{
		_ptr = p;
		AddRef();
	}
	Shared_ptr(const Shared_ptr& rhs)
	{
		_ptr = rhs._ptr;
		AddRef();
	}
	Shared_ptr& operator=(const Shared_ptr& rhs)
	{
		if (this != &rhs)
		{
			DelRef();
			if (0 == GetRef())
			{
				delete _ptr;
			}
			_ptr = rhs._ptr;
			AddRef();
		}
		return *this;
	}
	T* GetPtr()const
	{
		return _ptr;
	}
	~Shared_ptr()
	{
		DelRef();
		if (0 == GetRef())
		{
			delete _ptr;
		}
	}
	T& operator*()
	{
		return *_ptr;
	}
	T* operator->()
	{
		return _ptr;
	}
private:
	void AddRef()
	{
		rm.addRef(_ptr);
	}
	void DelRef()
	{
		rm.delRef(_ptr);
	}
	int GetRef()
	{
		return rm.getRef(_ptr);
	}
	T* _ptr;
	static RefManage &rm;
};
template
RefManage& Shared_ptr::rm = RefManage::getIntance();

class Test
{
public:
	Test(int a = 0) :ma(a)
	{
		cout << "Test::Test()" << endl;
	}
	~Test()
	{
		cout << "Test::~Test()" << endl;
	}
private:
	int ma;
};
template
class Weak_ptr
{
public:
	Weak_ptr(T* p = NULL)
	{
		_ptr = p;
	}
	Weak_ptr(const Shared_ptr& rhs)
	{
		_ptr = rhs.GetPtr();
	}
	Weak_ptr& operator=(const Shared_ptr& rhs)
	{
		_ptr = rhs.GetPtr();
		return *this;
	}
	~Weak_ptr()
	{
		_ptr = NULL;
	}
	
private:
	T* _ptr;
};
class B;
class A
{
public:
	A()
	{
		cout << "A::A()" << endl;
	}
	~A()
	{
		cout << "A::~A()" << endl;
	}
	Weak_ptr pa;
};
/*
	A::
	  pa::
		_ptr = NULL
	B::
	  pb::
		_ptr = NULL
*/
class B
{
public:
	B()
	{
		cout << "B::B()" << endl;
	}
	~B()
	{
		cout << "B::~B()" << endl;
	}
	Weak_ptr pb;
};
int main()
{
	Shared_ptr spa(new A());
	Shared_ptr spb(new B());
	spa->pa = spb;
	spb->pb = spa;
	return 0;
}

 

你可能感兴趣的:(C++)