Design mode code comments are changed to English

https://github.com/huihut/interview/pull/73
This commit is contained in:
huihut 2020-12-16 16:07:24 +08:00
parent 8fe5157ae3
commit 3a9e2123aa
21 changed files with 78 additions and 78 deletions

View File

@ -9,13 +9,13 @@ Factory* Factory::CreateFactory(FACTORY_TYPE factory)
{
Factory *pFactory = nullptr;
switch (factory) {
case FACTORY_TYPE::BENZ_FACTORY: // 奔驰工厂
case FACTORY_TYPE::BENZ_FACTORY: // Benz factory
pFactory = new BenzFactory();
break;
case FACTORY_TYPE::BMW_FACTORY: // 宝马工厂
case FACTORY_TYPE::BMW_FACTORY: // BMW factory
pFactory = new BmwFactory();
break;
case FACTORY_TYPE::AUDI_FACTORY: // 奥迪工厂
case FACTORY_TYPE::AUDI_FACTORY: // Audi factory
pFactory = new AudiFactory();
break;
default:

View File

@ -7,18 +7,18 @@
#include "product.h"
// 抽象工厂模式
// Abstract factory pattern
class Factory {
public:
enum FACTORY_TYPE {
BENZ_FACTORY, // 奔驰工厂
BMW_FACTORY, // 宝马工厂
AUDI_FACTORY // 奥迪工厂
BENZ_FACTORY, // Benz factory
BMW_FACTORY, // BMW factory
AUDI_FACTORY // Audi factory
};
virtual ICar* CreateCar() = 0; // 生产汽车
virtual IBike* CreateBike() = 0; // 生产自行车
static Factory * CreateFactory(FACTORY_TYPE factory); // 创建工厂
virtual ICar* CreateCar() = 0; // Production car
virtual IBike* CreateBike() = 0; // Production bicycle
static Factory * CreateFactory(FACTORY_TYPE factory); // Create factory
};
#endif //DESIGNPATTERN_FACTORY_H

View File

@ -10,7 +10,7 @@ using namespace std;
void FactoryMain()
{
// ±Ό³Ϋ
// Benz
Factory * pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::BENZ_FACTORY);
ICar * pCar = pFactory->CreateCar();
IBike * pBike = pFactory->CreateBike();
@ -22,7 +22,7 @@ void FactoryMain()
SAFE_DELETE(pBike);
SAFE_DELETE(pFactory);
// ±¦Βν
// BMW
pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::BMW_FACTORY);
pCar = pFactory->CreateCar();
pBike = pFactory->CreateBike();
@ -33,7 +33,7 @@ void FactoryMain()
SAFE_DELETE(pBike);
SAFE_DELETE(pFactory);
// °Β΅Ο
// Audi
pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::AUDI_FACTORY);
pCar = pFactory->CreateCar();
pBike = pFactory->CreateBike();

View File

@ -8,7 +8,7 @@
#include "Factory.h"
#include "concrete_product.h"
// 奔驰工厂
// Benz factory
class BenzFactory : public Factory
{
public:
@ -22,7 +22,7 @@ public:
}
};
// 宝马工厂
// BMW factory
class BmwFactory : public Factory
{
public:
@ -35,7 +35,7 @@ public:
}
};
// 奥迪工厂
// Audi factory
class AudiFactory : public Factory
{
public:

View File

@ -7,8 +7,8 @@
#include "product.h"
/********** 汽车 **********/
// 奔驰
/********** Car **********/
// Benz
class BenzCar : public ICar
{
public:
@ -18,7 +18,7 @@ public:
}
};
// 宝马
// BMW
class BmwCar : public ICar
{
public:
@ -28,7 +28,7 @@ public:
}
};
// 奥迪
// Audi
class AudiCar : public ICar
{
public:
@ -38,8 +38,8 @@ public:
}
};
/********** 自行车 **********/
// 奔驰
/********** Bicycle **********/
// Benz
class BenzBike : public IBike
{
public:
@ -49,7 +49,7 @@ public:
}
};
// 宝马
// BMW
class BmwBike : public IBike
{
public:
@ -59,7 +59,7 @@ public:
}
};
// 奥迪
// Audi
class AudiBike : public IBike
{
public:

View File

@ -8,14 +8,14 @@
#include <string>
using std::string;
// 汽车接口
// Car Interface
class ICar
{
public:
virtual string Name() = 0;
};
// 自行车接口
// Bike Interface
class IBike
{
public:

View File

@ -9,10 +9,10 @@
void AdapterMain()
{
// 创建适配器
// Create a power adapter
IRussiaSocket * pAdapter = new PowerAdapter();
// 充电
// Recharge
pAdapter->Charge();
SAFE_DELETE(pAdapter);

View File

@ -7,7 +7,7 @@
#include <iostream>
// 自带的充电器(两脚扁型)
// Built-in charger (two-leg flat type)
class OwnCharger
{
public:

View File

@ -12,7 +12,7 @@
#define SAFE_DELETE(p) { if(p){delete(p); (p)=NULL;} }
#endif
// 电源适配器
// Power Adapter
class PowerAdapter : public IRussiaSocket
{
public:
@ -23,11 +23,11 @@ public:
}
void Charge()
{
// 使用自带的充电器(两脚扁形)充电
// Use the built-in charger (two-pin flat) to charge
m_pCharger->ChargeWithFeetFlat();
}
private:
// 持有需要被适配的接口对象(自带的充电器)
// Hold the interface object that needs to be adapted (the built-in charger)
OwnCharger* m_pCharger;
};

View File

@ -5,11 +5,11 @@
#ifndef DESIGNPATTERN_TARGET_H
#define DESIGNPATTERN_TARGET_H
// 俄罗斯提供的插座
// Sockets provided by Russia
class IRussiaSocket
{
public:
// 使用双脚圆形充电(暂不实现)
// Use both feet to charge in a round shape (not implemented yet)
virtual void Charge() = 0;
};

View File

@ -6,20 +6,20 @@
void BridgeMain()
{
// 创建电器(电灯、电风扇)
// Create electrical appliances (electric lights, electric fans)
IElectricalEquipment * light = new Light();
IElectricalEquipment * fan = new Fan();
// 创建开关(拉链式开关、两位开关)
// 将拉链式开关和电灯关联起来,两位开关和风扇关联起来
// Create switch (pull chain switch, two-position switch)
// Associating a pull chain switch with a light and a two-position switch with a fan
ISwitch * pullChain = new PullChainSwitch(light);
ISwitch * twoPosition = new TwoPositionSwitch(fan);
// 开灯、关灯
// Lights on, lights off
pullChain->On();
pullChain->Off();
// 打开风扇、关闭风扇
// Turn on the fan, turn off the fan
twoPosition->On();
twoPosition->Off();

View File

@ -7,14 +7,14 @@
#include "implementor.h"
// 开关
// Switch
class ISwitch
{
public:
ISwitch(IElectricalEquipment *ee){ m_pEe = ee; }
virtual ~ISwitch(){}
virtual void On() = 0; // 打开电器
virtual void Off() = 0; // 关闭电器
virtual void On() = 0; // Turn on the appliance
virtual void Off() = 0; // Turn off the appliance
protected:
IElectricalEquipment * m_pEe;

View File

@ -8,32 +8,32 @@
#include "implementor.h"
#include <iostream>
// 电灯
// Electric lights
class Light : public IElectricalEquipment
{
public:
// 开灯
// Turn on the lights
virtual void PowerOn() override
{
std::cout << "Light is on." << std::endl;
}
// 关灯
// Turn off the lights
virtual void PowerOff() override
{
std::cout << "Light is off." << std::endl;
}
};
// 风扇
// Electric Fan
class Fan : public IElectricalEquipment
{
public:
// 打开风扇
// Turn on the fan
virtual void PowerOn() override
{
std::cout << "Fan is on." << std::endl;
}
//关闭风扇
// Turn off the fan
virtual void PowerOff() override
{
std::cout << "Fan is off." << std::endl;

View File

@ -5,13 +5,13 @@
#ifndef DESIGNPATTERN_IMPLEMENTOR_H
#define DESIGNPATTERN_IMPLEMENTOR_H
// 电器
// Electric equipment
class IElectricalEquipment
{
public:
virtual ~IElectricalEquipment(){}
virtual void PowerOn() = 0; // 打开
virtual void PowerOff() = 0; // 关闭
virtual void PowerOn() = 0;
virtual void PowerOff() = 0;
};
#endif //DESIGNPATTERN_IMPLEMENTOR_H

View File

@ -8,43 +8,43 @@
#include "abstraction.h"
#include <iostream>
// 拉链式开关
// Zipper switch
class PullChainSwitch : public ISwitch
{
public:
PullChainSwitch(IElectricalEquipment *ee) : ISwitch(ee) {}
// 用拉链式开关打开电器
// Turn on the equipment with a zipper switch
virtual void On() override
{
std::cout << "Switch on the equipment with a pull chain switch." << std::endl;
std::cout << "Turn on the equipment with a zipper switch." << std::endl;
m_pEe->PowerOn();
}
// 用拉链式开关关闭电器
// Turn off the equipment with a zipper switch
virtual void Off() override
{
std::cout << "Switch off the equipment with a pull chain switch." << std::endl;
std::cout << "Turn off the equipment with a zipper switch." << std::endl;
m_pEe->PowerOff();
}
};
// 两位开关
// Two-position switch
class TwoPositionSwitch : public ISwitch
{
public:
TwoPositionSwitch(IElectricalEquipment *ee) : ISwitch(ee) {}
// 用两位开关打开电器
// Turn on the equipment with a two-position switch
virtual void On() override
{
std::cout << "Switch on the equipment with a two-position switch." << std::endl;
std::cout << "Turn on the equipment with a two-position switch." << std::endl;
m_pEe->PowerOn();
}
// 用两位开关关闭电器
// Turn off the equipment with a two-position switch
virtual void Off() override {
std::cout << "Switch off the equipment with a two-position switch." << std::endl;
std::cout << "Turn off the equipment with a two-position switch." << std::endl;
m_pEe->PowerOff();
}
};

View File

@ -6,25 +6,25 @@
void ObserverMain()
{
// 创建主题
// Create Subject
ConcreteSubject * pSubject = new ConcreteSubject();
// 创建观察者
// Create Observer
IObserver * pObserver1 = new ConcreteObserver("Jack Ma");
IObserver * pObserver2 = new ConcreteObserver("Pony");
// 注册观察者
// Attach Observers
pSubject->Attach(pObserver1);
pSubject->Attach(pObserver2);
// 更改价格,并通知观察者
// Change the price and notify the observer
pSubject->SetPrice(12.5);
pSubject->Notify();
// 注销一个观察者
// Detach Observers
pSubject->Detach(pObserver2);
// 再次更改状态,并通知观察者
// Change the state again and notify the observer
pSubject->SetPrice(15.0);
pSubject->Notify();

View File

@ -19,7 +19,7 @@ public:
}
private:
std::string m_strName; // 名字
std::string m_strName; // name
};
#endif //DESIGNPATTERN_CONCRETE_OBSERVER_H

View File

@ -10,7 +10,7 @@
#include <iostream>
#include <list>
// 具体主题
// Specific Subject
class ConcreteSubject : public ISubject
{
public:
@ -27,7 +27,7 @@ public:
{
m_observers.remove(observer);
}
// 通知所有观察者
// Notify all observers
void Notify()
{
std::list<IObserver *>::iterator it = m_observers.begin();
@ -38,8 +38,8 @@ public:
}
}
private:
std::list<IObserver *> m_observers; // 观察者列表
float m_fPrice; // 价格
std::list<IObserver *> m_observers; // Observer list
float m_fPrice; // Price
};
#endif //DESIGNPATTERN_CONCRETE_SUBJECT_H

View File

@ -5,11 +5,11 @@
#ifndef DESIGNPATTERN_OBSERVER_H
#define DESIGNPATTERN_OBSERVER_H
// 抽象观察者
// Abstract observer
class IObserver
{
public:
virtual void Update(float price) = 0; // 更新价格
virtual void Update(float price) = 0; // Update price
};
#endif //DESIGNPATTERN_OBSERVER_H

View File

@ -10,9 +10,9 @@ class IObserver;
class ISubject
{
public:
virtual void Attach(IObserver *) = 0; // 注册观察者
virtual void Detach(IObserver *) = 0; // 注销观察者
virtual void Notify() = 0; // 通知观察者
virtual void Attach(IObserver *) = 0; // Attach observer
virtual void Detach(IObserver *) = 0; // Detach observer
virtual void Notify() = 0; // Notify observer
};
#endif //DESIGNPATTERN_SUBJECT_H

View File

@ -5,7 +5,7 @@
#ifndef DESIGNPATTERN_SINGLETON_H
#define DESIGNPATTERN_SINGLETON_H
// 单例模式
// Singleton mode
class Singleton {
private:
Singleton(){}