mirror of
https://github.com/huihut/interview.git
synced 2025-12-18 21:14:38 +03:00
添加设计模式及例子,包括:单例、抽象工厂、适配器、桥接、观察者模式
This commit is contained in:
25
DesignPattern/AbstractFactoryPattern/Factory.cpp
Normal file
25
DesignPattern/AbstractFactoryPattern/Factory.cpp
Normal 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;
|
||||
}
|
||||
24
DesignPattern/AbstractFactoryPattern/Factory.h
Normal file
24
DesignPattern/AbstractFactoryPattern/Factory.h
Normal 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
|
||||
46
DesignPattern/AbstractFactoryPattern/FactoryMain.cpp
Normal file
46
DesignPattern/AbstractFactoryPattern/FactoryMain.cpp
Normal 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()
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD>
|
||||
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);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>
|
||||
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);
|
||||
|
||||
// <20>µ<EFBFBD>
|
||||
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);
|
||||
}
|
||||
14
DesignPattern/AbstractFactoryPattern/FactoryMain.h
Normal file
14
DesignPattern/AbstractFactoryPattern/FactoryMain.h
Normal 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
|
||||
51
DesignPattern/AbstractFactoryPattern/concrete_factory.h
Normal file
51
DesignPattern/AbstractFactoryPattern/concrete_factory.h
Normal 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
|
||||
72
DesignPattern/AbstractFactoryPattern/concrete_product.h
Normal file
72
DesignPattern/AbstractFactoryPattern/concrete_product.h
Normal 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
|
||||
25
DesignPattern/AbstractFactoryPattern/product.h
Normal file
25
DesignPattern/AbstractFactoryPattern/product.h
Normal 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
|
||||
Reference in New Issue
Block a user