添加设计模式及例子,包括:单例、抽象工厂、适配器、桥接、观察者模式

This commit is contained in:
huihut 2018-07-22 14:50:54 +08:00
parent 92bc715c1f
commit b69685a5a7
31 changed files with 839 additions and 31 deletions

View File

@ -0,0 +1,25 @@
//
// Created by xiemenghui on 2018/7/20.
//
#include "Factory.h"
#include "concrete_factory.h"
Factory* Factory::CreateFactory(FACTORY_TYPE factory)
{
Factory *pFactory = nullptr;
switch (factory) {
case FACTORY_TYPE::BENZ_FACTORY: // 奔驰工厂
pFactory = new BenzFactory();
break;
case FACTORY_TYPE::BMW_FACTORY: // 宝马工厂
pFactory = new BmwFactory();
break;
case FACTORY_TYPE::AUDI_FACTORY: // 奥迪工厂
pFactory = new AudiFactory();
break;
default:
break;
}
return pFactory;
}

View File

@ -0,0 +1,24 @@
//
// Created by xiemenghui on 2018/7/20.
//
#ifndef DESIGNPATTERN_FACTORY_H
#define DESIGNPATTERN_FACTORY_H
#include "product.h"
// 抽象工厂模式
class Factory {
public:
enum FACTORY_TYPE {
BENZ_FACTORY, // 奔驰工厂
BMW_FACTORY, // 宝马工厂
AUDI_FACTORY // 奥迪工厂
};
virtual ICar* CreateCar() = 0; // 生产汽车
virtual IBike* CreateBike() = 0; // 生产自行车
static Factory * CreateFactory(FACTORY_TYPE factory); // 创建工厂
};
#endif //DESIGNPATTERN_FACTORY_H

View File

@ -0,0 +1,46 @@
//
// Created by xiemenghui on 2018/7/20.
//
#include "Factory.h"
#include "product.h"
#include "FactoryMain.h"
#include <iostream>
using namespace std;
void FactoryMain()
{
// ąźłŰ
Factory * pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::BENZ_FACTORY);
ICar * pCar = pFactory->CreateCar();
IBike * pBike = pFactory->CreateBike();
cout << "Benz factory - Car: " << pCar->Name() << endl;
cout << "Benz factory - Bike: " << pBike->Name() << endl;
SAFE_DELETE(pCar);
SAFE_DELETE(pBike);
SAFE_DELETE(pFactory);
// ąŚÂí
pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::BMW_FACTORY);
pCar = pFactory->CreateCar();
pBike = pFactory->CreateBike();
cout << "Bmw factory - Car: " << pCar->Name() << endl;
cout << "Bmw factory - Bike: " << pBike->Name() << endl;
SAFE_DELETE(pCar);
SAFE_DELETE(pBike);
SAFE_DELETE(pFactory);
// °ÂľĎ
pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::AUDI_FACTORY);
pCar = pFactory->CreateCar();
pBike = pFactory->CreateBike();
cout << "Audi factory - Car: " << pCar->Name() << endl;
cout << "Audi factory - Bike: " << pBike->Name() << endl;
SAFE_DELETE(pCar);
SAFE_DELETE(pBike);
SAFE_DELETE(pFactory);
}

View File

@ -0,0 +1,14 @@
//
// Created by xiemenghui on 2018/7/20.
//
#ifndef DESIGNPATTERN_FACTORYMAIN_H
#define DESIGNPATTERN_FACTORYMAIN_H
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if(p) {delete(p); (p)=nullptr;}}
#endif
void FactoryMain();
#endif //DESIGNPATTERN_FACTORYMAIN_H

View File

@ -0,0 +1,51 @@
//
// Created by xiemenghui on 2018/7/20.
//
#ifndef DESIGNPATTERN_CONCRETE_FACTORY_H
#define DESIGNPATTERN_CONCRETE_FACTORY_H
#include "Factory.h"
#include "concrete_product.h"
// 奔驰工厂
class BenzFactory : public Factory
{
public:
ICar* CreateCar()
{
return new BenzCar();
}
IBike* CreateBike()
{
return new BenzBike();
}
};
// 宝马工厂
class BmwFactory : public Factory
{
public:
ICar* CreateCar() {
return new BmwCar();
}
IBike* CreateBike() {
return new BmwBike();
}
};
// 奥迪工厂
class AudiFactory : public Factory
{
public:
ICar* CreateCar() {
return new AudiCar();
}
IBike* CreateBike() {
return new AudiBike();
}
};
#endif //DESIGNPATTERN_CONCRETE_FACTORY_H

View File

@ -0,0 +1,72 @@
//
// Created by xiemenghui on 2018/7/20.
//
#ifndef DESIGNPATTERN_CONCRETE_PRODUCT_H
#define DESIGNPATTERN_CONCRETE_PRODUCT_H
#include "product.h"
/********** 汽车 **********/
// 奔驰
class BenzCar : public ICar
{
public:
string Name()
{
return "Benz Car";
}
};
// 宝马
class BmwCar : public ICar
{
public:
string Name()
{
return "Bmw Car";
}
};
// 奥迪
class AudiCar : public ICar
{
public:
string Name()
{
return "Audi Car";
}
};
/********** 自行车 **********/
// 奔驰
class BenzBike : public IBike
{
public:
string Name()
{
return "Benz Bike";
}
};
// 宝马
class BmwBike : public IBike
{
public:
string Name()
{
return "Bmw Bike";
}
};
// 奥迪
class AudiBike : public IBike
{
public:
string Name()
{
return "Audi Bike";
}
};
#endif //DESIGNPATTERN_CONCRETE_PRODUCT_H

View File

@ -0,0 +1,25 @@
//
// Created by xiemenghui on 2018/7/20.
//
#ifndef DESIGNPATTERN_PRODUCT_H
#define DESIGNPATTERN_PRODUCT_H
#include <string>
using std::string;
// 汽车接口
class ICar
{
public:
virtual string Name() = 0;
};
// 自行车接口
class IBike
{
public:
virtual string Name() = 0;
};
#endif //DESIGNPATTERN_PRODUCT_H

View File

@ -0,0 +1,21 @@
//
// Created by xiemenghui on 2018/7/20.
//
#ifndef DESIGNPATTERN_ADAPTERMAIN_H
#define DESIGNPATTERN_ADAPTERMAIN_H
#include "adapter.h"
void AdapterMain()
{
// 创建适配器
IRussiaSocket * pAdapter = new PowerAdapter();
// 充电
pAdapter->Charge();
SAFE_DELETE(pAdapter);
}
#endif //DESIGNPATTERN_ADAPTERMAIN_H

View File

@ -0,0 +1,20 @@
//
// Created by xiemenghui on 2018/7/20.
//
#ifndef DESIGNPATTERN_ADAPTEE_H
#define DESIGNPATTERN_ADAPTEE_H
#include <iostream>
// 自带的充电器(两脚扁型)
class OwnCharger
{
public:
void ChargeWithFeetFlat()
{
std::cout << "OwnCharger::ChargeWithFeetFlat\n";
}
};
#endif //DESIGNPATTERN_ADAPTEE_H

View File

@ -0,0 +1,34 @@
//
// Created by xiemenghui on 2018/7/20.
//
#ifndef DESIGNPATTERN_ADAPTER_H
#define DESIGNPATTERN_ADAPTER_H
#include "target.h"
#include "adaptee.h"
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if(p){delete(p); (p)=NULL;} }
#endif
// 电源适配器
class PowerAdapter : public IRussiaSocket
{
public:
PowerAdapter() : m_pCharger(new OwnCharger()){}
~PowerAdapter()
{
SAFE_DELETE(m_pCharger);
}
void Charge()
{
// 使用自带的充电器(两脚扁形)充电
m_pCharger->ChargeWithFeetFlat();
}
private:
// 持有需要被适配的接口对象(自带的充电器)
OwnCharger* m_pCharger;
};
#endif //DESIGNPATTERN_ADAPTER_H

