Monday, October 17, 2005

Loose coupling between layers of an application

I was listening to Mario Cardinal's podcast discussion with George Hohpe, where they discuss the layering pattern and issues surrounding it.

Mario asks George, how can we loosen the coupling between layers, and George replies among other things, one can:

* Wrap lower layer exceptions in the upper layer's exception type and not expose them to the layer above. George thinks, this is almost always a good idea.
* Use the all mighty - Program to interfaces - even though the interface which is in the current layer, but has some or all of its implementations in the lower layer or even on layers at the same level, it is OK to depend on the interface. And use dependency injection to plug and play implementations at run-time, without actually being dependent on any one of them. This is also very useful for testing and mocking objects
* Use events - asynchronous or not (something I am adding) - so the upper layer can subscribe to some events of interest happening in the lower layers and can and get notified when they occur - aka Observer pattern - but applied to objects that could be potentially in different layers. Events are great to communicate messages and notifications without being coupled to the implementations that generate those events. The good part here is that the lower layer does not which objects in the layer above are listening to the events emittted.

Above all this, George says, no matter what, it takes only one bad coupling to break the entire loose coupling between layers, that's why this is sometimes so hard to get it right, especially in deployment scenarios.

You can listen to the podcast on Mario's website. This is his show # 7. He plans to do this oftenly and like his style of thinking and his questions are interesting. I plan to catch up with his podcasts, since I have used the Observer design pattern and asynchronously subsribed to his podcast notifications :)

No comments:

Post a Comment

© 2008 Vishal V. Shah. All rights reserved. Contents of this web site reflect my personal work and is not representative of my employer.