第二章 使用接口(Using Interfaces)-书籍翻译
PDF预览
下载地址
http://files.cnblogs.com/DKSoft/CodingInDelphi.pdf

1.1. 解耦(Decoupling)
All through this book, I’ll talk a lot about decoupling your code and why that is really good. But a definition here is probably a good idea.
贯穿本书,我将讨论了许多有关解耦代码和这样做会带有什么样的好处的话题。不过在这里定义也许是一个好主意。
Code can be said to be decoupled when your classes are designed in such away that they don’t depend on the concrete implementations of other classes. Two classes are loosely coupled when they know as little about each other as possible, the dependencies between them are as thin as possible, and the communication lines between them are as simple as possible.
当你的类如果是这样设计的: 它们不依赖于其他类的具体实现, 这样的代码可以说是松散耦合的. 两个类当相互之间了解的尽可能的少, 相互依赖尽可能的弱, 彼此之间通讯尽可能的简单, 这样的两个类是松散耦合的.
In other words, decoupling is the process of limiting your dependencies to abstractions as much as possible. If you want to write good, clean code, you will try to couple only to abstractions; interfaces are abstractions. Much of what will be discussed in this book will be about using interfaces to code against abstractions. As I tweeted above, this is critical to writing good code that is easy to maintain.
换句话说, 解耦就是限制你尽可能多的依赖抽象的过程, 如果你想写出漂亮干净的代码, 你该试试只耦合抽象, 接口就是抽象的, 这本书里很多将要讨论的就是使用接口写出针对抽象的代码, 如我在上面推特里写到的, 这是写出容易维护的好代码的关键.
In Delphi, decoupling starts by limiting what goes into the uses clause of any given unit. Every time you use code from another unit, you are creating a connection or a coupling of code. You should try as much as possible to expose units that declare only Delphi interfaces to other units. You should endeavor to put as little as possible in the interface section of your units. This will allow you to limit the coupling of your code – if you don’t put it in the interface section, it can’t be coupled to.
Delphi中,解耦从限制任何进入uses的单元开始. 每当你使用另外一个单元的代码时,你是在创建一个关联或者说是耦合代码,你应该尽可能的尝试去暴露那些只申明了接口的单元给其他单元。你应该努力做到尽可能少放代码在你单元的interface部分. 这将限制你代码的耦合 - 如果你不不放在interface节中,那它不会被耦合.
Once you have limited the connections between your units, you can then start limiting the connections between your classes. Classes have to connect to each other somehow – otherwise you can’t build a system. But if you are going to connect things together, you want those connections to be as thin as possible. That’s where interfaces come in.
一旦你已经限制了单元之间关联, 你就可以限制类之间的关联. 类不得不以某种方法连接在一起, 否则你就不能构建出一个系统, 但是如果你将要把一些东西关联在一起, 你就要让这些关联尽可能的弱, 那就是接口的用武之地.
Of course to program against an interface, you have to first know what an interface is.
当然针对接口编程,你首先必须知道什么是接口
第二章 使用接口(Using Interfaces)-书籍翻译的更多相关文章
- 第二章——Parcelable接口的使用(跨进程,Intent传输)
一.Parcelable类(Android独有的) 简介:Parcelable是一个接口. 作用:是Android提供的序列化接口,实现序列化和反序列化的操作. 二.跨进程使用 步骤一:创建Book类 ...
- [书籍翻译] 《JavaScript并发编程》 第二章 JavaScript运行模型
本文是我翻译<JavaScript Concurrency>书籍的第二章 JavaScript运行模型,该书主要以Promises.Generator.Web workers等技术来讲解J ...
- 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...
- [书籍翻译] 《JavaScript并发编程》第五章 使用Web Workers
本文是我翻译<JavaScript Concurrency>书籍的第五章 使用Web Workers,该书主要以Promises.Generator.Web workers等技术来讲解Ja ...
- [书籍翻译] 《JavaScript并发编程》第七章 抽取并发逻辑
本文是我翻译<JavaScript Concurrency>书籍的第七章 抽取并发逻辑,该书主要以Promises.Generator.Web workers等技术来讲解JavaScrip ...
- [书籍翻译] 《JavaScript并发编程》第三章 使用Promises实现同步
本文是我翻译<JavaScript Concurrency>书籍的第三章 使用Promises实现同步,该书主要以Promises.Generator.Web workers等技术来讲解J ...
- xv6课本翻译之——第0章 操作系统接口
Chapter 0 第0章 Operating system interfaces 操作系统接口 The job of an operating system is to share a comput ...
- 《Entity Framework 6 Recipes》翻译系列 (3) -----第二章 实体数据建模基础之创建一个简单的模型
第二章 实体数据建模基础 很有可能,你才开始探索实体框架,你可能会问“我们怎么开始?”,如果你真是这样的话,那么本章就是一个很好的开始.如果不是,你已经建模,并在实体分裂和继承方面感觉良好,那么你可以 ...
- [书籍翻译] 《JavaScript并发编程》第一章 JavaScript并发简介
> 本文是我翻译<JavaScript Concurrency>书籍的第一章,该书主要以Promises.Generator.Web workers等技术来讲解JavaScript并 ...
随机推荐
- 【tp5】表单验证之token
1.本场景仅介绍复杂一点的ajax请求带上token验证,普通的form提交不讲 2.原理仅个人理解,如有偏差 欢迎各路大神指点:框架tp5.0.18 目前将token放置于 ajax的header头 ...
- 【jquery】ajax 动态 改变 select下拉框选中的值
//JS<script type="text/javascript> //ajax动态给添加原料的[商品名称]下拉框绑定selected属性 $("#origin_co ...
- FTP命令字和响应码解释
FTP命令: 命令 描述 ABOR 中断数据连接程序 ACCT <account> 系统特权帐号 ALLO <bytes> 为服务器上的文件存储器分配字节 APPE &l ...
- iOS10 打开APP设置界面和WIFI界面
在iOS10以上,权限这块有了一些变化 首先在info的URL Types 添加 prefs 1.打开APP设置界面 //打开设置 let url:NSURL = NSURL(string: UIA ...
- ThinkPHP学习(二)
书接前文. 我想前台填写内容,然后在后台保存一下,规划了一下前台要录入的内容,主要包括title.content两大内容.然后简单设计了一个前台页面(复杂的我得会啊),就在上篇文章的基础上直接加了: ...
- JavaScript Window Location 当前页面的地址
window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. Window Location window.location 对象在编写时可不使用 window ...
- fisheye Error occurred during initialization of VM Could not reserve enough space for object heap 问题解决!
参考文章:https://answers.atlassian.com/questions/9397/not-enough-heap-space-to-run-fisheye fisheye下载好了之后 ...
- JAVA List删除时需注意的地方
JAVA的LIST在删除时,一般会用list.remove(o); 但这样往往会出现问题,先来看下面的这段代码: package com.demo; import java.util.ArrayLis ...
- Socket模型(二):完成端口(IOCP)
为什么要采用Socket模型,而不直接使用Socket? 原因源于recv()方法是堵塞式的,当多个客户端连接服务器时,其中一个socket的recv调用时,会产生堵塞,使其他链接不能继续.这样我们又 ...
- 使用Cordova搭建Andoid和iOS开发环境
最近在了解cordova ,下面的分享出来 大家可以看看, 我 有空也按照这个写写demo 1.下载node.js,进行安装 https://nodejs.org/en/ 2.安装cordova ...