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

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

View File

@ -1909,22 +1909,46 @@ 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
class CSingleton
// 懒汉式单例模式
class Singleton
{
private:
CSingleton()
{
}
static CSingleton *m_pInstance;
Singleton() { }
static Singleton * pInstance;
public:
static CSingleton * GetInstance()
static Singleton * GetInstance()
{
if(m_pInstance == nullptr)
m_pInstance = new CSingleton();
return m_pInstance;
if (pInstance == nullptr)
pInstance = new Singleton();
return pInstance;
}
};
// 线程安全的单例模式
class Singleton
{
private:
Singleton() { }
~Singleton() { }
Singleton(const Singleton &);
Singleton & operator = (const Singleton &);
public:
static Singleton & GetInstance()
{
static Singleton instance;
return instance;
}
};
```