单例模式
顾名思义就是只有一个的对象,分为懒汉模式,和饿汉模式
懒汉模式即为在需要的时候才会实例化,而饿汉一旦定义了类就会实例化,饿汉天生就是线程安全的
单例模式的优点:
1 在内存中只有一个对象,节省内存
2 避免对对象的频繁的申请的释放,提高性能
3 可以全局访问
什么时候选择饿汉还是懒汉呢:
1 当线程的访问量较多时,因为要进程同步,所以采用饿汉,可以提高性能
2 当线程的访问量较少时,可以选择懒汉式
懒汉式实现(线程安全)
如果用new的方式的话,那么要考虑释放掉
当是如果直接在外面delete的话容易导致野指针的出现,我们可以用析构函数实现,即声明一个嵌套类,
在嵌套类里面释放,并且声明一个嵌套类的静态对象
#include<iostream>
#include<pthread.h>
using namespace std;
class singleton
{
private:
singleton(){
};
class B
{
public:
~B()
{
if(singleton::p!=nullptr)
{
cout<<"fsfsf"<<endl;
delete singleton::p;
singleton::p = nullptr;
}
}
};
static pthread_mutex_t mutex;
static singleton* p;
static B b;
public:
static singleton* get()
{
if(p==nullptr)
{
pthread_mutex_lock(&mutex);
if(p==nullptr)
{
p = new singleton();
}
pthread_mutex_unlock(&mutex);
}
}
};
pthread_mutex_t singleton::mutex = PTHREAD_MUTEX_INITIALIZER;
singleton* singleton::p = nullptr;
singleton::B singleton::b;
int main()
{
singleton *a = singleton::get();
}
一定要记得static变量的外部声明也是返回类型在前面的
如果使用局部static对象的话,就不用考虑释放了
并且在c++11中 static是线程安全的
所以不需要考虑加锁的问题
#include<iostream>
#include<pthread.h>
using namespace std;
class singleton
{
private:
singleton(){
}
static singleton* p;
static B b;
public:
static singleton* get()
{
static singleton p;
return &p;
}
}
int main()
{
singleton *a = singleton::get();
}
饿汉式天生就是线程安全的,因为static对象在程序开始是,进入main函数之前就已经初始化好了
#include<iostream>
#include<pthread.h>
using namespace std;
class singleton
{
private:
singleton(){
};
static pthread_mutex_t mutex;
static singleton* p;
public:
static singleton* get()
{
return p;
}
};
singleton* singleton::p = new singleton();
int main()
{
singleton *a = singleton::get();
}
工厂模式:
简单工厂模式:
就是用一个工厂根据用户指定的参数生产指定的产品
产品会继承一个虚基类,然后工厂返回这个虚基类指针,利用了虚函数的多态
代码:
#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
#include<iostream>
using namespace std;
class product
{
public:
virtual void show() = 0;
};
class productA:public product
{
public:
void show()
{
cout<<"product A"<<endl;
}
};
class productB:public product
{
public:
void show()
{
cout<<"product B"<<endl;
}
};
class fac
{
public:
product* create(int va)
{
switch(va)
{
case 1:
return new productA;
break;
case 2:
return new productB;
break;
default:
return NULL;
};
}
};
int main()
{
fac f;
f.create(1)->show();
f.create(2)->show();
}
工厂方法模式:
简单来说就是多个工厂,多个商品,工厂继承同一个虚基类,商品继承同一个虚基类,
由于简单工厂模式,每增加一个商品就要修改工厂代码
所以出现了工厂方法模式
抽象工厂模式:
多个工厂,多个产品,每个产品有不同的型号,不同的型号继承不同的虚基类
#include <iostream>
using namespace std;
//定义抽象类
class product1
{
public:
virtual void show() = 0;
};
//定义具体类
class product_A1 :public product1
{
public:
void show(){ cout << "product A1" << endl; }
};
class product_B1 :public product1
{
public:
void show(){ cout << "product B1" << endl; }
};
//定义抽象类
class product2
{
public:
virtual void show() = 0;
};
//定义具体类
class product_A2 :public product2
{
public:
void show(){ cout << "product A2" << endl; }
};
class product_B2 :public product2
{
public:
void show(){ cout << "product B2" << endl; }
};
class Factory
{
public:
virtual product1 *creat1() = 0;
virtual product2 *creat2() = 0;
};
class FactoryA
{
public:
product1 *creat1(){ return new product_A1(); }
product2 *creat2(){ return new product_A2(); }
};
class FactoryB
{
public:
product1 *creat1(){ return new product_B1(); }
product2 *creat2(){ return new product_B2(); }
};
int main()
{
FactoryA *factoryA = new FactoryA();
factoryA->creat1()->show();
factoryA->creat2()->show();
FactoryB *factoryB = new FactoryB();
factoryB->creat1()->show();
factoryB->creat2()->show();
return 0;