View File

@ -0,0 +1,16 @@
//
// Created by xiemenghui on 2018/7/20.
//
#ifndef DESIGNPATTERN_TARGET_H
#define DESIGNPATTERN_TARGET_H
// 俄罗斯提供的插座
class IRussiaSocket
{
public:
// 使用双脚圆形充电(暂不实现)
virtual void Charge() = 0;
};
#endif //DESIGNPATTERN_TARGET_H

View File

@ -0,0 +1,30 @@
//
// Created by xiemenghui on 2018/7/21.
//
#include "BridgeMain.h"
void BridgeMain()
{
// 创建电器(电灯、电风扇)
IElectricalEquipment * light = new Light();
IElectricalEquipment * fan = new Fan();
// 创建开关(拉链式开关、两位开关)
// 将拉链式开关和电灯关联起来,两位开关和风扇关联起来
ISwitch * pullChain = new PullChainSwitch(light);
ISwitch * twoPosition = new TwoPositionSwitch(fan);
// 开灯、关灯
pullChain->On();
pullChain->Off();
// 打开风扇、关闭风扇
twoPosition->On();
twoPosition->Off();
SAFE_DELETE(twoPosition);
SAFE_DELETE(pullChain);
SAFE_DELETE(fan);
SAFE_DELETE(light);
}

View File

@ -0,0 +1,17 @@
//
// Created by xiemenghui on 2018/7/21.
//
#ifndef DESIGNPATTERN_BRIDGEMAIN_H
#define DESIGNPATTERN_BRIDGEMAIN_H
#include "refined_abstraction.h"
#include "concrete_implementor.h"
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if(p){delete(p); (p)=nullptr;} }
#endif
void BridgeMain();
#endif //DESIGNPATTERN_BRIDGEMAIN_H

View File

@ -0,0 +1,23 @@
//
// Created by xiemenghui on 2018/7/21.
//
#ifndef DESIGNPATTERN_ABSTRACTION_H
#define DESIGNPATTERN_ABSTRACTION_H
#include "implementor.h"
// 开关
class ISwitch
{
public:
ISwitch(IElectricalEquipment *ee){ m_pEe = ee; }
virtual ~ISwitch(){}
virtual void On() = 0; // 打开电器
virtual void Off() = 0; // 关闭电器
protected:
IElectricalEquipment * m_pEe;
};
#endif //DESIGNPATTERN_ABSTRACTION_H

View File

@ -0,0 +1,44 @@
//
// Created by xiemenghui on 2018/7/21.
//
#ifndef DESIGNPATTERN_CONCRETE_IMPLEMENTOR_H
#define DESIGNPATTERN_CONCRETE_IMPLEMENTOR_H
#include "implementor.h"
#include <iostream>
// 电灯
class Light : public IElectricalEquipment
{
public:
// 开灯
virtual void PowerOn() override
{
std::cout << "Light is on." << std::endl;
}
// 关灯
virtual void PowerOff() override
{
std::cout << "Light is off." << std::endl;
}
};
// 风扇
class Fan : public IElectricalEquipment
{
public:
// 打开风扇
virtual void PowerOn() override
{
std::cout << "Fan is on." << std::endl;
}
//关闭风扇
virtual void PowerOff() override
{
std::cout << "Fan is off." << std::endl;
}
};
#endif //DESIGNPATTERN_CONCRETE_IMPLEMENTOR_H

View File

@ -0,0 +1,17 @@
//
// Created by xiemenghui on 2018/7/21.
//
#ifndef DESIGNPATTERN_IMPLEMENTOR_H
#define DESIGNPATTERN_IMPLEMENTOR_H
// 电器
class IElectricalEquipment
{
public:
virtual ~IElectricalEquipment(){}
virtual void PowerOn() = 0; // 打开
virtual void PowerOff() = 0; // 关闭
};
#endif //DESIGNPATTERN_IMPLEMENTOR_H

