文章目录
记录一下常见的设计模式的实现方法(Cpp实现)。
当只允许类创建一个实例的时候,可以使用单例模式。
线程不安全
#include <iostream>
using namespace std;
class Singleton{
public:
static Singleton * getInstance(){
if(pinstance==nullptr){
pinstance=new Singleton;
}
return pinstance;
}
private:
Singleton(){}
~Singleton(){}
Singleton(const Singleton &s);
const Singleton &operator=(const Singleton &s);
private:
static Singleton *pinstance;
};
Singleton * Singleton::pinstance=nullptr;
int main(){
Singleton *s1=Singleton::getInstance();
Singleton *s2=Singleton::getInstance();
if(s1==s2){
cout<<"s1==s2"<<endl;
}
return 0;
}
线程安全
#include <iostream>
#include <mutex>
using namespace std;
class Singleton{
public:
static Singleton * getInstance(){
if(pinstance==nullptr){
std::lock_guard<std::mutex> lock(mutex1);
if(pinstance==nullptr){
pinstance=new Singleton;
}
}
return pinstance;
}
private:
Singleton(){}
~Singleton(){}
Singleton(const Singleton &s);
const Singleton &operator=(const Singleton &s);
private:
static Singleton *pinstance;
static std::mutex mutex1;
};
Singleton * Singleton::pinstance=nullptr;
std::mutex Singleton::mutex1;
int main(){
Singleton *s1=Singleton::getInstance();
Singleton *s2=Singleton::getInstance();
if(s1==s2){
cout<<"s1==s2"<<endl;
}
return 0;
}
effective c++中的实现、
饿汉式是程序运行的时候马上就创建。
//本身就线程安全
#include <iostream>
using namespace std;
class Singleton{
public:
static Singleton * getInstance(){
return pinstance;
}
private:
Singleton(){}
~Singleton(){}
Singleton(const Singleton &s);
const Singleton &operator=(const Singleton &s);
private:
static Singleton *pinstance;
};
Singleton * Singleton::pinstance=new (std::nothrow)Singleton;
int main(){
Singleton *s1=Singleton::getInstance();
Singleton *s2=Singleton::getInstance();
if(s1==s2){
cout<<"s1==s2"<<endl;
}
return 0;
}
观察者模式又叫发布订阅模式,在被观察者发生改变时,多个观察者应当能发现其变化。
#include <bits/stdc++.h>
using namespace std;
class Observer;
//被观察者
class Subject{
public:
Subject(int a):state(a){}
void addObserver(Observer *obs){
observers.push_back(obs);
}
void delObserver(Observer *obs){
observers.remove(obs);
}
int getState(){
return state;
}
void setState(int _state){
state=_state;
}
void notifyAll();
private:
int state;
list<Observer*> observers;
};
//观察者-虚基类
class Observer{
public:
Observer(Subject *sub):_subject(sub){}
virtual void update()=0;
protected:
Subject *_subject;
};
//观察者1
class Observer1:public Observer{
public:
Observer1(Subject *sub):Observer(sub){}
void update() override{
cout<<"Observer1: "<<_subject->getState()<<endl;
}
};
//观察者2
class Observer2:public Observer{
public:
Observer2(Subject *sub):Observer(sub){}
void update() override{
cout<<"Observer2: "<<_subject->getState()<<endl;
}
};
/* 注意Observer类进行了前向声明,但是不能访问其类内成员
* 在Oberver类的定义之后,a才能访问其update()方法.
*/
void Subject::notifyAll(){
for(auto a:observers){
a->update();
}
}
int main(){
Subject *sub1=new Subject(10);
Observer1 *obs1=new Observer1(sub1);
Observer2 *obs2=new Observer2(sub1);
sub1->addObserver(obs1);
sub1->addObserver(obs2);
cout<<"init:state="<<sub1->getState()<<endl;
cout<<"set:state=20"<<endl;
sub1->setState(20);
sub1->notifyAll();
delete sub1;
delete obs1;
delete obs2;
return 0;
}