C++设计模式(单例模式,工厂模式)

开放封闭原则:类的改动是通过增加代码进行的,而不是修改源代码

#include <iostream>

 

using namespace std;

 

/*class BankWorker

{

public:

void SaveMoney()

{

cout << "存款业务办理" << endl;

}

 

void GetMoney()

{

cout << "取款业务办理" << endl;

}

 

void TransformMoney()

{

cout << "转账业务办理" << endl;

}

};*/

 

class BankWorker //基类

{

public:

virtual void worker() = 0;

};

 

class GetMoney : public BankWorker

{

public:

void worker()

{

cout << "取款业务办理" << endl;

}

};

 

class SaveMoney : public BankWorker

{

public:

void worker()

{

cout << "存款业务办理" << endl;

}

};

 

int main()

{

/*BankWorker *b = new BankWorker;

 

b->GetMoney();

b->SaveMoney();*/

 

BankWorker *b = new SaveMoney;

b->worker();

delete b;

 

b = new GetMoney;

b->worker();

delete b;

 

return 0;

}

 

依赖倒置原则:依赖于抽象(接口),不要依赖具体的实现(类),也就是针对接口编程

#include <iostream>

 

using namespace std;

 

class HardDisk

{

public:

virtual void work() = 0;

};

 

class AHardDisk : public HardDisk

{

public:

void work()

{

cout << "HardDisk work" << endl;

}

};

 

class CPU

{

public:

virtual void work() = 0;

};

 

class IntelCPU : public CPU

{

public:

void work()

{

cout << "cpu work" << endl;

}

};

 

class Computer

{

private:

HardDisk *m_h;

CPU *m_c;

public:

Computer(HardDisk *h, CPU *c)

{

m_h = h;

m_c = c;

}

void work()

{

m_h->work();

m_c->work();

}

};

 

int main()

{

CPU *c = new IntelCPU;

HardDisk *h = new AHardDisk;

 

Computer *com = new Computer(h, c);

com->work();

 

return 0;

}

 

单例懒汉模式

#include <iostream>

#include <pthread.h>

 

using namespace std;

 

pthread_mutex_t mutex;

 

class Singleton

{

private:

static Singleton *m_instance;

static int count;

private:

Singleton() //构造函数私有 只能在类的内部创建对象

{

}

public:

static Singleton *GetInstance()

{

if (NULL == m_instance)

{

usleep(100000);

m_instance = new Singleton;

}

count++;

return m_instance;

}

 

static int GetCount()

{

return count;

}

 

void Release()

{

count--;

if (count == 0 && m_instance != NULL)

{

delete m_instance;

}

}

};

 

Singleton * Singleton::m_instance = NULL;

int Singleton::count = 0;

 

void *CreateInstance(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();

 

cout << Singleton::GetCount() << endl;

 

if (s1 == s2)

{

cout << "equal" << endl;

}*/

 

int ret;

pthread_t tid[10];

 

pthread_mutex_init(&mutex, NULL);

 

for (int i = 0; i < 10; i++)

{

ret = pthread_create(&tid[i], NULL, CreateInstance, NULL);

if (ret != 0)

{

perror("pthread_create");

}

}

 

void *status;

for (int i = 0; i < 10; i++)

{

pthread_join(tid[i], &status);

}

pthread_mutex_destroy(&mutex);

 

return 0;

}

 

单例饿汉模式

#include <iostream>

 

using namespace std;

 

class Singleton

{

private:

static Singleton *m_instance;

static int count;

private:

Singleton() //构造函数私有 只能在类的内部创建对象

{

}

public:

static Singleton *GetInstance()

{

count++;

return m_instance;

}

 

static int GetCount()

{

return count;

}

 

void Release()

{

count--;

if (count == 0 && m_instance != NULL)

{

delete m_instance;

}

}

};

 

Singleton *Singleton::m_instance = new Singleton;

int Singleton::count = 0;

 

int main()

