Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
你比较机灵❤️
interview
提交
3a9e2123
I
interview
项目概览
你比较机灵❤️
/
interview
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
interview
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
3a9e2123
编写于
12月 16, 2020
作者:
辉哈
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Design mode code comments are changed to English
https://github.com/huihut/interview/pull/73
上级
8fe5157a
变更
21
隐藏空白更改
内联
并排
Showing
21 changed file
with
78 addition
and
78 deletion
+78
-78
DesignPattern/AbstractFactoryPattern/Factory.cpp
DesignPattern/AbstractFactoryPattern/Factory.cpp
+3
-3
DesignPattern/AbstractFactoryPattern/Factory.h
DesignPattern/AbstractFactoryPattern/Factory.h
+7
-7
DesignPattern/AbstractFactoryPattern/FactoryMain.cpp
DesignPattern/AbstractFactoryPattern/FactoryMain.cpp
+3
-3
DesignPattern/AbstractFactoryPattern/concrete_factory.h
DesignPattern/AbstractFactoryPattern/concrete_factory.h
+3
-3
DesignPattern/AbstractFactoryPattern/concrete_product.h
DesignPattern/AbstractFactoryPattern/concrete_product.h
+8
-8
DesignPattern/AbstractFactoryPattern/product.h
DesignPattern/AbstractFactoryPattern/product.h
+2
-2
DesignPattern/AdapterPattern/AdapterMain.h
DesignPattern/AdapterPattern/AdapterMain.h
+2
-2
DesignPattern/AdapterPattern/adaptee.h
DesignPattern/AdapterPattern/adaptee.h
+1
-1
DesignPattern/AdapterPattern/adapter.h
DesignPattern/AdapterPattern/adapter.h
+3
-3
DesignPattern/AdapterPattern/target.h
DesignPattern/AdapterPattern/target.h
+2
-2
DesignPattern/BridgePattern/BridgeMain.cpp
DesignPattern/BridgePattern/BridgeMain.cpp
+5
-5
DesignPattern/BridgePattern/abstraction.h
DesignPattern/BridgePattern/abstraction.h
+3
-3
DesignPattern/BridgePattern/concrete_implementor.h
DesignPattern/BridgePattern/concrete_implementor.h
+6
-6
DesignPattern/BridgePattern/implementor.h
DesignPattern/BridgePattern/implementor.h
+3
-3
DesignPattern/BridgePattern/refined_abstraction.h
DesignPattern/BridgePattern/refined_abstraction.h
+10
-10
DesignPattern/ObserverPattern/ObserverMain.cpp
DesignPattern/ObserverPattern/ObserverMain.cpp
+6
-6
DesignPattern/ObserverPattern/concrete_observer.h
DesignPattern/ObserverPattern/concrete_observer.h
+1
-1
DesignPattern/ObserverPattern/concrete_subject.h
DesignPattern/ObserverPattern/concrete_subject.h
+4
-4
DesignPattern/ObserverPattern/observer.h
DesignPattern/ObserverPattern/observer.h
+2
-2
DesignPattern/ObserverPattern/subject.h
DesignPattern/ObserverPattern/subject.h
+3
-3
DesignPattern/SingletonPattern/Singleton.h
DesignPattern/SingletonPattern/Singleton.h
+1
-1
未找到文件。
DesignPattern/AbstractFactoryPattern/Factory.cpp
浏览文件 @
3a9e2123
...
...
@@ -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:
...
...
DesignPattern/AbstractFactoryPattern/Factory.h
浏览文件 @
3a9e2123
...
...
@@ -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
DesignPattern/AbstractFactoryPattern/FactoryMain.cpp
浏览文件 @
3a9e2123
...
...
@@ -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
();
...
...
DesignPattern/AbstractFactoryPattern/concrete_factory.h
浏览文件 @
3a9e2123
...
...
@@ -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:
...
...
DesignPattern/AbstractFactoryPattern/concrete_product.h
浏览文件 @
3a9e2123
...
...
@@ -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:
...
...
DesignPattern/AbstractFactoryPattern/product.h
浏览文件 @
3a9e2123
...
...
@@ -8,14 +8,14 @@
#include <string>
using
std
::
string
;
//
汽车接口
//
Car Interface
class
ICar
{
public:
virtual
string
Name
()
=
0
;
};
//
自行车接口
//
Bike Interface
class
IBike
{
public:
...
...
DesignPattern/AdapterPattern/AdapterMain.h
浏览文件 @
3a9e2123
...
...
@@ -9,10 +9,10 @@
void
AdapterMain
()
{
//
//
Create a power adapter
IRussiaSocket
*
pAdapter
=
new
PowerAdapter
();
//
//
Recharge
pAdapter
->
Charge
();
SAFE_DELETE
(
pAdapter
);
...
...
DesignPattern/AdapterPattern/adaptee.h
浏览文件 @
3a9e2123
...
...
@@ -7,7 +7,7 @@
#include <iostream>
//
自带的充电器(两脚扁型)
//
Built-in charger (two-leg flat type)
class
OwnCharger
{
public:
...
...
DesignPattern/AdapterPattern/adapter.h
浏览文件 @
3a9e2123
...
...
@@ -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
;
};
...
...
DesignPattern/AdapterPattern/target.h
浏览文件 @
3a9e2123
...
...
@@ -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
;
};
...
...
DesignPattern/BridgePattern/BridgeMain.cpp
浏览文件 @
3a9e2123
...
...
@@ -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
();
...
...
DesignPattern/BridgePattern/abstraction.h
浏览文件 @
3a9e2123
...
...
@@ -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
;
...
...
DesignPattern/BridgePattern/concrete_implementor.h
浏览文件 @
3a9e2123
...
...
@@ -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
;
...
...
DesignPattern/BridgePattern/implementor.h
浏览文件 @
3a9e2123
...
...
@@ -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
DesignPattern/BridgePattern/refined_abstraction.h
浏览文件 @
3a9e2123
...
...
@@ -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
();
}
};
...
...
DesignPattern/ObserverPattern/ObserverMain.cpp
浏览文件 @
3a9e2123
...
...
@@ -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
();
...
...
DesignPattern/ObserverPattern/concrete_observer.h
浏览文件 @
3a9e2123
...
...
@@ -19,7 +19,7 @@ public:
}
private:
std
::
string
m_strName
;
//
名字
std
::
string
m_strName
;
//
name
};
#endif //DESIGNPATTERN_CONCRETE_OBSERVER_H
DesignPattern/ObserverPattern/concrete_subject.h
浏览文件 @
3a9e2123
...
...
@@ -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
DesignPattern/ObserverPattern/observer.h
浏览文件 @
3a9e2123
...
...
@@ -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
DesignPattern/ObserverPattern/subject.h
浏览文件 @
3a9e2123
...
...
@@ -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
DesignPattern/SingletonPattern/Singleton.h
浏览文件 @
3a9e2123
...
...
@@ -5,7 +5,7 @@
#ifndef DESIGNPATTERN_SINGLETON_H
#define DESIGNPATTERN_SINGLETON_H
//
单例模式
//
Singleton mode
class
Singleton
{
private:
Singleton
(){}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录