添加设计模式的六大原则、修改单例模式

This commit is contained in:
huihut 2018-04-11 00:09:38 +08:00
parent 8c6aeb5760
commit 23970dc9ab

View File

@ -1909,23 +1909,47 @@ ssize_t write(int fd, const void *buf, size_t count);
## 设计模式 ## 设计模式
### 设计模式的六大原则
* 单一职责原则SRPSingle Responsibility Principle
* 里氏替换原则LSPLiskov Substitution Principle
* 依赖倒置原则DIPDependence Inversion Principle
* 接口隔离原则ISPInterface Segregation Principle
* 迪米特法则LoDLaw of Demeter
* 开放封闭原则OCPOpen Close Principle
### 单例模式 ### 单例模式
```cpp ```cpp
class CSingleton // 懒汉式单例模式
class Singleton
{ {
private: private:
CSingleton() Singleton() { }
{ static Singleton * pInstance;
}
static CSingleton *m_pInstance;
public: public:
static CSingleton * GetInstance() static Singleton * GetInstance()
{ {
if(m_pInstance == nullptr) if (pInstance == nullptr)
m_pInstance = new CSingleton(); pInstance = new Singleton();
return m_pInstance; return pInstance;
} }
};
// 线程安全的单例模式
class Singleton
{
private:
Singleton() { }
~Singleton() { }
Singleton(const Singleton &);
Singleton & operator = (const Singleton &);
public:
static Singleton & GetInstance()
{
static Singleton instance;
return instance;
}
}; };
``` ```