{

Singleton *s1 = Singleton::GetInstance();

Singleton *s2 = Singleton::GetInstance();

Singleton *s3 = Singleton::GetInstance();

Singleton *s4 = Singleton::GetInstance();

 

cout << Singleton::GetCount() << endl;

 

if (s1 == s2)

{

cout << "equal" << endl;

}

 

return 0;

}

 

简单工厂模式:

#include <iostream>

 

using namespace std;

 

class Fruit

{

public:

virtual void show() = 0;

};

 

class Apple : public Fruit

{

public:

void show()

{

cout << "This is Apple..." << endl;

}

};

 

class Banana : public Fruit

{

public:

void show()

{

cout << "This is banana..." << endl;

}

};

 

class Pear : public Fruit

{

public:

void show()

{

cout << "This is Pear..." << endl;

}

};

 

class Factory

{

public:

Fruit *CreateApple()

{

return new Apple;

}

Fruit *CreateBanana()

{

return new Banana;

}

Fruit *CreatePear()

{

return new Pear;

}

};

 

int main()

{

/*Fruit *f1 = new Apple;

Fruit *f2 = new Banana;

Fruit *f3 = new Pear;*/

 

Fruit *fruit;

Factory *f = new Factory;

 

fruit = f->CreateApple();

fruit->show();

delete fruit;

 

fruit = f->CreatePear();

fruit->show();

 

return 0;

}

 

 

开放封闭原则:类的改动是通过增加代码进行的,而不是修改源代码

#include <iostream>

 

using namespace std;

 

/*class BankWorker

{

public:

void SaveMoney()

{

cout << "存款业务办理" << endl;

}

 

void GetMoney()

{

cout << "取款业务办理" << endl;

}

 

void TransformMoney()

{

cout << "转账业务办理" << endl;

}

};*/

 

class BankWorker //基类

{

public:

virtual void worker() = 0;

};

 

class GetMoney : public BankWorker

{

public:

void worker()

{

cout << "取款业务办理" << endl;

}

};

 

class SaveMoney : public BankWorker

{

public:

void worker()

{

cout << "存款业务办理" << endl;

}

};

 

int main()

{

/*BankWorker *b = new BankWorker;

 

b->GetMoney();

b->SaveMoney();*/

 

BankWorker *b = new SaveMoney;

b->worker();

delete b;

 

b = new GetMoney;

b->worker();

delete b;

 

return 0;

}

 

依赖倒置原则:依赖于抽象(接口),不要依赖具体的实现(类),也就是针对接口编程

#include <iostream>

 

using namespace std;

 

class HardDisk

{

public:

virtual void work() = 0;

};

 

class AHardDisk : public HardDisk

{

public:

void work()

{

cout << "HardDisk work" << endl;

}

};

 

class CPU

{

public:

virtual void work() = 0;

};

 

class IntelCPU : public CPU

{

public:

void work()

{

cout << "cpu work" << endl;

}

};

 

class Computer

{

private:

HardDisk *m_h;

CPU *m_c;

public:

Computer(HardDisk *h, CPU *c)

{

m_h = h;

m_c = c;

}

void work()

{

m_h->work();

m_c->work();

}

};

 

int main()

{

CPU *c = new IntelCPU;

HardDisk *h = new AHardDisk;

 

Computer *com = new Computer(h, c);

com->work();

 

return 0;

}

 

单例懒汉模式

#include <iostream>

#include <pthread.h>

 

using namespace std;

 

pthread_mutex_t mutex;

 

class Singleton

{

private:

static Singleton *m_instance;

static int count;

private:

Singleton() //构造函数私有 只能在类的内部创建对象

{

}

public:

static Singleton *GetInstance()

{

if (NULL == m_instance)

{

usleep(100000);

m_instance = new Singleton;

}

count++;

return m_instance;

}

 

static int GetCount()

{

return count;

}

 

void Release()

{

count--;

if (count == 0 && m_instance != NULL)

{

delete m_instance;

}

}

};

 

Singleton * Singleton::m_instance = NULL;

