洞察力+实践 :)




Why Use Interfaces

曾经以为接口的大部分作用只是为运行时提供类型信息,元数据,换句话说就是为类提供一份运行时的文档。运行时的程序能够理解这份文档,这样可以避免许多在编译时检查不出来的错误。而当我接触python,我突然发现接口的用处突然没了,因为python这样的动态语言元数据太丰富了,我相信很多玩python的朋友和我有同样感觉,我想这也是python至今没有直接支持接口这个概念的原因。
然后我又接触了zope,准确的说是zope3,因为这个版本变化太大了。zope3让我重新认识到了接口的用处。请看zope是怎么说的:

Why Use Interfaces?

Interfaces solve a number of problems that arise while developing large systems with lots of developers.

  • Developers waste a lot of time looking at the source code of your system to figure out how objects work. This is even worse if someone else has already wasted their time doing the same thing.
    (不用花时间去阅读代码就可以大致知道程序的用途 )
  • Developers who are new to your system may misunderstand how your object works, causing, and possibly propagating, usage errors.
    (还是方便理解代码)
  • Because an object's interface is inferred from the source, developers may end up using methods and attributes that are meant for "internal use only".
    (可以用来隐藏实现,我想这个很重要,因为python没有提供这个)
  • Code inspection can be hard, and very discouraging to novice programmers trying to understand code written by gurus.
    (还是方便理解代码)

总的来说貌似就剩一条了: 使应用程序结构更清晰,易于理解。呵呵





suppercabbage 发布于 11:23 | Trackback (0) | 编辑

Comments



Add Comment


姓名:

Email:

主页: