{"id":13958956,"url":"https://github.com/liu-jianhao/Cpp-Design-Patterns","last_synced_at":"2025-07-21T00:32:45.493Z","repository":{"id":37335630,"uuid":"151187502","full_name":"liu-jianhao/Cpp-Design-Patterns","owner":"liu-jianhao","description":"C++设计模式","archived":false,"fork":false,"pushed_at":"2024-10-11T08:12:29.000Z","size":93,"stargazers_count":2413,"open_issues_count":3,"forks_count":602,"subscribers_count":44,"default_branch":"master","last_synced_at":"2024-11-28T02:35:42.413Z","etag":null,"topics":["cpp","design-patterns"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/liu-jianhao.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-02T02:00:04.000Z","updated_at":"2024-11-26T06:49:55.000Z","dependencies_parsed_at":"2022-07-12T12:04:27.670Z","dependency_job_id":null,"html_url":"https://github.com/liu-jianhao/Cpp-Design-Patterns","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/liu-jianhao/Cpp-Design-Patterns","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liu-jianhao%2FCpp-Design-Patterns","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liu-jianhao%2FCpp-Design-Patterns/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liu-jianhao%2FCpp-Design-Patterns/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liu-jianhao%2FCpp-Design-Patterns/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liu-jianhao","download_url":"https://codeload.github.com/liu-jianhao/Cpp-Design-Patterns/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liu-jianhao%2FCpp-Design-Patterns/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266221324,"owners_count":23894966,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","design-patterns"],"created_at":"2024-08-08T13:02:11.882Z","updated_at":"2025-07-21T00:32:40.483Z","avatar_url":"https://github.com/liu-jianhao.png","language":"C++","funding_links":[],"categories":["C/C++ 程序设计","C++"],"sub_categories":["网络服务_其他"],"readme":"# C++设计模式\n\n## 什么是设计模式\n“每一个模式描述了一个在我们周围不断重复发生的问题，以及该问题的解决方案的核心。这样，你就能一次又一次地使用该方案而不必做重复劳动”。\n——Christopher Alexander\n\n## 如何解决复杂性？\n+ 分解\n  + 人们面对复杂性有一个常见的做法：即分而治之，将大问题分解为多个小问题，将复杂问题分解为多个简单问题。\n+ 抽象\n  + 更高层次来讲，人们处理复杂性有一个通用的技术，即抽象。由于不能掌握全部的复杂对象，我们选择忽视它的非本质细节，而去处理泛化和理想化了的对象模型。\n  \n  \n## 面向对象设计原则\n1. 依赖倒置原则（DIP）\n  + 高层模块(稳定)不应该依赖于低层模块(变化)，二者都应该依赖于抽象(稳定) 。\n  + 抽象(稳定)不应该依赖于实现细节(变化) ，实现细节应该依赖于抽象(稳定)。\n2. 开放封闭原则（OCP）\n  + 对扩展开放，对更改封闭。\n  + 类模块应该是可扩展的，但是不可修改。\n3. 单一职责原则（SRP）\n  + 一个类应该仅有一个引起它变化的原因。\n  + 变化的方向隐含着类的责任。\n4. Liskov 替换原则（LSP）\n  + 子类必须能够替换它们的基类(IS-A)。\n  + 继承表达类型抽象。\n5. 接口隔离原则（ISP）\n  + 不应该强迫客户程序依赖它们不用的方法。\n  + 接口应该小而完备。\n6. 优先使用对象组合，而不是类继承\n  + 类继承通常为“白箱复用”，对象组合通常为“黑箱复用” 。\n  + 继承在某种程度上破坏了封装性，子类父类耦合度高。\n  + 而对象组合则只要求被组合的对象具有良好定义的接口，耦合度低。\n7. 封装变化点\n  + 使用封装来创建对象之间的分界层，让设计者可以在分界层的一侧进行修改，而不会对另一侧产生不良的影响，从而实现层次间的松耦合。\n8. 针对接口编程，而不是针对实现编程\n  + 不将变量类型声明为某个特定的具体类，而是声明为某个接口。\n  + 客户程序无需获知对象的具体类型，只需要知道对象所具有的接口。\n  + 减少系统中各部分的依赖关系，从而实现“高内聚、松耦合”的类型设计方案。\n\n## 从封装变化角度对模式分类\n### 组件协作：\n+ [Template Method](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Template%20Method)\n+ [Template-Pattern(Head-First版)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Template-Pattern)\n+ [Observer/Event](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Observer)\n+ [Observer(Head-First版)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Observer-Pattern)\n+ [Strategy](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Strategy)\n+ [Strategy(Head-First版)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Strategy-Pattern)\n### 单一职责：\n+ [Decorator](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Decorator)\n+ [Decorator(Head-First版)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Decorator-Pattern)\n+ [Bridge](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Bridge)\n### 对象创建:\n+ [Factory Method](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Factory%20Method)\n+ [Factory(Head-First版)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Factory-Pattern)\n+ [Bridge](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Bridge)\n+ [Abstract Factory](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Abstract%20Factory)\n+ [Prototype](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Prototype)\n+ [Builder](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Builder)\n### 对象性能：\n+ [Singleton](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Singleton)\n+ [Flyweight(享元模式)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Flyweight)\n### 接口隔离:\n+ [Façade(门面模式)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Facade)\n+ [Proxy](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Proxy)\n+ [Mediator(中介者)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Mediator)\n+ [Adapter](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Adapter)\n### 状态变化：\n+ [Memento(备忘录)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Memento)\n+ [State](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/State)\n### 数据结构：\n+ [Composite(组合模式)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Composite)\n+ [Iterator](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Iterator)\n+ [Chain of Resposibility(职责链)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Chain%20of%20Resposibility)\n### 行为变化：\n+ [Command](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Command)\n+ [Command(Head-First版)](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Command-Pattern)\n+ [Visitor](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Visitor)\n### 领域问题：\n+ [Interpreter](https://github.com/liu-jianhao/Cpp-Design-Patterns/tree/master/Interpreter)\n\n\n## 总结\n### 现代较少用的模式\n+ Builder\n+ Mediator\n+ Memento\n+ Iterator\n+ Chain of Resposibility\n+ Command\n+ Visitor\n+ Interpreter\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliu-jianhao%2FCpp-Design-Patterns","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliu-jianhao%2FCpp-Design-Patterns","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliu-jianhao%2FCpp-Design-Patterns/lists"}