程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 第二章 使用接口(Using Interfaces)-書籍翻譯

第二章 使用接口(Using Interfaces)-書籍翻譯

編輯:Delphi

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.

當然針對接口編程,你首先必須知道什麼是接口

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved