第二章 使用接口(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. 贯穿本书,我将讨论了许多有关解耦代码和这样做会带有什么样的好处的话题。

PDF预览

下载地址 

http://files.cnblogs.com/DKSoft/CodingInDelphi.pdf

 

image

 

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.

当然针对接口编程,你首先必须知道什么是接口

目录
相关文章
|
人工智能 JSON API
迎战2022 - Python中文翻译《环球时报》整篇文章实战演示,调用有道翻译API接口进行英文转中文翻译实例训练
迎战2022 - Python中文翻译《环球时报》整篇文章实战演示,调用有道翻译API接口进行英文转中文翻译实例训练
181 0
|
Web App开发
Chrome操作指南——入门篇(十一)network
Chrome操作指南——入门篇(十一)network
Chrome操作指南——入门篇(十一)network
|
网络协议 JavaScript Java
Google Java编程风格规范(2020年4月原版翻译)
Google Java Style Guide 这份文档是Google Java编程风格规范的完整定义。当且仅当一个Java源文件符合此文档中的规则, 我们才认为它符合Google的Java编程风格。 与其它的编程风格指南一样,这里所讨论的不仅仅是编码格式美不美观的问题, 同时也讨论一些约定及编码标准。然而,这份文档主要侧重于我们所普遍遵循的规则,
1192 0
|
NoSQL 编译器 iOS开发
[OC Runtime编程指南_翻译]三、与运行时交互
[OC Runtime编程指南_翻译]三、与运行时交互
149 0
|
Web App开发 JavaScript 前端开发
mobl:针对移动Web开发的“.NET研究”DSL
  简介   现在,针对移动设备像智能手机和平板电脑的应用开发很流行。Apple公司的AppStore(针对iPhone、iPod和iPad)拥有超过350,000种应用,而Android的marketplace也快速追赶上来,现在已经拥有超过200,000种应用。
1310 0
|
前端开发 容器
使用 MEF 公开 Silverlight“.NET研究” MVVM 应用程序中的接口
  下载代码示例   许多开发人员可能都将 Silverlight 视为以 Web 为中心的技术,但实际上,它已经成为构建任何应用程序的优秀平台。
932 0
Visual Studio 2010 中的代码“.NET研究”约定设置
  软件约定称为代码约定,通过这一约定可以表示代码正常工作所需的正式条件。 如果方法未按预期收到数据或生成的数据不符合预期的后置条件,代码约定将导致代码引发异常。 有关前置条件和后置条件的概述,您可能需要查看我上个月发表的文章 (msdn.microsoft.com/magazine/gg983479)。
780 0

热门文章

最新文章