View File

@ -0,0 +1,53 @@
//
// Created by xiemenghui on 2018/7/21.
//
#ifndef DESIGNPATTERN_REFINED_ABSTRACTION_H
#define DESIGNPATTERN_REFINED_ABSTRACTION_H
#include "abstraction.h"
#include <iostream>
// 拉链式开关
class PullChainSwitch : public ISwitch
{
public:
PullChainSwitch(IElectricalEquipment *ee) : ISwitch(ee) {}
// 用拉链式开关打开电器
virtual void On() override
{
std::cout << "Switch on the equipment with a pull chain switch." << std::endl;
m_pEe->PowerOn();
}
// 用拉链式开关关闭电器
virtual void Off() override
{
std::cout << "Switch off the equipment with a pull chain switch." << std::endl;
m_pEe->PowerOff();
}
};
// 两位开关
class TwoPositionSwitch : public ISwitch
{
public:
TwoPositionSwitch(IElectricalEquipment *ee) : ISwitch(ee) {}
// 用两位开关打开电器
virtual void On() override
{
std::cout << "Switch on the equipment with a two-position switch." << std::endl;
m_pEe->PowerOn();
}
// 用两位开关关闭电器
virtual void Off() override {
std::cout << "Switch off the equipment with a two-position switch." << std::endl;
m_pEe->PowerOff();
}
};
#endif //DESIGNPATTERN_REFINED_ABSTRACTION_H

View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.10)
project(DesignPattern)
set(CMAKE_CXX_STANDARD 11)
add_executable(DesignPattern main.cpp AbstractFactoryPattern/product.h AbstractFactoryPattern/concrete_product.h AbstractFactoryPattern/Factory.cpp AbstractFactoryPattern/Factory.h AbstractFactoryPattern/concrete_factory.h AbstractFactoryPattern/FactoryMain.cpp AbstractFactoryPattern/FactoryMain.h SingletonPattern/Singleton.cpp SingletonPattern/Singleton.h SingletonPattern/SingletonMain.h AdapterPattern/target.h AdapterPattern/adaptee.h AdapterPattern/adapter.h AdapterPattern/AdapterMain.h BridgePattern/implementor.h BridgePattern/concrete_implementor.h BridgePattern/abstraction.h BridgePattern/refined_abstraction.h BridgePattern/BridgeMain.h BridgePattern/BridgeMain.cpp ObserverPattern/subject.h ObserverPattern/observer.h ObserverPattern/concrete_subject.h ObserverPattern/concrete_observer.h ObserverPattern/ObserverMain.cpp ObserverPattern/ObserverMain.h)

View File

@ -0,0 +1,34 @@
//
// Created by xiemenghui on 2018/7/21.
//
#include "ObserverMain.h"
void ObserverMain()
{
// 创建主题
ConcreteSubject * pSubject = new ConcreteSubject();
// 创建观察者
IObserver * pObserver1 = new ConcreteObserver("Jack Ma");
IObserver * pObserver2 = new ConcreteObserver("Pony");
// 注册观察者
pSubject->Attach(pObserver1);
pSubject->Attach(pObserver2);
// 更改价格,并通知观察者
pSubject->SetPrice(12.5);
pSubject->Notify();
// 注销一个观察者
pSubject->Detach(pObserver2);
// 再次更改状态,并通知观察者
pSubject->SetPrice(15.0);
pSubject->Notify();
SAFE_DELETE(pObserver1);
SAFE_DELETE(pObserver2);
SAFE_DELETE(pSubject);
}

View File

