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; Factory *pFactory = nullptr;
switch (factory) { switch (factory) {
case FACTORY_TYPE::BENZ_FACTORY: // 奔驰工厂 case FACTORY_TYPE::BENZ_FACTORY: // Benz factory
pFactory = new BenzFactory(); pFactory = new BenzFactory();
break; break;
case FACTORY_TYPE::BMW_FACTORY: // 宝马工厂 case FACTORY_TYPE::BMW_FACTORY: // BMW factory
pFactory = new BmwFactory(); pFactory = new BmwFactory();
break; break;
case FACTORY_TYPE::AUDI_FACTORY: // 奥迪工厂 case FACTORY_TYPE::AUDI_FACTORY: // Audi factory
pFactory = new AudiFactory(); pFactory = new AudiFactory();
break; break;
default: default:

View File

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

View File

@@ -10,7 +10,7 @@ using namespace std;
void FactoryMain() void FactoryMain()
{ {
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> // Benz
Factory * pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::BENZ_FACTORY); Factory * pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::BENZ_FACTORY);
ICar * pCar = pFactory->CreateCar(); ICar * pCar = pFactory->CreateCar();
IBike * pBike = pFactory->CreateBike(); IBike * pBike = pFactory->CreateBike();
@@ -22,7 +22,7 @@ void FactoryMain()
SAFE_DELETE(pBike); SAFE_DELETE(pBike);
SAFE_DELETE(pFactory); SAFE_DELETE(pFactory);
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> // BMW
pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::BMW_FACTORY); pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::BMW_FACTORY);
pCar = pFactory->CreateCar(); pCar = pFactory->CreateCar();
pBike = pFactory->CreateBike(); pBike = pFactory->CreateBike();
@@ -33,7 +33,7 @@ void FactoryMain()
SAFE_DELETE(pBike); SAFE_DELETE(pBike);
SAFE_DELETE(pFactory); SAFE_DELETE(pFactory);
// <EFBFBD>µ<EFBFBD> // Audi
pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::AUDI_FACTORY); pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::AUDI_FACTORY);
pCar = pFactory->CreateCar(); pCar = pFactory->CreateCar();
pBike = pFactory->CreateBike(); pBike = pFactory->CreateBike();

View File

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

View File

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

View File

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

View File

@@ -9,10 +9,10 @@
void AdapterMain() void AdapterMain()
{ {
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> // Create a power adapter
IRussiaSocket * pAdapter = new PowerAdapter(); IRussiaSocket * pAdapter = new PowerAdapter();
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD> // Recharge
pAdapter->Charge(); pAdapter->Charge();
SAFE_DELETE(pAdapter); SAFE_DELETE(pAdapter);

View File

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

View File

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

View File

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

View File

@@ -6,20 +6,20 @@
void BridgeMain() void BridgeMain()
{ {
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ơ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȣ<EFBFBD> // Create electrical appliances (electric lights, electric fans)
IElectricalEquipment * light = new Light(); IElectricalEquipment * light = new Light();
IElectricalEquipment * fan = new Fan(); IElectricalEquipment * fan = new Fan();
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>ء<EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD> // Create switch (pull chain switch, two-position switch)
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>غ͵<EFBFBD><EFBFBD>ƹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><EFBFBD><EFBFBD>غͷ<EFBFBD><EFBFBD>ȹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> // Associating a pull chain switch with a light and a two-position switch with a fan
ISwitch * pullChain = new PullChainSwitch(light); ISwitch * pullChain = new PullChainSwitch(light);
ISwitch * twoPosition = new TwoPositionSwitch(fan); ISwitch * twoPosition = new TwoPositionSwitch(fan);
// <EFBFBD><EFBFBD><EFBFBD>ơ<EFBFBD><EFBFBD>ص<EFBFBD> // Lights on, lights off
pullChain->On(); pullChain->On();
pullChain->Off(); pullChain->Off();
// <EFBFBD>򿪷<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>رշ<EFBFBD><EFBFBD><EFBFBD> // Turn on the fan, turn off the fan
twoPosition->On(); twoPosition->On();
twoPosition->Off(); twoPosition->Off();

View File

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

View File

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

View File

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

View File

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

View File

@@ -6,25 +6,25 @@
void ObserverMain() void ObserverMain()
{ {
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> // Create Subject
ConcreteSubject * pSubject = new ConcreteSubject(); ConcreteSubject * pSubject = new ConcreteSubject();
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>۲<EFBFBD><EFBFBD><EFBFBD> // Create Observer
IObserver * pObserver1 = new ConcreteObserver("Jack Ma"); IObserver * pObserver1 = new ConcreteObserver("Jack Ma");
IObserver * pObserver2 = new ConcreteObserver("Pony"); IObserver * pObserver2 = new ConcreteObserver("Pony");
// ע<EFBFBD><EFBFBD><EFBFBD>۲<EFBFBD><EFBFBD><EFBFBD> // Attach Observers
pSubject->Attach(pObserver1); pSubject->Attach(pObserver1);
pSubject->Attach(pObserver2); pSubject->Attach(pObserver2);
// <EFBFBD><EFBFBD><EFBFBD>ļ۸񣬲<EFBFBD>֪ͨ<EFBFBD>۲<EFBFBD><EFBFBD><EFBFBD> // Change the price and notify the observer
pSubject->SetPrice(12.5); pSubject->SetPrice(12.5);
pSubject->Notify(); pSubject->Notify();
// ע<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>۲<EFBFBD><EFBFBD><EFBFBD> // Detach Observers
pSubject->Detach(pObserver2); pSubject->Detach(pObserver2);
// <EFBFBD>ٴθ<EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>֪ͨ<EFBFBD>۲<EFBFBD><EFBFBD><EFBFBD> // Change the state again and notify the observer
pSubject->SetPrice(15.0); pSubject->SetPrice(15.0);
pSubject->Notify(); pSubject->Notify();

View File

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

View File

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

View File

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

View File

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

View File

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