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
知难行易?知易行难?
这样的代码不是仍然在我的程序里出现,甚至所谓的Factory Method不是照样返回一个concrete class?
programming to an Interface, 并不是象我曾经以为的那样简单。
路还很长啊....
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, 并不是象我曾经以为的那样简单。
路还很长啊....