@ -0,0 +1,17 @@
//
// Created by xiemenghui on 2018/7/21.
//
#ifndef DESIGNPATTERN_OBSERVERMAIN_H
#define DESIGNPATTERN_OBSERVERMAIN_H
#include "concrete_subject.h"
#include "concrete_observer.h"
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if(p){delete(p); (p)=nullptr;} }
#endif
void ObserverMain();
#endif //DESIGNPATTERN_OBSERVERMAIN_H

View File

@ -0,0 +1,25 @@
//
// Created by xiemenghui on 2018/7/21.
//
#ifndef DESIGNPATTERN_CONCRETE_OBSERVER_H
#define DESIGNPATTERN_CONCRETE_OBSERVER_H
#include "observer.h"
#include <iostream>
#include <string>
class ConcreteObserver : public IObserver
{
public:
ConcreteObserver(std::string name) { m_strName = name; }
void Update(float price)
{
std::cout << m_strName << " - price" << price << "\n";
}
private:
std::string m_strName; // 名字
};
#endif //DESIGNPATTERN_CONCRETE_OBSERVER_H

View File

@ -0,0 +1,45 @@
//
// Created by xiemenghui on 2018/7/21.
//
#ifndef DESIGNPATTERN_CONCRETE_SUBJECT_H
#define DESIGNPATTERN_CONCRETE_SUBJECT_H
#include "subject.h"
#include "observer.h"
#include <iostream>
#include <list>
// 具体主题
class ConcreteSubject : public ISubject
{
public:
ConcreteSubject(){ m_fPrice = 10.0; }
void SetPrice(float price)
{
m_fPrice = price;
}
void Attach(IObserver * observer)
{
m_observers.push_back(observer);
}
void Detach(IObserver * observer)
{
m_observers.remove(observer);
}
// 通知所有观察者
void Notify()
{
std::list<IObserver *>::iterator it = m_observers.begin();
while (it != m_observers.end())
{
(*it)->Update(m_fPrice);
++it;
}
}
private:
std::list<IObserver *> m_observers; // 观察者列表
float m_fPrice; // 价格
};
#endif //DESIGNPATTERN_CONCRETE_SUBJECT_H

View File

@ -0,0 +1,15 @@
//
// Created by xiemenghui on 2018/7/21.
//
#ifndef DESIGNPATTERN_OBSERVER_H
#define DESIGNPATTERN_OBSERVER_H
// 抽象观察者
class IObserver
{
public:
virtual void Update(float price) = 0; // 更新价格
};
#endif //DESIGNPATTERN_OBSERVER_H

View File

@ -0,0 +1,18 @@
//
// Created by xiemenghui on 2018/7/21.
//
#ifndef DESIGNPATTERN_SUBJECT_H
#define DESIGNPATTERN_SUBJECT_H
class IObserver;
class ISubject
{
public:
virtual void Attach(IObserver *) = 0; // 注册观察者
virtual void Detach(IObserver *) = 0; // 注销观察者
virtual void Notify() = 0; // 通知观察者
};
#endif //DESIGNPATTERN_SUBJECT_H

1
DesignPattern/README.md Normal file
View File

@ -0,0 +1 @@
# DesignPattern

View File

@ -0,0 +1,34 @@
# 单例模式
```cpp
// 懒汉式单例模式
class Singleton
{
private:
Singleton() { }
static Singleton * pInstance;
public:
static Singleton * GetInstance()
{
if (pInstance == nullptr)
pInstance = new Singleton();
return pInstance;
}
};
// 线程安全的单例模式
class Singleton
{
private:
Singleton() { }
~Singleton() { }
Singleton(const Singleton &);
Singleton & operator = (const Singleton &);
public:
static Singleton & GetInstance()
{
static Singleton instance;
return instance;
}
};
```

View File

@ -0,0 +1,11 @@
//
// Created by xiemenghui on 2018/7/20.
//
#include <iostream>
#include "Singleton.h"
void Singleton::DoSomething()
{
std::cout << "Singleton do something\n";
}

View File

@ -0,0 +1,25 @@
//
// Created by xiemenghui on 2018/7/20.
//
#ifndef DESIGNPATTERN_SINGLETON_H
#define DESIGNPATTERN_SINGLETON_H
// 单例模式
class Singleton {
private:
Singleton(){}
~Singleton(){}
Singleton(const Singleton &);
Singleton & operator= (const Singleton &);
public:
static Singleton & GetInstance()
{
static Singleton instance;
return instance;
}
void DoSomething();
};
#endif //DESIGNPATTERN_SINGLETON_H

View File

@ -0,0 +1,15 @@
//
// Created by xiemenghui on 2018/7/20.
//
#ifndef DESIGNPATTERN_SINGLETONMAIN_H
#define DESIGNPATTERN_SINGLETONMAIN_H
#include "Singleton.h"
void SingletonMain()
{
Singleton::GetInstance().DoSomething();
}
#endif //DESIGNPATTERN_SINGLETONMAIN_H

43
DesignPattern/main.cpp Normal file
View File

@ -0,0 +1,43 @@
//
// Created by xiemenghui on 2018/7/20.
//
#include <iostream>
#include "SingletonPattern/SingletonMain.h"
#include "AbstractFactoryPattern/FactoryMain.h"
#include "AdapterPattern/AdapterMain.h"
#include "BridgePattern/BridgeMain.h"
#include "ObserverPattern/ObserverMain.h"
int main() {
std::cout << "*******************" << std::endl;
std::cout << "** Éè¼ÆģʽÀý×Ó **" << std::endl;
std::cout << "*******************" << std::endl;
std::cout << "*******************" << std::endl;
std::cout << "** µ¥Àýģʽ **" << std::endl;
std::cout << "*******************" << std::endl;
SingletonMain();
std::cout << "*******************" << std::endl;
std::cout << "** ³éÏ󹤳§Ä£Ê½ **" << std::endl;
std::cout << "*******************" << std::endl;
FactoryMain();
std::cout << "*******************" << std::endl;
std::cout << "** ÊÊÅäÆ÷ģʽ **" << std::endl;
std::cout << "*******************" << std::endl;
AdapterMain();
std::cout << "*******************" << std::endl;
std::cout << "** ÇŽÓģʽ **" << std::endl;
std::cout << "*******************" << std::endl;
BridgeMain();
std::cout << "*******************" << std::endl;
std::cout << "** ¹Û²ìÕßģʽ **" << std::endl;
std::cout << "*******************" << std::endl;
ObserverMain();
return 0;
}

View File

@ -2420,6 +2420,8 @@ ssize_t write(int fd, const void *buf, size_t count);
## 设计模式
> 各大设计模式例子参考:[CSDN专栏 . C++ 设计模式](https://blog.csdn.net/column/details/15392.html) 系列博文
### 设计模式的六大原则
* 单一职责原则SRPSingle Responsibility Principle
@ -2431,38 +2433,23 @@ ssize_t write(int fd, const void *buf, size_t count);
### 单例模式
```cpp
// 懒汉式单例模式
class Singleton
{
private:
Singleton() { }
static Singleton * pInstance;
public:
static Singleton * GetInstance()
{
if (pInstance == nullptr)
pInstance = new Singleton();
return pInstance;
}
};
[单例模式例子](DesignPattern/SingletonPattern)
// 线程安全的单例模式
class Singleton
{
private:
Singleton() { }
~Singleton() { }
Singleton(const Singleton &);
Singleton & operator = (const Singleton &);
public:
static Singleton & GetInstance()
{
static Singleton instance;
return instance;
}
};
```
### 抽象工厂模式
[抽象工厂模式例子](DesignPattern/AbstractFactoryPattern)
### 适配器模式
[适配器模式例子](DesignPattern/AdapterPattern)
### 桥接模式
[桥接模式例子](DesignPattern/BridgePattern)
### 观察者模式
[观察者模式例子](DesignPattern/ObserverPattern)
## 链接装载库