int Singleton::count = 0;

 

void *CreateInstance(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();

 

cout << Singleton::GetCount() << endl;

 

if (s1 == s2)

{

cout << "equal" << endl;

}*/

 

int ret;

pthread_t tid[10];

 

pthread_mutex_init(&mutex, NULL);

 

for (int i = 0; i < 10; i++)

{

ret = pthread_create(&tid[i], NULL, CreateInstance, NULL);

if (ret != 0)

{

perror("pthread_create");

}

}

 

void *status;

for (int i = 0; i < 10; i++)

{

pthread_join(tid[i], &status);

}

pthread_mutex_destroy(&mutex);

 

return 0;

}

 

单例饿汉模式

#include <iostream>

 

using namespace std;

 

class Singleton

{

private:

static Singleton *m_instance;

static int count;

private:

Singleton() //构造函数私有 只能在类的内部创建对象

{

}

public:

static Singleton *GetInstance()

{

count++;

return m_instance;

}

 

static int GetCount()

{

return count;

}

 

void Release()

{

count--;

if (count == 0 && m_instance != NULL)

{

delete m_instance;

}

}

};

 

Singleton *Singleton::m_instance = new Singleton;

int Singleton::count = 0;

 

int main()

{

Singleton *s1 = Singleton::GetInstance();

Singleton *s2 = Singleton::GetInstance();

Singleton *s3 = Singleton::GetInstance();

Singleton *s4 = Singleton::GetInstance();

 

cout << Singleton::GetCount() << endl;

 

if (s1 == s2)

{

cout << "equal" << endl;

}

 

return 0;

}

 

简单工厂模式:

#include <iostream>

 

using namespace std;

 

class Fruit

{

public:

virtual void show() = 0;

};

 

class Apple : public Fruit

{

public:

void show()

{

cout << "This is Apple..." << endl;

}

};

 

class Banana : public Fruit

{

public:

void show()

{

cout << "This is banana..." << endl;

}

};

 

class Pear : public Fruit

{

public:

void show()

{

cout << "This is Pear..." << endl;

}

};

 

class Factory

{

public:

Fruit *CreateApple()

{

return new Apple;

}

Fruit *CreateBanana()

{

return new Banana;

}

Fruit *CreatePear()

{

return new Pear;

}

};

 

int main()

{

/*Fruit *f1 = new Apple;

Fruit *f2 = new Banana;

Fruit *f3 = new Pear;*/

 

Fruit *fruit;

Factory *f = new Factory;

 

fruit = f->CreateApple();

fruit->show();

delete fruit;

 

fruit = f->CreatePear();

fruit->show();

 

return 0;

}

 

 

开放封闭原则:类的改动是通过增加代码进行的,而不是修改源代码

#include <iostream>

 

using namespace std;

 

/*class BankWorker

{

public:

void SaveMoney()

{

cout << "存款业务办理" << endl;

}

 

void GetMoney()

{

cout << "取款业务办理" << endl;

}

 

void TransformMoney()

{

cout << "转账业务办理" << endl;

}

};*/

 

class BankWorker //基类

{

public:

virtual void worker() = 0;

};

 

class GetMoney : public BankWorker

{

public:

void worker()

{

cout << "取款业务办理" << endl;

}

};

 

class SaveMoney : public BankWorker

{

public:

void worker()

{

cout << "存款业务办理" << endl;

}

};

 

int main()

{

/*BankWorker *b = new BankWorker;

 

b->GetMoney();

b->SaveMoney();*/

 

BankWorker *b = new SaveMoney;

b->worker();

delete b;

 

b = new GetMoney;

b->worker();

delete b;

 

return 0;

}

 

依赖倒置原则:依赖于抽象(接口),不要依赖具体的实现(类),也就是针对接口编程

#include <iostream>

 

using namespace std;

 

class HardDisk

{

public:

virtual void work() = 0;

};

 

class AHardDisk : public HardDisk

