mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
添加设计模式的六大原则、修改单例模式
This commit is contained in:
parent
8c6aeb5760
commit
23970dc9ab
46
README.md
46
README.md
|
@ -1909,23 +1909,47 @@ ssize_t write(int fd, const void *buf, size_t count);
|
|||
|
||||
## 设计模式
|
||||
|
||||
### 设计模式的六大原则
|
||||
|
||||
* 单一职责原则(SRP,Single Responsibility Principle)
|
||||
* 里氏替换原则(LSP,Liskov Substitution Principle)
|
||||
* 依赖倒置原则(DIP,Dependence Inversion Principle)
|
||||
* 接口隔离原则(ISP,Interface Segregation Principle)
|
||||
* 迪米特法则(LoD,Law of Demeter)
|
||||
* 开放封闭原则(OCP,Open Close Principle)
|
||||
|
||||
### 单例模式
|
||||
|
||||
```cpp
|
||||
class CSingleton
|
||||
// 懒汉式单例模式
|
||||
class Singleton
|
||||
{
|
||||
private:
|
||||
CSingleton()
|
||||
{
|
||||
}
|
||||
static CSingleton *m_pInstance;
|
||||
Singleton() { }
|
||||
static Singleton * pInstance;
|
||||
public:
|
||||
static CSingleton * GetInstance()
|
||||
{
|
||||
if(m_pInstance == nullptr)
|
||||
m_pInstance = new CSingleton();
|
||||
return m_pInstance;
|
||||
}
|
||||
static Singleton * GetInstance()
|
||||
{
|
||||
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;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user