Skip to content

Commit b05c676

Browse files
committed
add source code link
1 parent 17eca36 commit b05c676

6 files changed

Lines changed: 28 additions & 13 deletions

src/design/1 design_patterns.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 设计原则
2+
title: 设计模式
33
order: 2
44
category:
55
- 架构设计
@@ -12,6 +12,8 @@ editLink: false
1212

1313
### 设计模式
1414

15+
>**设计模式是解决反复出现的问题的解决方案, 是关于如何解决某些问题的指导方针.**
16+
1517
**设计模式**是解决常见软件设计问题的通用解决方案。它们分为三大类:
1618
1. **创建型模式**:关注对象创建,如单例模式、[工厂模式](/design/6%20factory_pattern)
1719
2. **结构型模式**:关注对象组合,如[装饰器模式](/design/4%20decorator_pattern)[桥接模式](/design/5%20bridge_pattern),组合模式。

src/design/2 observer_pattern.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ head:
3131
3. **具体主题(Concrete Subject)**:实现主题接口,维护一个具体的状态。当状态改变时,会通知所有观察者。
3232
4. **具体观察者(Concrete Observer)**:实现观察者接口,负责在接收到主题的通知后进行相应的更新操作。
3333

34-
### 示例
34+
### [示例](https://github.com/hackcpp/cplusplus/blob/main/source%20code/design_pattern/decorator.cpp)
3535

3636
​下面以一个文件分割器的实现模拟观察者模式。示例中 GUI 类 `MainForm` 通过实现`IProgress` 接口监听文件分割处理的进度。
3737

src/design/3 strategy_pattern.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ head:
2525
2. **具体策略类**:实现策略接口的不同算法。
2626
3. **上下文类**:持有一个策略对象,并在运行时决定使用哪种策略。
2727

28-
### 示例
28+
### [示例](https://github.com/hackcpp/cplusplus/blob/main/source%20code/design_pattern/strategy.cpp)
2929

3030
下面是一个用C++实现的商品计算折扣的策略模式事例。这个示例模拟了不同折扣率下商品价格的计算。
3131

@@ -58,6 +58,7 @@ class SeasonalDiscount {
5858
class DiscountStrategy {
5959
public:
6060
virtual double calculate(double price) const = 0;
61+
virtual ~DiscountStrategy() {}
6162
};
6263

6364
// 具体策略

src/design/4 decorator_pattern.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ tag:
88
- 设计模式
99
- 装饰器模式
1010
editLink: false
11-
description: "结构型设计模式,装饰器模式(Decorator Pattern)"
11+
description: "结构型设计模式,装饰器模式(Decorator Pattern)通过将对象放入包含行为的特殊封装对象中,来为原始对象提供新的行为,使得我们可以在不改变原始类代码的情况下,动态地添加新的功能."
1212
star: 803
1313
sticky: 803
14+
head:
15+
- - meta
16+
- name: keywords
17+
content: 设计模式 装饰器模式 结构型设计模式 Decorator Pattern
1418
---
1519

1620
### 概述
@@ -28,7 +32,7 @@ sticky: 803
2832
3. **装饰器类(Decorator)**:实现组件接口,并持有一个组件对象的引用。这个类是装饰器模式的核心,通过在方法中调用组件对象的方法,实现功能的叠加。
2933
4. **具体装饰器(Concrete Decorator)**:继承装饰器类,具体实现要添加的功能。
3034

31-
### 示例
35+
### [示例](https://github.com/hackcpp/cplusplus/blob/main/source%20code/design_pattern/decorator.cpp)
3236

3337
@startuml 类图
3438
abstract class Stream {
@@ -79,7 +83,7 @@ class Stream {
7983

8084
class FileStream: public Stream {
8185
public:
82-
std::string read() ovverride {
86+
std::string read() override {
8387
std::cout << "FileStream::read" << std::endl;
8488
return "";
8589
}
@@ -104,7 +108,7 @@ class MemoryStream: public Stream {
104108
class BufferedStream: public Stream {
105109
public:
106110
BufferedStream(Stream* s): mSteam(s) {}
107-
virtual std::string read() ovverride {
111+
virtual std::string read() override {
108112
//..buffer
109113
return DoBuffer(mSteam->read());
110114
}

src/design/5 bridge_pattern.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ tag:
88
- 设计模式
99
- 桥接模式
1010
editLink: false
11-
description: "结构型设计模式,桥接模式(Bridge Pattern)"
11+
description: "结构型设计模式,桥接模式(Bridge Pattern)将抽象部分与实现部分分离,使它们可以独立地变化。桥接模式通过将类的功能层次与实现层次分离,使得系统更具扩展性和灵活性"
1212
star: 804
1313
sticky: 804
14+
head:
15+
- - meta
16+
- name: keywords
17+
content: 设计模式 桥接模式 结构型设计模式 Bridge Pattern
1418
---
1519

1620

@@ -27,7 +31,7 @@ sticky: 804
2731
3. **实现类接口(Implementor)**:定义实现类的接口,但不提供具体实现。实现类接口与抽象类没有继承关系。
2832
4. **具体实现类(Concrete Implementor)**:实现实现类接口,提供具体的实现逻辑。
2933

30-
### 示例
34+
### [示例](https://github.com/hackcpp/cplusplus/blob/main/source%20code/design_pattern/bridge.cpp)
3135

3236
@startuml 类图
3337
abstract class Color {

src/design/7 chain_of_responsibility_pattern.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ tag:
88
- 设计模式
99
- 责任链模式
1010
editLink: false
11-
description: "行为型设计模式,责任链模式(Chain of Responsibility Pattern)"
11+
description: "行为型设计模式,责任链模式(Chain of Responsibility Pattern)允许多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合。该模式将这些对象连成一条链,并沿着这条链传递请求,直到有对象处理它为止"
1212
sticky: 805
13+
head:
14+
- - meta
15+
- name: keywords
16+
content: 设计模式 责任链模式 行为型设计模式 Chain of Responsibility Pattern
1317
---
1418

1519
### 概述
@@ -26,7 +30,7 @@ sticky: 805
2630
- **具体处理者(Concrete Handler)**:实现了处理者接口,负责处理具体的请求。
2731
- **请求**:需要沿着责任链传递的请求。
2832

29-
### 示例
33+
### [示例](https://github.com/hackcpp/cplusplus/blob/main/source%20code/design_pattern/chain_of_responsibility.cpp)
3034

3135
下面是一个用 C++ 实现的请假流程处理的责任链模式示例。这个示例模拟了请假请求根据天数由不同级别的领导审批的场景。
3236

@@ -92,7 +96,7 @@ public:
9296
// Manager 处理类
9397
class ManagerHandler : public LeaveHandler {
9498
public:
95-
void handleRequest(int days) override {
99+
bool handleRequest(int days) override {
96100
if (days > 3 && days <= 7) {
97101
std::cout << "Manager approved " << days << " days leave." << std::endl;
98102
return true;
@@ -107,7 +111,7 @@ public:
107111
// Director 处理类
108112
class DirectorHandler : public LeaveHandler {
109113
public:
110-
void handleRequest(int days) override {
114+
bool handleRequest(int days) override {
111115
if (days > 7) {
112116
std::cout << "Director approved " << days << " days leave." << std::endl;
113117
return true;

0 commit comments

Comments
 (0)