What makes coupling "loose" and is it better to write my code loosely coupled? [duplicate]
Possible Duplicate:
What 开发者_开发技巧is “loose coupling?” Please provide examples.
What makes coupling "loose" and is it better to write my code loosely coupled?
** please provide me with some links.
It's a quite vast topic, you should start looking at GRASP patterns.
Loose coupling between 2 classes means each class has very few knowledge of the internal behavior of the other class. You may have a higher degree of "coupling" between classes which belong to the same "module" or "package", and it's not a bad practice : it makes your model more understandable than if you had only low coupled classes.
Loose coupling is an architecture, which consists of modules. That modules access each other via some interfaces (public methods are OK, but even more untangled ways exist). Modules need not one another for compiling (generally) and are easily changed to another one with the same interface.
There are a few examples in the related question What is "loose coupling".
As to whether your code should be loosely coupled, that rather depends what the system it is implementing is.
For example, the web is loosely coupled - any component goes through the same http interface, and you can switch the implementations of components and providers of services willy-nilly anywhere in the layers from hardware to application.
On the other hand, the driver for a high performance graphics card is tightly coupled to the hardware it is designed for - anything else, and you lose performance. Whereas the interface between the OS and the driver is less tightly coupled, being common to all drivers for a given OS version, and the interface between application code and the OS is often less tightly coupled again, perhaps conforming to a long lived standard such as OpenGL.
Where your code fits between these extremes will determine what degree of coupling is appropriate.
By decreasing the dependency you make coupling more loose ..
Loose coupling is a software-development approach that values the importance of making pieces interchangeable. If two pieces of code are loosely coupled, then changes made to one of the pieces will have little or no effect on the other.
Here are some links ..
http://programmingexamples.wikidot.com/glossary:loose-coupling
http://www.artima.com/weblogs/viewpost.jsp?thread=6544
http://c2.com/cgi/wiki?CouplingAndCohesion
http://earthli.com/news/view_article.php?id=2507
http://www.codeproject.com/KB/cs/DynamicInvoking.aspx
精彩评论