-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5390a41
commit 40ca392
Showing
127 changed files
with
3,731 additions
and
2 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
05~设计模式/99~参考资料/liu-jianhao~Cpp-Design-Patterns/Abstract Factory/EmployeeDAO1.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
|
||
class EmployeeDAO{ | ||
|
||
public: | ||
vector<EmployeeDO> GetEmployees(){ | ||
SqlConnection* connection = | ||
new SqlConnection(); | ||
connection->ConnectionString = "..."; | ||
|
||
SqlCommand* command = | ||
new SqlCommand(); | ||
command->CommandText="..."; | ||
command->SetConnection(connection); | ||
|
||
SqlDataReader* reader = command->ExecuteReader(); | ||
while (reader->Read()){ | ||
|
||
} | ||
|
||
} | ||
}; |
92 changes: 92 additions & 0 deletions
92
05~设计模式/99~参考资料/liu-jianhao~Cpp-Design-Patterns/Abstract Factory/EmployeeDAO2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
|
||
//数据库访问有关的基类 | ||
class IDBConnection{ | ||
|
||
}; | ||
class IDBConnectionFactory{ | ||
public: | ||
virtual IDBConnection* CreateDBConnection()=0; | ||
}; | ||
|
||
|
||
class IDBCommand{ | ||
|
||
}; | ||
class IDBCommandFactory{ | ||
public: | ||
virtual IDBCommand* CreateDBCommand()=0; | ||
}; | ||
|
||
|
||
class IDataReader{ | ||
|
||
}; | ||
class IDataReaderFactory{ | ||
public: | ||
virtual IDataReader* CreateDataReader()=0; | ||
}; | ||
|
||
|
||
//支持SQL Server | ||
class SqlConnection: public IDBConnection{ | ||
|
||
}; | ||
class SqlConnectionFactory:public IDBConnectionFactory{ | ||
|
||
}; | ||
|
||
|
||
class SqlCommand: public IDBCommand{ | ||
|
||
}; | ||
class SqlCommandFactory:public IDBCommandFactory{ | ||
|
||
}; | ||
|
||
|
||
class SqlDataReader: public IDataReader{ | ||
|
||
}; | ||
class SqlDataReaderFactory:public IDataReaderFactory{ | ||
|
||
}; | ||
|
||
//支持Oracle | ||
class OracleConnection: public IDBConnection{ | ||
|
||
}; | ||
|
||
class OracleCommand: public IDBCommand{ | ||
|
||
}; | ||
|
||
class OracleDataReader: public IDataReader{ | ||
|
||
}; | ||
|
||
|
||
|
||
class EmployeeDAO{ | ||
IDBConnectionFactory* dbConnectionFactory; | ||
IDBCommandFactory* dbCommandFactory; | ||
IDataReaderFactory* dataReaderFactory; | ||
|
||
|
||
public: | ||
vector<EmployeeDO> GetEmployees(){ | ||
IDBConnection* connection = | ||
dbConnectionFactory->CreateDBConnection(); | ||
connection->ConnectionString("..."); | ||
|
||
IDBCommand* command = | ||
dbCommandFactory->CreateDBCommand(); | ||
command->CommandText("..."); | ||
command->SetConnection(connection); //关联性 | ||
|
||
IDBDataReader* reader = command->ExecuteReader(); //关联性 | ||
while (reader->Read()){ | ||
|
||
} | ||
|
||
} | ||
}; |
80 changes: 80 additions & 0 deletions
80
05~设计模式/99~参考资料/liu-jianhao~Cpp-Design-Patterns/Abstract Factory/EmployeeDAO3.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
|
||
//数据库访问有关的基类 | ||
class IDBConnection{ | ||
|
||
}; | ||
|
||
class IDBCommand{ | ||
|
||
}; | ||
|
||
class IDataReader{ | ||
|
||
}; | ||
|
||
|
||
class IDBFactory{ | ||
public: | ||
virtual IDBConnection* CreateDBConnection()=0; | ||
virtual IDBCommand* CreateDBCommand()=0; | ||
virtual IDataReader* CreateDataReader()=0; | ||
|
||
}; | ||
|
||
|
||
//支持SQL Server | ||
class SqlConnection: public IDBConnection{ | ||
|
||
}; | ||
class SqlCommand: public IDBCommand{ | ||
|
||
}; | ||
class SqlDataReader: public IDataReader{ | ||
|
||
}; | ||
|
||
|
||
class SqlDBFactory:public IDBFactory{ | ||
public: | ||
virtual IDBConnection* CreateDBConnection()=0; | ||
virtual IDBCommand* CreateDBCommand()=0; | ||
virtual IDataReader* CreateDataReader()=0; | ||
|
||
}; | ||
|
||
//支持Oracle | ||
class OracleConnection: public IDBConnection{ | ||
|
||
}; | ||
|
||
class OracleCommand: public IDBCommand{ | ||
|
||
}; | ||
|
||
class OracleDataReader: public IDataReader{ | ||
|
||
}; | ||
|
||
|
||
|
||
class EmployeeDAO{ | ||
IDBFactory* dbFactory; | ||
|
||
public: | ||
vector<EmployeeDO> GetEmployees(){ | ||
IDBConnection* connection = | ||
dbFactory->CreateDBConnection(); | ||
connection->ConnectionString("..."); | ||
|
||
IDBCommand* command = | ||
dbFactory->CreateDBCommand(); | ||
command->CommandText("..."); | ||
command->SetConnection(connection); //关联性 | ||
|
||
IDBDataReader* reader = command->ExecuteReader(); //关联性 | ||
while (reader->Read()){ | ||
|
||
} | ||
|
||
} | ||
}; |
14 changes: 14 additions & 0 deletions
14
05~设计模式/99~参考资料/liu-jianhao~Cpp-Design-Patterns/Abstract Factory/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Abstract Factory | ||
|
||
## 动机(motivation) | ||
+ 在软件系统中,经常面临着“一系列相互依赖的对象工作”;同时,由于需求的变化,往往存在更多系列对象的创建工作。 | ||
+ 如何应对这种变化?如何绕过常规的对象创建方法(new),提供一种“封装机制”来避免客户程序和这种“多系列具体对象创建工作”的紧耦合。 | ||
|
||
## 模式定义 | ||
提供一个接口,让该接口负责创建一系列”相关或者相互依赖的对象“,无需指定它们具体的类。 | ||
——《设计模式》GoF | ||
|
||
## 要点总结 | ||
+ 如果没有应对”多系列对象创建“的需求变化,则没有必要使用Abstract Factory模式,这时候使用简单的工厂即可。 | ||
+ ”系列对象“指的是在某一个特定系列的对象之间有相互依赖、或作用的关系。不同系列的对象之间不能相互依赖。 | ||
+ Abstract Factory模式主要在于应用”新系列“的需求变动。其缺点在与难以应对”新对象“的需求变动。 |
76 changes: 76 additions & 0 deletions
76
05~设计模式/99~参考资料/liu-jianhao~Cpp-Design-Patterns/Adapter/Adapter.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//目标接口(新接口) | ||
class ITarget{ | ||
public: | ||
virtual void process()=0; | ||
}; | ||
|
||
//遗留接口(老接口) | ||
class IAdaptee{ | ||
public: | ||
virtual void foo(int data)=0; | ||
virtual int bar()=0; | ||
}; | ||
|
||
//遗留类型 | ||
class OldClass: public IAdaptee{ | ||
//.... | ||
}; | ||
|
||
//对象适配器 | ||
class Adapter: public ITarget{ //继承 | ||
protected: | ||
IAdaptee* pAdaptee;//组合 | ||
|
||
public: | ||
|
||
Adapter(IAdaptee* pAdaptee){ | ||
this->pAdaptee=pAdaptee; | ||
} | ||
|
||
virtual void process(){ | ||
int data=pAdaptee->bar(); | ||
pAdaptee->foo(data); | ||
|
||
} | ||
|
||
|
||
}; | ||
|
||
|
||
//类适配器 | ||
class Adapter: public ITarget, | ||
protected OldClass{ //多继承 | ||
|
||
|
||
} | ||
|
||
|
||
int main(){ | ||
IAdaptee* pAdaptee=new OldClass(); | ||
|
||
|
||
ITarget* pTarget=new Adapter(pAdaptee); | ||
pTarget->process(); | ||
|
||
|
||
} | ||
|
||
|
||
class stack{ | ||
deqeue container; | ||
|
||
}; | ||
|
||
class queue{ | ||
deqeue container; | ||
|
||
}; | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
12 changes: 12 additions & 0 deletions
12
05~设计模式/99~参考资料/liu-jianhao~Cpp-Design-Patterns/Adapter/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Adapter | ||
|
||
## 动机(Motivation) | ||
+ 由于应用环境的变化,常常需要将”一些现存的对象“放在新的环境中应用,但是新环境要求的接口是这些现存对象所不满足。 | ||
+ 如何应对这些”迁移的变化“? | ||
|
||
## 模式定义 | ||
将一个类的接口转换成客户希望的另一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。 | ||
——《设计模式》GoF | ||
|
||
## 要点总结 | ||
+ 在遗留代码复用、类库迁移等方面有用 |
14 changes: 14 additions & 0 deletions
14
05~设计模式/99~参考资料/liu-jianhao~Cpp-Design-Patterns/Bridge/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Bridge | ||
|
||
## 动机(Motivation) | ||
+ 由于某些类型的固有的实现逻辑,使得它们具有两个变化的维度,乃至多个纬度的变化。 | ||
+ 如何应对这种“多维度的变化”?如何利用面向对象技术来使得类型可以轻松地沿着两个乃至多个方向变化,而不引入额外的复杂度? | ||
|
||
## 模式定义 | ||
将抽象部分(业务功能)与实现部分(平台实现)分离,使它们都可以独立地变化。 | ||
——《设计模式》GoF | ||
|
||
## 要点总结 | ||
+ Bridge模式使用“对象间的组合关系”解耦了抽象和实现之间固有的绑定关系,使得抽象和实现可以沿着各自的维度来变化。所谓抽象和实现沿着各自纬度的变化,即“子类化”它们。 | ||
+ Bridge模式有时候类似于多继承方案,但是多继承方案往往违背单一职责原则(即一个类只有一个变化的原因),复用性比较差。Bridge模式是比多继承方案更好的解决方法。 | ||
+ Bridge模式的应用一般在“两个非常强的变化维度”,有时一个类也有多于两个的变化维度,这时可以使用Bridge的扩展模式。 |
Binary file not shown.
Binary file not shown.
17 changes: 17 additions & 0 deletions
17
05~设计模式/99~参考资料/liu-jianhao~Cpp-Design-Patterns/Builder/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Builder | ||
|
||
## 动机(Motivation) | ||
+ 在软件系统中,有时候面临着“一个复杂对象”的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这 | ||
个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定。 | ||
+ 如何应对这种变化?如何提供一种“封装机制”来隔离出“复杂对象的各个部分”的变化,从而保持系统中的“稳定构建算法”不随着需求改变而改变? | ||
|
||
|
||
## 模式定义 | ||
将一个复杂对象的构建与其表示相分离,使得同样的构建过程(稳定)可以创建不同的表示(变化)。 | ||
——《设计模式》GoF | ||
|
||
|
||
## 要点总结 | ||
+ Builder 模式主要用于“分步骤构建一个复杂的对象”。在这其中“分步骤”是一个稳定的算法,而复杂对象的各个部分则经常变化。 | ||
+ 变化点在哪里,封装哪里—— Builder模式主要在于应对“复杂对象各个部分”的频繁需求变动。其缺点在于难以应对“分步骤构建算法”的需求变动。 | ||
+ 在Builder模式中,要注意不同语言中构造器内调用虚函数的差别(C++(构造函数中不可以调用虚函数) vs. C#)。 |
Oops, something went wrong.