Framework Design Guidelines笔记

Framework Design Guidelines是微软对于好api设计的一个指导,当然针对的是.NET,其中对于Android/Java有裨益的记录一下。

命名

大小写
  • 首字母大写:class、interface。对于IO这种缩写,所有字母都大写,例如IOStream
  • 驼峰:其他所有。对于缩写,保持大小写一致,例如ioStream,fileIOStream
  • xml中下划线:Android的xml里会用到下划线,按系统的逻辑,下划线是手动命名空间的分割线,例如layout_toLeftOf,layout是命名空间,toLeftOf是名字,中间用下划线分隔
  • 类中下划线:非private的static变量都应该用全大写字母+下划线
命名内容
  • 按正常语序排列单词
  • 别怕长,说明白事情,少用缩写
  • 避免匈牙利命名
  • 是否使用语言强相关名称,比如getInteger。如果是有上下文语义的(getLength)不使用;如果是一个基础语言类函数(IntegerWrapper#getInteger),就应该使用
  • 升级接口时,命名尽量接近,尽量用后缀来标示(方便自动完成),推荐直接使用版本号
包名
  • 避免把公司组织架构带到包名中,组织架构太不靠谱了
  • 包名合理的时候用复数
  • 包名和包内类名不能重复
类名
  • 类名用名词,interface用形容词组,I开头
  • interface的默认实现,应该是interface名去掉I
  • 泛型名用T,如果T不好使,就用以T开头的命名,例如ISessionChannel<TSession>
  • 如果继承自通用系统类(Map、Collection)最好加上相应的后缀(HashMap)
  • 枚举用单数,除非是bitSet这种能够并存(取或)的
方法和成员变量
  • 方法名用动词,避免get、set等易混名
  • boolean型的命名都用正面形式,不要有don’tXxx
  • .NET不推荐有类型前缀,但是Android官方比较推荐mFoo(member)和sBar(static)的写法
  • 对于资源,命名要有层次结构。见xml中下划线

类结构设计

抽象类(abstract)
  • 构造函数应该是protected而不是public的
  • 应该有一个抽象类的实现,来考察设计合理性
静态类
  • 工具类或者全局Context
Interface
  • 避免空interface,宁肯使用一个成员变量来标示(之前用过IProguardKeep来简化proguard文件,不知道算不算)
  • 有一个实现,有一个使用的地方
  • 一旦暴露就不应该再改动
  • 推荐抽象类而不是interface,除非有多继承或者设计模式上的需求,最好不要用interface,否则会有api升级的问题
Enum和内部类
  • Enum分为常量和位操作(Java似乎不支持)
  • Android不推荐使用Enum
  • 内部类应该是对外不可见的,Android系统基本只有interface的声明会出现外部可见的内部类的情况

可扩展性

  • 继承机制,最方便,但是应该限制于叶子类型(View)或者有替换能力的中间类型(Handler)。否则继承是没有效用的
  • protected成员变量在继承过程中与public一样,避免过度暴露实现机制。特别是成员变量,还是private+protected getter setter比较好,封装一层(本人教训)
  • 回调,用以插入逻辑(模板方法)或者改变整体行为(带返回的回调)
  • 回调不能保证性能,因为被插入的逻辑不可知
  • 回调尽量用lamda表达式,一定要预防未知异常
  • 合理使用final来修饰方法,让继承更多的用于扩展而非修改基类
  • 用多实现层次的基类来简化接入(Android中的Adapter系列)。对于不希望直接使用的类,即使不是抽象的也一定要用abstract。最好放到独立的包中

异常

  • 不要使用error code
  • Tester-Doer模式和Try-Parse模式,

系统接口

  • 接口中尽量用更抽象的Collection类
  • 注意接口中变量的可变性
  • 接口中泛型的类型一定要明确
  • 接口中不要出现Iterator
  • 在getCollection类接口中,不要返回null,返回空集合
  • 要仔细考察CopyOnWrite的应用场景
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Framework Design Guidelines, Second Edition, teaches developers the best practices for designing reusable libraries for the Microsoft .NET Framework. Expanded and updated for .NET 3.5, this new edition focuses on the design issues that directly affect the programmability of a class library, specifically its publicly accessible APIs. This book can improve the work of any .NET developer producing code that other developers will use. It includes copious annotations to the guidelines by thirty-five prominent architects and practitioners of the .NET Framework, providing a lively discussion of the reasons for the guidelines as well as examples of when to break those guidelines. Microsoft architects Krzysztof Cwalina and Brad Abrams teach framework design from the top down. From their significant combined experience and deep insight, you will learn * The general philosophy and fundamental principles of framework design * Naming guidelines for the various parts of a framework * Guidelines for the design and extending of types and members of types * Issues affecting-and guidelines for ensuring-extensibility * How (and how not) to design exceptions * Guidelines for-and examples of-common framework design patterns Guidelines in this book are presented in four major forms: Do, Consider, Avoid, and Do not. These directives help focus attention on practices that should always be used, those that should generally be used, those that should rarely be used, and those that should never be used. Every guideline includes a discussion of its applicability, and most include a code example to help illuminate the dialogue. Framework Design Guidelines, Second Edition, is the only definitive source of best practices for managed code API development, direct from the architects themselves. A companion DVD includes the Designing .N ET Class Libraries video series, instructional presentations by the authors on design guidelines for developing classes and components that extend the .NET Framework. A sample API specification and other useful resources and tools are also included.
 2006年JOLT生产效率大奖得主!来自微软。NET框架设计组的智慧结晶, 洞悉。NET技术内幕,.NET开发者的必备图书.   本书为框架设计师和广大开发人员设计高质量的软件提供了权威的指南。书中介绍了在设计框架时的最佳实践,提供了自顶向下的规范,其中所描述的规范普遍适用于规模不同、可重用程序不同的框架和软件。这些规范经历.NET框架三个版本的长斯开发,凝聚了数千名开发人员的经验和智慧。微软的各开发组正在使用这些规范开发下一代影响世界的软件产品。   本书适用于框架设计以及相关的专业技术人员,也适用于高等院校相关专业的学生和教师阅读参考。 -------------------------------------------------------------------------------- 作者简介   Krzysztof Cwalina微软公司公共语言运行库(CLR)的项目经理。他曾为.NET框架的首个版本设计API,并负责任框架中的多个名字空间,包括System.Collections、System.Diagnostics、System.Messaging,等等。他也是FxCop开发组的发起成员之一。目前,他正致力于在整个公司范围内开发推广设计规范,并将其应用到.NET框架和WinFX中。 -------------------------------------------------------------------------------- 编辑推荐   本书为框架设计师和广大开发人员设计高质量的软件提供了权威的指南。书中介绍了在设计框架时的最佳实践,提供了自顶向下的规范,其中所描述的规范普遍适用于规模不同、可重用程序不同的框架和软件。这些规范经历.NET框架三个版本的长斯开发,凝聚了数千名开发人员的经验和智慧。微软的各开发组正在使用这些规范开发下一代影响世界的软件产品。   本书适用于框架设计以及相关的专业技术人员,也适用于高等院校相关专业的学生和教师阅读参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值