"Programming to an Interface, not an Implementation"

博客围绕面向对象编程中的接口展开,指出接口代表一组契约或基本行为,能让开发者切换实现而无需反复修改代码。谨慎的程序员会合理使用接口避免程序耦合,而新手可能滥用。还提到使用工厂模式,但实际编程中仍面临问题,说明面向接口编程并非易事。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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, 并不是象我曾经以为的那样简单。

路还很长啊....
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值