设计模式分类:①创建型模式②结构型模式③行为型模式
设计模式的基本原则:①开放封闭原则②依赖倒置原则③接口隔离原则④里氏替用原则⑤合成复用原则⑥迪米特模式
开放封闭原则代码:
#include <iostream>
using namespace std;
/*class BankWorker
{
public:
void SaveMoney()
{
cout << "SaveMoney" << endl;
}
void GetMoney()
{
cout << "GetMoney" << endl;
}
void TransformMoney()
{
cout << "TransformMoney" << endl;
}
};*/
class BankWorker
{
public:
virtual void work() = 0;
};
class SaveWorker : public BankWorker
{
public:
void work()
{
cout << "SaveMoney" << endl;
}
};
class GetWorker : public BankWorker
{
public:
void work()
{
cout << "GetMoney" << endl;
}
};
int main()
{
/*BankWorker *b = new BankWorker;
b->SaveMoney();
b->GetMoney();
delete b;*/
BankWorker *b = NULL;
b = new SaveWorker;
b->work();
delete b;
b = new GetWorker;
b->work();
delete b;
return 0;
}
依赖倒置原则代码:
#include <iostream>
using namespace std;
/*class RAM_2G
{
public:
void RAMWork()
{
cout << "RAM_2G" << endl;
}
};
class RAM_4G
{
public:
void RAMWork()
{
cout << "RAM_4G" << endl;
}
};
class ROM_32G
{
public:
void ROMWork()
{
cout << "ROM_32G" << endl;
}
};
class Phone //依赖于实现
{
private:
RAM_2G *ram;
ROM_32G *rom;
public:
Phone(RAM_2G *a, ROM_32G *o)
{
ram = a;
rom = o;
}
void PhoneWork()
{
ram->RAMWork();
rom->ROMWork();
}
};*/
class RAM //抽象
{
public:
virtual void RAMWork() = 0;
};
class RAM_2G : public RAM //实现
{
public:
void RAMWork()
{
cout << "RAM_2G" << endl;
}
};
class RAM_4G : public RAM //实现
{
public:
void RAMWork()
{
cout << "RAM_4G" << endl;
}
};
class ROM //抽象
{
public:
virtual void ROMWork() = 0;
};
class ROM_32G : public ROM //实现
{
public:
void ROMWork()
{
cout << "ROM_32G" << endl;
}
};
class ROM_64G : public ROM //实现
{
public:
void ROMWork()
{
cout << "ROM_64G" << endl;
}
};
class Phone
{
private:
RAM *ram;
ROM *rom;
public:
public:
Phone(RAM *a, ROM *o)
{
ram = a;
rom = o;
}
void PhoneWork()
{
ram->RAMWork();
rom->ROMWork();
}
};
int main()
{
/*RAM_2G *ram = new RAM_2G;
ROM_32G *rom = new ROM_32G;
Phone *p = new Phone(ram, rom);
p->PhoneWork();*/
RAM *ram = new RAM_2G;
ROM *rom = new ROM_32G;
Phone *p = new Phone(ram, rom);
p->PhoneWork();
delete ram;
delete rom;
delete p;
ram = new RAM_4G;
rom = new ROM_64G;
p = new Phone(ram, rom);
p->PhoneWork();
delete ram;
delete rom;
delete p;
return 0;
}
单例模式-懒汉式代码:
#include <iostream>
#include <pthread.h>
using namespace std;
pthread_mutex_t mutex;
class Singleton
{
private:
static Singleton *mInstance;
static int ObjectCount;
private: //构造函数声明成私有
Singleton()
{
}
public:
static Singleton *GetInstance() //提供给外部创建对象的接口
{
ObjectCount++;
if (mInstance == NULL)
{
usleep(10000);
mInstance = new Singleton;
}
return mInstance;
}
static int GetObjectCount()
{
return ObjectCount;
}
void release()
{
ObjectCount--;
if (ObjectCount == 0 && mInstance != NULL)
{
delete mInstance;
mInstance = NULL;
}
}
};
Singleton *Singleton::mInstance = NULL;
int Singleton::ObjectCount = 0;
void *Create(void *arg)
{
pthread_mutex_lock(&mutex);
Singleton *s = Singleton::GetInstance();
cout << s << endl;
pthread_mutex_unlock(&mutex);
}
int main()
{
/*Singleton *s1 = Singleton::GetInstance();
Singleton *s2 = Singleton::GetInstance();
Singleton *s3 = Singleton::GetInstance();
Singleton *s4 = Singleton::GetInstance();
Singleton *s5 = Singleton::GetInstance();
Singleton *s6 = Singleton::GetInstance();
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cout << s5 << endl;
cout << s6 << endl;
cout << Singleton::GetObjectCount() << endl;*/
pthread_mutex_init(&mutex, NULL);
int i, ret;
pthread_t tid[10];
for (i = 0; i < 10; i++)
{
ret = pthread_create(&tid[i], NULL, Create, NULL);
if (ret != 0)
{
perror("pthread_create");
}
}
void *status;
for (i = 0; i < 10; i++)
{
pthread_join(tid[i], &status);
}
pthread_mutex_destroy(&mutex);
return 0;
}
单例模式-饿汉式:
#include <iostream>
#include <pthread.h>
using namespace std;
class Singleton
{
private:
static Singleton *mInstance;
static int ObjectCount;
private: //构造函数声明成私有
Singleton()
{
}
public:
static Singleton *GetInstance() //提供给外部创建对象的接口
{
ObjectCount++;
return mInstance;
}
static int GetObjectCount()
{
return ObjectCount;
}
void release()
{
ObjectCount--;
if (ObjectCount == 0 && mInstance != NULL)
{
delete mInstance;
mInstance = NULL;
}
}
};
Singleton *Singleton::mInstance = new Singleton;
int Singleton::ObjectCount = 0;
void *Create(void *arg)
{
Singleton *s = Singleton::GetInstance();
cout << s << endl;
}
int main()
{
/*Singleton *s1 = Singleton::GetInstance();
Singleton *s2 = Singleton::GetInstance();
Singleton *s3 = Singleton::GetInstance();
Singleton *s4 = Singleton::GetInstance();
Singleton *s5 = Singleton::GetInstance();
Singleton *s6 = Singleton::GetInstance();
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cout << s5 << endl;
cout << s6 << endl;
cout << Singleton::GetObjectCount() << endl;*/
int i, ret;
pthread_t tid[10];
for (i = 0; i < 10; i++)
{
ret = pthread_create(&tid[i], NULL, Create, NULL);
if (ret != 0)
{
perror("pthread_create");
}
}
void *status;
for (i = 0; i < 10; i++)
{
pthread_join(tid[i], &status);
}
return 0;
}
简单工厂模式:
#include <iostream>
using namespace std;
class Car
{
public:
virtual void show() = 0;
};
class BMW : public Car
{
public:
void show()
{
cout << "BMW" << endl;
}
};
class BENZ : public Car
{
public:
void show()
{
cout << "BENZ" << endl;
}
};
class Honda : public Car
{
public:
void show()
{
cout << "Honda" << endl;
}
};
class Factory
{
public:
Car *CreateBMW()
{
return new BMW;
}
Car *CreateBENZ()
{
return new BENZ;
}
Car *CreateHonda()
{
return new Honda;
}
};
int main()
{
Car *car = NULL;
Factory *f = new Factory;
car = f->CreateBMW(); //不需要关注对象创建的细节
car->show();
delete car;
car = f->CreateBENZ();
car->show();
delete car;
return 0;
}
工厂模式代码:
#include <iostream>
using namespace std;
class Car
{
public:
virtual void show() = 0;
};
class BMW : public Car
{
public:
void show()
{
cout << "BMW" << endl;
}
};
class BENZ : public Car
{
public:
void show()
{
cout << "BENZ" << endl;
}
};
class Honda : public Car
{
public:
void show()
{
cout << "Honda" << endl;
}
};
class Factory
{
public:
virtual Car *CreateCar() = 0;
};
class Factorybenz : public Factory
{
public:
Car *CreateCar()
{
return new BENZ;
}
};
class Factoryhonda : public Factory
{
public:
Car *CreateCar()
{
return new Honda;
}
};
class Factorybmw : public Factory
{
public:
Car *CreateCar()
{
return new BMW;
}
};
int main()
{
Car *car = NULL;
Factory *f = new Factorybmw;
car = f->CreateCar();
car->show();
delete car;
delete f;
f = new Factorybenz;
car = f->CreateCar();
car->show();
delete car;
delete f;
return 0;
}
抽象工厂模式代码:
#include <iostream>
using namespace std;
class Fruit
{
public:
virtual void show() = 0;
};
class NorthApple : public Fruit
{
public:
void show()
{
cout << "NorthApple" << endl;
}
};
class NorthPear : public Fruit
{
public:
void show()
{
cout << "NorthPear" << endl;
}
};
class SouthPear : public Fruit
{
public:
void show()
{
cout << "SouthPear" << endl;
}
};
class SouthApple : public Fruit
{
public:
void show()
{
cout << "SouthApple" << endl;
}
};
class Factory
{
public:
virtual Fruit *CreateApple() = 0;
virtual Fruit *CreatePear() = 0;
};
class SouthFactory : public Factory
{
public:
Fruit *CreateApple()
{
return new SouthApple;
}
Fruit *CreatePear()
{
return new SouthPear;
}
};
class NorthFactory : public Factory
{
public:
Fruit *CreateApple()
{
return new NorthApple;
}
Fruit *CreatePear()
{
return new NorthPear;
}
};
void Create(Factory *f)
{
Fruit *fruit = NULL;
fruit = f->CreateApple();
fruit->show();
delete fruit;
fruit = f->CreatePear();
fruit->show();
delete fruit;
}
int main()
{
Factory *f = new NorthFactory;
Create(f);
delete f;
f = new SouthFactory;
Create(f);
delete f;
return 0;
}