Skip to content

Commit

Permalink
Merge pull request huihut#15 from huihut/master
Browse files Browse the repository at this point in the history
归并排序空间复杂度有误
  • Loading branch information
huihut authored Sep 17, 2018
2 parents 2ce83e2 + 153ad57 commit 71605ec
Show file tree
Hide file tree
Showing 37 changed files with 1,655 additions and 220 deletions.
25 changes: 25 additions & 0 deletions DesignPattern/AbstractFactoryPattern/Factory.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 24 additions & 0 deletions DesignPattern/AbstractFactoryPattern/Factory.h
Original file line number Diff line number Diff line change
@@ -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 changes: 46 additions & 0 deletions DesignPattern/AbstractFactoryPattern/FactoryMain.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
14 changes: 14 additions & 0 deletions DesignPattern/AbstractFactoryPattern/FactoryMain.h
Original file line number Diff line number Diff line change
@@ -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 changes: 51 additions & 0 deletions DesignPattern/AbstractFactoryPattern/concrete_factory.h
Original file line number Diff line number Diff line change
@@ -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 changes: 72 additions & 0 deletions DesignPattern/AbstractFactoryPattern/concrete_product.h
Original file line number Diff line number Diff line change
@@ -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 changes: 25 additions & 0 deletions DesignPattern/AbstractFactoryPattern/product.h
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions DesignPattern/AdapterPattern/AdapterMain.h
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions DesignPattern/AdapterPattern/adaptee.h
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions DesignPattern/AdapterPattern/adapter.h
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions DesignPattern/AdapterPattern/target.h
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions DesignPattern/BridgePattern/BridgeMain.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
Loading

0 comments on commit 71605ec

Please sign in to comment.