{

public:

void work()

{

cout << "HardDisk work" << endl;

}

};

 

class CPU

{

public:

virtual void work() = 0;

};

 

class IntelCPU : public CPU

{

public:

void work()

{

cout << "cpu work" << endl;

}

};

 

class Computer

{

private:

HardDisk *m_h;

CPU *m_c;

public:

Computer(HardDisk *h, CPU *c)

{

m_h = h;

m_c = c;

}

void work()

{

m_h->work();

m_c->work();

}

};

 

int main()

{

CPU *c = new IntelCPU;

HardDisk *h = new AHardDisk;

 

Computer *com = new Computer(h, c);

com->work();

 

return 0;

}

 

单例懒汉模式

#include <iostream>

#include <pthread.h>

 

using namespace std;

 

pthread_mutex_t mutex;

 

class Singleton

{

private:

static Singleton *m_instance;

static int count;

private:

Singleton() //构造函数私有 只能在类的内部创建对象

{

}

public:

static Singleton *GetInstance()

{

if (NULL == m_instance)

{

usleep(100000);

m_instance = new Singleton;

}

count++;

return m_instance;

}

 

static int GetCount()

{

return count;

}

 

void Release()

{

count--;

if (count == 0 && m_instance != NULL)

{

delete m_instance;

}

}

};

 

Singleton * Singleton::m_instance = NULL;

int Singleton::count = 0;

 

void *CreateInstance(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();

 

cout << Singleton::GetCount() << endl;

 

if (s1 == s2)

{

cout << "equal" << endl;

}*/

 

int ret;

pthread_t tid[10];

 

pthread_mutex_init(&mutex, NULL);

 

for (int i = 0; i < 10; i++)

{

ret = pthread_create(&tid[i], NULL, CreateInstance, NULL);

if (ret != 0)

{

perror("pthread_create");

}

}

 

void *status;

for (int i = 0; i < 10; i++)

{

pthread_join(tid[i], &status);

}

pthread_mutex_destroy(&mutex);

 

return 0;

}

 

单例饿汉模式

#include <iostream>

 

using namespace std;

 

class Singleton

{

private:

static Singleton *m_instance;

static int count;

private:

Singleton() //构造函数私有 只能在类的内部创建对象

{

}

public:

static Singleton *GetInstance()

{

count++;

return m_instance;

}

 

static int GetCount()

{

return count;

}

 

void Release()

{

count--;

if (count == 0 && m_instance != NULL)

{

delete m_instance;

}

}

};

 

Singleton *Singleton::m_instance = new Singleton;

int Singleton::count = 0;

 

int main()

{

Singleton *s1 = Singleton::GetInstance();

Singleton *s2 = Singleton::GetInstance();

Singleton *s3 = Singleton::GetInstance();

Singleton *s4 = Singleton::GetInstance();

 

cout << Singleton::GetCount() << endl;

 

if (s1 == s2)

{

cout << "equal" << endl;

}

 

return 0;

}

 

简单工厂模式:

#include <iostream>

 

using namespace std;

 

class Fruit

{

public:

virtual void show() = 0;

};

 

class Apple : public Fruit

{

public:

void show()

{

cout << "This is Apple..." << endl;

}

};

 

class Banana : public Fruit

{

public:

void show()

{

cout << "This is banana..." << endl;

}

};

 

class Pear : public Fruit

{

public:

void show()

{

cout << "This is Pear..." << endl;

}

};

 

class Factory

{

public:

Fruit *CreateApple()

{

return new Apple;

}

Fruit *CreateBanana()

{

return new Banana;

}

Fruit *CreatePear()

{

return new Pear;

}

};

 

int main()

{

/*Fruit *f1 = new Apple;

Fruit *f2 = new Banana;

Fruit *f3 = new Pear;*/

 

Fruit *fruit;

Factory *f = new Factory;

 

fruit = f->CreateApple();

fruit->show();

delete fruit;

 

fruit = f->CreatePear();

fruit->show();

 

return 0;

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值