"Programming to an Interface, not an Implementation"

Ashwin Jayaprakash wrote in    Towards Bug-Free Code:

One of the fundamental constructs of an object-oriented programming language is an interface. As you might already know, an interface represents a set of contracts or basic behavior that allows the developer to switch the implementation without having to make changes to the code again and again.

A conscientious programmer will have a handful of interfaces in his program to ensure that the program is not too tightly coupled. An overzealous or novice programmer will be indiscriminate in creating too many interfaces and will still have packages that are neither stable nor easily extensible.

A line of code such as MyInterface intf = new MyInterfaceImpl() has little meaning, as the programmer has already decided that the implementation of MyInterface is MyInterfaceImpl right away. To a mature programmer, the solution seems obvious: use a factory pattern.

知难行易?知易行难?
这样的代码不是仍然在我的程序里出现,甚至所谓的Factory Method不是照样返回一个concrete class?

programming to an Interface, 并不是象我曾经以为的那样简单。

路还很长啊....
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值