HackingC++ Learning笔记 Chapter12-Software Design Basics软件设计基础

扫花
• 阅读 647

Design Principles

Why bother?

  • Correctness 正确性
  • Maintainability 可维护性

What we want:

  • understandable programs/functions/classes/...
  • compiler as correctness checker if it compiles it will (almost certainly) be correct!

interfaces should be easy to use correctly and hard to use incorrectly. - Scott Meyers

Common Sources of Bad Design 坏设计的常见来源

The road to (code) hell is paved with good intentions:
通往(代码)地狱的道路是由良好的意图铺成的。

  • trying too hard to anticipate future changes
    太过努力去预测未来的变化
  • class interfaces and interactions too closely modeled after real-world or abstract math objects
    类的界面和互动太过接近于现实世界或抽象数学对象的模式
  • trying to build the ultimate and universal system or inheritance hierarchy that can do/describe/model anything
    试图建立一个终极的、通用的系统或继承权 层次结构,可以做/描述/模拟任何事情
  • trying to "abstract away" hardware restrictions
    试图 "抽象化 "硬件限制

Common Manifestations of Bad Design 不良设计的常见表现形式

  • inconsistent naming
    命名不一致
  • inheritance used to avoid data redundancy
    用继承来避免数据冗余
  • setter/getter-for-every-member
    每一个成员的设置器/获取器
  • classes or functions with way more than one purpose ("god classes", 1000-line functions with 10 if-levels)
    类或函数的目的远不止一个 ("神类",有10个if-levels的1000行函数)
  • functions with many parameters (often of the same type)
    有许多参数的函数(通常是同一类型的)
  • interfaces that expose (too much) implementation details
    暴露(太多)实现细节的接口
  • deep inheritance hierarchies
    深度继承层次结构
  • hyper-flexible and ultra-modular systems with a gazillion of possible ways to let objects interact with each other
    超灵活和超模块化的系统,有无数的 让对象之间相互作用的可能方式

Make Your Intent Clear & Avoid Confusion 明确你的意图,避免混淆

  • unambiguous & consistent naming
    毫不含糊的、一致的命名
  • restrict choices (e.g. set of possible/valid values)
    限制选择(例如,可能/有效值的集合)
  • dedicated types with clearly defined purpose
    有明确目的的专用类型
  • few function parameters with distinct types, avoid output parameters (call by non-const reference)
    少数具有不同类型的函数参数。避免输出参数(通过非常量引用调用)
  • prefer standard library containers & algorithms
    喜欢标准库中的容器和算法
  • prefer patterns that are well-known in the C++ community
    喜欢C++社区中众所周知的模式

You don't know what's coming! 你不知道会发生什么!

… neither do your customers/users!
Change is coming - and not the way you thought it would:
变化正在到来--而且不是你想象的那样。

  • do not try to anticipate future changes/use cases
    不要试图预测未来的变化/用例
  • only add customization points that are needed/requested right now
    只添加现在需要/要求的定制点 现在就需要/要求的定制点
  • limit the potential impact of future changes
    限制未来变化的潜在影响

Limit The Scope/Impact of Potential Changes 限制潜在变化的范围/影响

Isolate Functionality! 隔离功能!

  • declare variables in the smallest possible scope
    在尽可能小的范围内声明变量
  • each function and type should have one responsibility
    每个函数和类型应该有一个责任
  • avoid elaborate, hard to check preconditions
    避免复杂的、难以检查的前提条件
  • avoid deep inheritance hierarchies
    避免深层次的继承层次结构

OOP Design Patterns OOP设计模式

Patterns that you should probably know include:

  • Composite, Decorator, Bridge, Visitor, Proxy, Façade,
  • Iterator, Strategy, Observer, Command, Template Method,
  • Factory Method, Abstract Factory, Singleton,

Classic Book:
Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides
Design Patterns: Elements of Reusable Object-Oriented Software

Please Take Note:
Most patterns reflect the lack of certain language features.
大多数模式反映了某些语言特性的缺乏。

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
11个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
HTTP面试题(二):HTTP请求报文和响应报文格式
!(https://oscimg.oschina.net/oscnet/0406894fb1274bee91fc53c84c516576.jpg)看都看了还不点个赞!(https://oscimg.oschina.net/oscnet/095d444dc9a449ee85afd19b00fdf52b.png)!(h
Stella981 Stella981
3年前
Duang,HUAWEI DevEco IDE全面升级啦
想感受全新UI带来的视觉及交互体验、HiKey970开发板调测、HiAIAPI推荐和收藏、深度AI模型分析等新功能,体验高清晰度和流畅度的远程AI真机调测吗?!(https://oscimg.oschina.net/oscnet/f4e1bb24ff00b8c6ea27f75370a53bfbacd.jpg)全新的UI设计
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
1年前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
美凌格栋栋酱 美凌格栋栋酱
5个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
扫花
扫花
Lv1
宁愿只是一首诗,厌腻可抛弃,喜爱就再不忘记
文章
4
粉丝
0
获赞
0