第二章 使用接口(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.

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

目录
相关文章
|
4月前
|
JSON 负载均衡 网络协议
Rpc编程系列文章第二篇:RPC框架设计目标
Rpc编程系列文章第二篇:RPC框架设计目标
|
6月前
|
算法 C++ iOS开发
软件开发入门教程网 Search之C++ 接口(抽象类)
软件开发入门教程网 Search之C++ 接口(抽象类)
|
30天前
|
设计模式 算法 C语言
【C/C++ 程序设计】 C++如何适配他人的接口(How to Adapt to Others‘ Interfaces in C++)
【C/C++ 程序设计】 C++如何适配他人的接口(How to Adapt to Others‘ Interfaces in C++)
74 1
|
3月前
|
Java 编译器 C++
重温经典《Thinking in java》第四版之第九章 接口(四十六)
重温经典《Thinking in java》第四版之第九章 接口(四十六)
17 0
|
3月前
|
设计模式 算法 Java
重温经典《Thinking in java》第四版之第九章 接口(四十七)
重温经典《Thinking in java》第四版之第九章 接口(四十七)
26 0
|
4月前
|
Java
Java反射的详细解析之三
面试题: 你觉得反射好不好?好,有两个方向 第一个方向:无视修饰符访问类中的内容。但是这种操作在开发中一般不用,都是框架底层来用的。 第二个方向:反射可以跟配置文件结合起来使用,动态的创建对象,动态的调用方法。
21 0
|
10月前
|
程序员 编译器 Serverless
【Effective C++详细总结】第四章 设计与声明
【Effective C++详细总结】第四章 设计与声明
89 0
|
C语言 C++
C++中的类简要介绍
C++中的类简要介绍
60 0
|
Python
Python编程:zope.interface实现接口
Python编程:zope.interface实现接口
100 0