这是Design Patterns in C++: Creational的学习笔记,课程链接:
https://app.pluralsight.com/library/courses/design-patterns-cpp-creational/table-of-contents

文章目录

  • Single Responsibility Principle
  • Open-closed Principle
  • Liskov Substitution Principle
  • Interface Segregation Principle
  • Dependency Inversion Principle
  • Dependency Injection with Boost.DI
  • Monads

Single Responsibility Principle

Open-closed Principle

  • Entities should be open for extension but closed for modification
enum class Color {Red, Green, Blue};
enum class Size {Small, Medium, Large};template <typename T> struct ISpecification
{virtual bool is_satisfied(T* item) = 0;
};template <typename T> struct IFilter
{virtual std::vector<T*> filter(std::vector<T*> items, ISpecification<T>& spec) = 0;
};struct BetterFilter : IFilter<Product>
{typedef std::vector<Product*> Items;Items filter(Items items, ISpecification<Product>& spec) override{Items result;for (auto& p : items)if (spec.is_satisfied(p))result.push_back(p);return result;}
};struct ColorSpecification: ISpecification<Product>
{Color color;explicit ColorSpecification(const Color color): color{color}{}bool is_satisfied(Product* item) override {return item->color == color;}
};struct SizeSpecification: ISpecification<Product>
{Size size;explicit SizeSpecification(const Size size): size{size}{}bool is_satisfied(Product* item) override {return item->size == size;}
};template <typename T> struct AndSpecification : ISpecification<T>
{ISpecification<T>& first;ISpecification<T>& second;AndSpecification(ISpecification<T>& first, ISpecification<T>& second): first{first},second{second}{}bool is_satisfied(T* item) override {return first.is_satisfied(item) && second.is_satisfied(item);}
};int main()
{Product apple{ "Apple", Color::Green, Size::Small };Product tree{ "Tree", Color::Green, Size::Large };Product house{ "House", Color::Blue, Size::Large };std::vector<Product*> all{ &apple, &tree, &house };BetterFilter bf;ColorSpecification green(Color::Green);auto green_things = bf.filter(all, green);for (auto& x : green_things)std::cout << x->name << " is green" << std::endl;SizeSpecification big(Size::Large);AndSpecification<Product> green_and_big{ big,green };auto green_big_things = bf.filter(all, green_and_big);for (auto& x : green_big_things)std::cout << x->name << " is green and big" << std::endl;return 0;
}

Liskov Substitution Principle

  • Objects should be replaceable with instances of their subtypes withou altering program correctness.

Interface Segregation Principle

  • No client should be forced to depend on methods it does not use.
  • Many client-specifc interfaces better than one general-purpose interface.
struct Document;// ======================Bad Example below================
struct IMachine
{virtual void print(std::vector<Document*> docs) = 0;virtual void scan(std::vector<Document*> docs) = 0;virtual void fax(std::vector<Document*> docs) = 0;
};struct MFP : IMachine
{void print(std::vector<Document*> docs) override;void scan(std::vector<Document*> docs) override;void fax(std::vector<Document*> docs) override;
};// ===============Good Example below===============
// Break up your interface into lots of little interfaces
struct IPrinter
{virtual void print(std::vector<Document*> docs) = 0;
};struct IScanner
{virtual void scan(std::vector<Document*> docs) = 0;
};struct Printer : IPrinter
{void print(std::vector<Document*> docs) override;
};struct Scanner : IScanner
{void scan(std::vector<Document*> docs) override;
};//if you still want that big interface,
//use multiple inheritance for the interface type itself
struct IMachine : IPrinter, IScanner {};struct Machine : IMachine
{IPrinter& printer;IScanner& scanner;Machine(IPrinter& printer, IScanner& scanner): printer{printer},scanner{scanner}{}void print(std::vector<Document*> docs) override {printer.print(docs);}void scan(std::vector<Document*> docs) override {scanner.scan(docs);}
};

Dependency Inversion Principle

  • Dependencies should be abstract rather than concrete.
  • High-level modules should not depend on low-level modules, but both should depend on some form of abstraction.
  • Abstractions should not depend upon details, but details should depend upon abstractions.

Dependency Injection with Boost.DI

Monads

SOLID Design Principle相关推荐

  1. design principle:java 回调与委派/委托机制

    博客 design principle:模拟 android Button 控件点击事件   主要说了一下模拟 android 的 Listener 模式,其实这就是一种委派与回调机制的体现. 委派, ...

  2. Design Principle

    Design Principle posted on 2013-12-04 14:34  JasonChang 阅读( ...) 评论( ...) 编辑 收藏 转载于:https://www.cnbl ...

  3. 设计模式-设计原则(Design Principle)

    本文由@呆代待殆原创,转载请注明出处. 写在前面:所谓设计原则并不是一定要遵守的法则,只是一种建议,因为保持这些原则本身会有一定代价,若是这些代价超过了带来的好处就得不偿失了,所以一切还是以简单为准. ...

  4. SOLID Design Principles in C#

    Single Responsibility Principle Open-Closed Principle Liskov Substitution Principle Interface Segreg ...

  5. Paper:2020年3月30日何恺明团队最新算法RegNet—来自Facebook AI研究院《Designing Network Design Spaces》的翻译与解读

    Paper:2020年3月30日何恺明团队最新算法RegNet-来自Facebook AI研究院<Designing Network Design Spaces>的翻译与解读 导读: 卧槽 ...

  6. Paper之RegNet:《Designing Network Design Spaces》的翻译与解读—2020年3月30日来自Facebook AI研究院何恺明团队最新算法RegNet

    Paper之RegNet:<Designing Network Design Spaces>的翻译与解读-2020年3月30日来自Facebook AI研究院何恺明团队最新算法RegNet ...

  7. SOLID 设计原则 (有点长但很透彻)

    面向对象设计原则 SOLID 应该是职业程序员必须掌握的基本原则,每个程序员都应该了然于胸,遵守这 5个原则可以帮助我们写出易维护.易拓展的高内聚低耦合的代码. 它是由罗伯特·C·马丁(知名的 Rob ...

  8. 从零学web前端_从零到前端英雄(第2部分)

    从零学web前端 This article is part two of the "From Zero to Front-end Hero" series. In part one ...

  9. 程序员应知道这十大面向对象设计原则

    面向对象设计原则是OOPS编程的核心, 但我见过的大多数Java程序员热心于像Singleton (单例) . Decorator(装饰器).Observer(观察者) 等设计模式, 而没有把足够多的 ...

最新文章

  1. 新装Ubuntu18.04系统配置PX4环境
  2. 分布式是写出来的(四)
  3. 前端学习(378):新春贺卡制作1
  4. RabbitMQ pull与push的区别
  5. 未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0, Culture=neutral,解决
  6. LINQ解决依据某个字段去重
  7. BlogEngine.Net架构与源代码分析系列part13:实现分析(上)——HttpHandlers与HttpModules...
  8. nbu备份nas文件服务器,NBU网络备份大全之远程配置备份策略
  9. js正则表达式校验手机号码和电话号码
  10. 北京地区常用dns地址解析速度快
  11. 把antd组件的英文切换为中文
  12. 如何产生JIC文件(sof+ELF=jic)
  13. 预防跌倒-笑做“不倒翁”
  14. 高标准农田建设通则2014
  15. js开发:数组的push()、pop()、shift()和unshift()
  16. 美国春季计算机硕士入学的学校,美国硕士春季入学学校推荐哪些?
  17. 正态分布的前世今生——如何发现的?
  18. 阿里 P9 开源分享内部 Java 核心开发手册(2022 版)覆盖 P5 到 P8
  19. 数据结构之:时间复杂度(T(n)=O(n))
  20. Linux SPI驱动框架(3)——设备驱动层

热门文章

  1. 【前端高频面试题】 浏览器地址栏输入网页地址后发生了什么?
  2. meituxiuxiu,做超长图
  3. 关于Frenet坐标系内曲率约束
  4. 抖店-多功能脚本-油猴脚本
  5. java jxl 解析excel,java使用jxl解析Excel
  6. mac关闭暂停播放键打开iTunes
  7. java.beans_JavaBeans的介绍与使用
  8. Cascad级删的单理解
  9. chrome硬解码265的方案
  10. Ubuntu pip install切换为清华大学镜像源