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)-书籍翻译的更多相关文章

  1. 第二章——Parcelable接口的使用(跨进程,Intent传输)

    一.Parcelable类(Android独有的) 简介:Parcelable是一个接口. 作用:是Android提供的序列化接口,实现序列化和反序列化的操作. 二.跨进程使用 步骤一:创建Book类 ...

  2. [书籍翻译] 《JavaScript并发编程》 第二章 JavaScript运行模型

    本文是我翻译<JavaScript Concurrency>书籍的第二章 JavaScript运行模型,该书主要以Promises.Generator.Web workers等技术来讲解J ...

  3. 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)

    书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...

  4. [书籍翻译] 《JavaScript并发编程》第五章 使用Web Workers

    本文是我翻译<JavaScript Concurrency>书籍的第五章 使用Web Workers,该书主要以Promises.Generator.Web workers等技术来讲解Ja ...

  5. [书籍翻译] 《JavaScript并发编程》第七章 抽取并发逻辑

    本文是我翻译<JavaScript Concurrency>书籍的第七章 抽取并发逻辑,该书主要以Promises.Generator.Web workers等技术来讲解JavaScrip ...

  6. [书籍翻译] 《JavaScript并发编程》第三章 使用Promises实现同步

    本文是我翻译<JavaScript Concurrency>书籍的第三章 使用Promises实现同步,该书主要以Promises.Generator.Web workers等技术来讲解J ...

  7. xv6课本翻译之——第0章 操作系统接口

    Chapter 0 第0章 Operating system interfaces 操作系统接口 The job of an operating system is to share a comput ...

  8. 《Entity Framework 6 Recipes》翻译系列 (3) -----第二章 实体数据建模基础之创建一个简单的模型

    第二章 实体数据建模基础 很有可能,你才开始探索实体框架,你可能会问“我们怎么开始?”,如果你真是这样的话,那么本章就是一个很好的开始.如果不是,你已经建模,并在实体分裂和继承方面感觉良好,那么你可以 ...

  9. [书籍翻译] 《JavaScript并发编程》第一章 JavaScript并发简介

    > 本文是我翻译<JavaScript Concurrency>书籍的第一章,该书主要以Promises.Generator.Web workers等技术来讲解JavaScript并 ...

随机推荐

  1. 收到新信息,弹出popup窗口提示

     

  2. easyui的日期控件

    1.日期控件只能点击控件进行选择, 不可手动编辑input框中的日期内容 editable="false" 2.日期控件既不可点击, 也不可手动编辑input框中的日期内容 dis ...

  3. Pycharm 中添加第三方库和插件

    在 PyCharm 中选择:File — Settings — 进入如下界面,点击 右上角的 “+” 可以添加其他库: 选择到相应的库,并 Install Package 即可:

  4. 升级cocoapods1.1.1版本

    先切换gem源 gem sources --remove https://rubygems.org/ gem source -a https://gems.ruby-china.org 查看是否切换成 ...

  5. apache配置中ProxyPassReverse指令的含义

    apache中的mod_proxy模块主要作用就是进行url的转发,即具有代理的功能.应用此功能,可以很方便的实现同tomcat等应用服务器的整合,甚者可以很方便的实现web集群的功能. 例如使用ap ...

  6. 进阶之路(基础篇) - 022 Arduino Leonardo 中文介绍(摘抄)

    本文摘抄:http://www.arduino.cn/thread-1205-1-1.html 概述Arduino Leonardo是基于ATmega32u4一个微控制器板.它有20个数字输入/输出引 ...

  7. MySQL的初次见面礼基础实战篇

    [版权申明] http://blog.csdn.net/javazejian/article/details/61614366 出自[zejian的博客] 关联文章: MySQL的初次见面礼基础实战篇 ...

  8. HDU 1258 Sum It Up (DFS)

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  9. 兼容各大浏览器的event获取

    event: //得到事件 function getEvent(evt){ if (evt && typeof(evt) != "undefined") { var ...

  10. 【Visual Studio】Visual Studio中常用的快捷键收集

    添加注释:Ctr+k+c 取消注释:Ctr+k+u 格式化:Ctr+a+k+f 当前光标自动向下移一行:Ctr+Shift+Enter 运行:F5 进入光标指定的方法或是类:F12 在当前文件中查找: ...