设计模式
单例模式
一个类只能创建一个对象 ---- 单例模式的核心资源控制
2、实现单例的步骤
(1)、构造函数私有化并且删除拷贝构造 //内外不能实例化
(2)、提供一个全局的静态方法 //不需要对象也可以调用
(3)、在类中定义一个静态指针,指向本类的变量的静态指针变量!!!
两种模式一个编码复杂资源利用效率高 一个编码简单资源利用效率低
class SingleInstance {
public:
static SingleInstance *getInstance() {
if (s == nullptr) { //加上这个效率提升
unique_lock<mutex> lock(Instance_mutex); //线程安全
if (s == nullptr) { //懒汉模式 遇上多线程
s = new SingleInstance();
}
}
return s;
}
private:
SingleInstance() {}
SingleInstance(const SingleInstance &) = delete;
static mutex Instance_mutex;
static SingleInstance *s;
};
SingleInstance *SingleInstance::s = nullptr;
mutex SingleInstance::Instance_mutex;
/***********************/
class SingleInstance {
public:
static SingleInstance *getInstance() {
if (s == nullptr) {
s = new SingleInstance();
}
return s;
}
private:
SingleInstance() {}
SingleInstance(const SingleInstance &) = delete;
static mutex Instance_mutex;
static SingleInstance *s;
};
SingleInstance *SingleInstance::s = SingleInstance::getInstance(); //启动的时候需要初始化不管需不需要
mutex SingleInstance::Instance_mutex;
访问者模式
对象类型到功能之间的映射需求 时间复杂度O(1) 代码好维护
利用虚函数进行跳转 让编译器帮我们改代码
class A;
class B;
class C;
class D;
class E;
class Base {
public:
class Ivisitor { //接口类里面的接口类
public:
virtual void visit(A *) = 0;
virtual void visit(B *) = 0;
virtual void visit(C *) = 0;
virtual void visit(D *) = 0;
virtual void visit(E *) = 0;
};
virtual void say() = 0;
virtual ~Base() {}
virtual void accept(Ivisitor *) = 0; //用一个函数接受接口类的对象指针
};
class A : public Base {
public:
void say() override {
cout << "class a" << endl;
}
void accept(Ivisitor *vid) override {
vid->visit(this);
return ;
}
};
class B : public Base {
public:
void say() override {
cout << "class b" << endl;
}
void accept(Ivisitor *vid) override {
vid->visit(this);
return ;
}
};
class C : public Base {
public:
void say() override {
cout << "class c" << endl;
}
void accept(Ivisitor *vid) override {
vid->visit(this);
return ;
}
};
class D : public Base {
public:
void say() override {
cout << "class d" << endl;
}
void accept(Ivisitor *vid) override {
vid->visit(this);
return ;
}
};
class E : public Base {
public:
void say() override {
cout << "class e" << endl;
}
void accept(Ivisitor *vid) override {
vid->visit(this);
return ;
}
};
/*void func_A() {
cout << "func A" << endl;
}
void func_B() {
cout << "func B" << endl;
}
void func_C() {
cout << "func C" << endl;
}
void func_D() {
cout << "func D" << endl;
}
void func_E() {
cout << "func E" << endl;
}*/
//访问者对象
class outp : public Base::Ivisitor { //继承一个接口类
public:
void visit(A *) {
cout << "func a "<< endl;
}
void visit(B *) {
cout << "func B "<< endl;
}
void visit(C *) {
cout << "func C "<< endl;
}
void visit(D *) {
cout << "func D "<< endl;
}
void visit(E *) {
cout << "func E "<< endl;
}
};
int main() {
srand(time(0));
Base *p = nullptr;
switch (rand() % 5) {
case 0: p = new A(); break;
case 1: p = new B(); break;
case 2: p = new C(); break;
case 3: p = new D(); break;
case 4: p = new E(); break;
}
p->say();
outp vis;
p->accept(&vis);
/* if (dynamic_cast<A *> (p)) { //很难后期维护
func_A();
} else if(dynamic_cast<B *>(p)) {
func_B();
} else if(dynamic_cast<C *>(p)) {
func_C();
} else if (dynamic_cast<D *>(p)) {
func_D();
} else {
func_E();
}*/
return 0;
}
工厂模式
专门定义一个类来负责创建其它类的实例,被创建的实例通常都具有共同的父类;
#include<iostream>
#include<string.h>
using namespace std;
class Fruit{
public:
virtual void getFruit() = 0;
private:
};
class Banana : public Fruit{
public:
virtual void getFruit(){
cout<<"我是香蕉......"<<endl;
}
private:
};
class Apple : public Fruit{
public:
virtual void getFruit(){
cout<<"我是苹果......"<<endl;
}
private:
};
class Factory{
public:
Fruit *create(const char *p){
if(strcmp(p, "banana") == 0){
return new Banana;
}else if(strcmp(p, "apple") == 0){
return new Apple;
}else{
cout<<"不支持"<<endl;
}
}
private:
};
int main(void){
Factory *f = new Factory;
Fruit *fruit = NULL;
//工厂生产香蕉
fruit = f->create("banana");
fruit->getFruit();
//工厂生产苹果
fruit = f->create("apple");
fruit->getFruit();
delete f;
return 0;
}
原型模式
提供一个clone()接口,让普通对象拥有自我复制的功能!
相当于拷贝构造函数的编写;(当有成员为指针时)这个牵扯深拷贝和浅拷贝的问题;
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
virtual Person *clone() = 0;
virtual void printT() = 0;
private:
};
class Cplus : public Person{
public:
Cplus(){
m_name = "";
m_age = 0;
}
Cplus(string name, int age){
m_name = name;
m_age = age;
}
virtual void printT(){
cout<<"m_name:"<<m_name<<" "<<"m_age:"<<m_age<<endl;
}
virtual Person *clone(){
Cplus *tmp = new Cplus;
*tmp = *this;
return tmp;
}
private:
string m_name;
int m_age;
char *p; //此时就会牵扯深拷贝的问题
};
int main(void){
Cplus *c1 = new Cplus("张三", 32);
c1->printT();
Person *c2 = c1->clone();
c2->printT();
return 0;
}
适配器模式
适配器是将2个不同的对象进行连接和组合!将一个类的接口转换成客户希望的另外一个接口。
#include<iostream>
using namespace std;
class Current18{
public:
virtual void useCurrent18() = 0;
private:
};
class Current220{
public:
void useCurrent220(){
cout<<"我是220V,欢迎使用"<<endl;
}
private:
};
class Adapter : public Current18 { //适配器,要满足客户的需求;
public:
Adapter(Current220 *current){
m_current = current;
}
virtual void useCurrent18(){
cout<<"适配器 适配220V"<<endl;
m_current->useCurrent220();
}
private:
Current220 *m_current;
};
int main(void){
Current220 *current220 = new Current220;
Adapter *adapter = new Adapter(current220);
adapter->useCurrent18();
delete current220;
return 0;
}
中介者模式
就是借用一个中间的类,来完成其他2个类之间要实现的功能!!!(用一个父类完成两个子类之间的联系)
class Person{
public:
Person(string name, int sex, int condi){
m_name = name;
m_sex = sex;
m_condi = condi;
}
string getName(){
return m_name;
}
int getSex(){
return m_sex;
}
int getCondi(){
return m_condi;
}
virtual void getParent(Person *p) = 0;
protected:
string m_name;
int m_sex;
int m_condi;
};
class Woman : public Person{
public:
Woman(string name, int sex, int condi) : Person(name, sex, condi){
}
void getParent(Person *p){
if(this->m_sex == p->getSex()){
cout<<"不是×××"<<endl;
}
if(this->getCondi() == p->getCondi()){
cout<<this->getName()<<"和"<<p->getName()<<"绝配"<<endl;
}else{
cout<<this->getName()<<"和"<<p->getName()<<"不配"<<endl;
}
}
private:
};
//
class Man : public Person{
public:
Man(string name, int sex, int condi) : Person(name, sex, condi){
}
void getParent(Person *p){
if(this->m_sex == p->getSex()){
cout<<"不是×××"<<endl;
}
if(this->getCondi() == p->getCondi()){
cout<<this->getName()<<"和"<<p->getName()<<"绝配"<<endl;
}else{
cout<<this->getName()<<"和"<<p->getName()<<"不配"<<endl;
}
}
private:
};
int main(void){
Person *xiaofang = new Woman("小芳", 2, 5);
Person *zhangsan = new Man("张三", 1, 4);
Person *lisi = new Man("李四", 1, 5);
xiaofang->getParent(zhangsan);
xiaofang->getParent(lisi);
return 0;
}
命令者模式
把一个动作进行分解,分成发布者和接受者;
#include<iostream>
using namespace std;
class Doctor{
public:
void treatEye(){
cout<<"医生 治疗 眼病"<<endl;
}
void treatNose(){
cout<<"医生 治疗 鼻科病"<<endl;
}
private:
};
class Command{
public:
virtual void treat() = 0;
private:
};
class CommandTreatEye : public Command{
public:
CommandTreatEye(Doctor *doctor){
m_doctor = doctor;
}
void treat(){
m_doctor->treatEye();
}
private:
Doctor *m_doctor;
};
class CommandTreatNose : public Command{
public:
CommandTreatNose(Doctor *doctor){
m_doctor = doctor;
}
void treat(){
m_doctor->treatNose();
}
private:
Doctor *m_doctor;
};
class BeautyNurse{
public:
BeautyNurse(Command *command){
this->command = command;
}
public:
void SubmittedCase(){ //提交病类, 下单命令
command->treat();
}
private:
Command *command;
};
int main(void){
/*
//1、医生直接看病
Doctor *doctor = new Doctor;
doctor->treatEye();
delete doctor;
*/
/*
//2、通过一个命令
Doctor *doctor = new Doctor;
Command *command = new CommandTreatEye(doctor);
command->treat();
delete command;
delete doctor;
*/
//护士提交简历,医生看病;
Doctor *doctor = new Doctor;
// Command *command = new CommandTreatEye(doctor);
Command *command01 = new CommandTreatNose(doctor);
BeautyNurse *beautyNurse = new BeautyNurse(command01);
beautyNurse->SubmittedCase();
delete doctor;
delete command01;
delete beautyNurse;
return 0;
}
迭代器模式
里面有一个对集合的引用,这个迭代器是对谁创建的,这个迭代器就持有谁(这个集合)的引用!
#include<iostream>
using namespace std;
#define SIZE 5
class MyIterator{ //抽象的迭代器
public:
virtual void First() = 0;
virtual void Next() = 0;
virtual bool isDone() = 0;
virtual int CurrentItem() = 0;
private:
};
class Abstract{ //抽象的集合类
public:
virtual MyIterator *createIterator() = 0;
virtual int getItem(int index) = 0;
virtual int getSize() = 0;
protected:
};
class ConIterator : public MyIterator{ // 具体的迭代器
public:
ConIterator(Abstract *ag){
p = ag;
currentIndex = 0;
}
virtual void First(){
currentIndex = 0; //让当前游标回到位置0;
}
virtual void Next(){
if(currentIndex < p->getSize())
currentIndex++;
}
virtual bool isDone(){
return currentIndex == p->getSize();
}
virtual int CurrentItem(){
return p->getItem(currentIndex);
}
private:
int currentIndex;
Abstract *p;
};
class Jh : public Abstract{
public:
Jh(){
for(int i = 0; i < SIZE; i++){
arr[i] = i+100;
}
}
virtual MyIterator *createIterator(){
return new ConIterator(this); //让迭代器持有一个集合的引用!!!
}
virtual int getItem(int index){
return arr[index];
}
virtual int getSize(){
return SIZE;
}
private:
int arr[SIZE];
};
int main(void){
Abstract *ag = new Jh;
MyIterator *it = ag->createIterator();
for(; !(it->isDone()); it->Next()){
cout<<it->CurrentItem()<<" ";
}
cout<<endl;
delete ag;
delete it;
return 0;
}