From 23970dc9aba5c99cbc4b1b7f94675b1a0453ce78 Mon Sep 17 00:00:00 2001 From: huihut Date: Wed, 11 Apr 2018 00:09:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E5=85=AD=E5=A4=A7=E5=8E=9F=E5=88=99=E3=80=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8D=95=E4=BE=8B=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ef20032..377c159 100644 --- a/README.md +++ b/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; + } }; ```