File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11---
2- title : 设计原则
2+ title : 设计模式
33order : 2
44category :
55 - 架构设计
@@ -12,6 +12,8 @@ editLink: false
1212
1313### 设计模式
1414
15+ > ** 设计模式是解决反复出现的问题的解决方案, 是关于如何解决某些问题的指导方针.**
16+
1517** 设计模式** 是解决常见软件设计问题的通用解决方案。它们分为三大类:
16181 . ** 创建型模式** :关注对象创建,如单例模式、[ 工厂模式] ( /design/6%20factory_pattern ) 。
17192 . ** 结构型模式** :关注对象组合,如[ 装饰器模式] ( /design/4%20decorator_pattern ) 、[ 桥接模式] ( /design/5%20bridge_pattern ) ,组合模式。
Original file line number Diff line number Diff line change 31313 . ** 具体主题(Concrete Subject)** :实现主题接口,维护一个具体的状态。当状态改变时,会通知所有观察者。
32324 . ** 具体观察者(Concrete Observer)** :实现观察者接口,负责在接收到主题的通知后进行相应的更新操作。
3333
34- ### 示例
34+ ### [ 示例 ] ( https://github.com/hackcpp/cplusplus/blob/main/source%20code/design_pattern/decorator.cpp )
3535
3636下面以一个文件分割器的实现模拟观察者模式。示例中 GUI 类 ` MainForm ` 通过实现` IProgress ` 接口监听文件分割处理的进度。
3737
Original file line number Diff line number Diff line change 25252 . ** 具体策略类** :实现策略接口的不同算法。
26263 . ** 上下文类** :持有一个策略对象,并在运行时决定使用哪种策略。
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 {
5858class DiscountStrategy {
5959public:
6060 virtual double calculate(double price) const = 0;
61+ virtual ~ DiscountStrategy() {}
6162};
6263
6364// 具体策略
Original file line number Diff line number Diff line change 88 - 设计模式
99 - 装饰器模式
1010editLink : false
11- description : " 结构型设计模式,装饰器模式(Decorator Pattern)"
11+ description : " 结构型设计模式,装饰器模式(Decorator Pattern)通过将对象放入包含行为的特殊封装对象中,来为原始对象提供新的行为,使得我们可以在不改变原始类代码的情况下,动态地添加新的功能. "
1212star : 803
1313sticky : 803
14+ head :
15+ - - meta
16+ - name : keywords
17+ content : 设计模式 装饰器模式 结构型设计模式 Decorator Pattern
1418---
1519
1620### 概述
@@ -28,7 +32,7 @@ sticky: 803
28323 . ** 装饰器类(Decorator)** :实现组件接口,并持有一个组件对象的引用。这个类是装饰器模式的核心,通过在方法中调用组件对象的方法,实现功能的叠加。
29334 . ** 具体装饰器(Concrete Decorator)** :继承装饰器类,具体实现要添加的功能。
3034
31- ### 示例
35+ ### [ 示例 ] ( https://github.com/hackcpp/cplusplus/blob/main/source%20code/design_pattern/decorator.cpp )
3236
3337@startuml 类图
3438abstract class Stream {
@@ -79,7 +83,7 @@ class Stream {
7983
8084class 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 {
104108class 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 }
Original file line number Diff line number Diff line change 88 - 设计模式
99 - 桥接模式
1010editLink : false
11- description : " 结构型设计模式,桥接模式(Bridge Pattern)"
11+ description : " 结构型设计模式,桥接模式(Bridge Pattern)将抽象部分与实现部分分离,使它们可以独立地变化。桥接模式通过将类的功能层次与实现层次分离,使得系统更具扩展性和灵活性 "
1212star : 804
1313sticky : 804
14+ head :
15+ - - meta
16+ - name : keywords
17+ content : 设计模式 桥接模式 结构型设计模式 Bridge Pattern
1418---
1519
1620
@@ -27,7 +31,7 @@ sticky: 804
27313 . ** 实现类接口(Implementor)** :定义实现类的接口,但不提供具体实现。实现类接口与抽象类没有继承关系。
28324 . ** 具体实现类(Concrete Implementor)** :实现实现类接口,提供具体的实现逻辑。
2933
30- ### 示例
34+ ### [ 示例 ] ( https://github.com/hackcpp/cplusplus/blob/main/source%20code/design_pattern/bridge.cpp )
3135
3236@startuml 类图
3337abstract class Color {
Original file line number Diff line number Diff line change 88 - 设计模式
99 - 责任链模式
1010editLink : false
11- description : " 行为型设计模式,责任链模式(Chain of Responsibility Pattern)"
11+ description : " 行为型设计模式,责任链模式(Chain of Responsibility Pattern)允许多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合。该模式将这些对象连成一条链,并沿着这条链传递请求,直到有对象处理它为止 "
1212sticky : 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 处理类
9397class ManagerHandler : public LeaveHandler {
9498public:
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 处理类
108112class DirectorHandler : public LeaveHandler {
109113public:
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;
You can’t perform that action at this time.
0 commit comments