views:

488

answers:

7

I was reading Loose Coupling and its benefits which are really good stuff but I started wondering which tools are great to create loosely-coupled solutions ? First what came to my mind are Type and Interfaces and Abstract classes but I am sure there are so many ways to provide loose coupling. Maybe Polymorphism helps to create loosely-coupled objects and systems.

Thanks.

A: 

A great way to loosen coupling is Dependency Injection.

Vinko Vrsalovic
+1  A: 
  • Good OOD
  • Use of Interfaces
  • IoC (DI such as Microsoft.Practices.Unity or Windsor etc)
  • Appropriate design patterns (MVC, n-tier etc)
Paul Creasey
+7  A: 

This is a very broad question - possibly too broad to answer.

However, two methodologies that can provide a solution with "loose coupling" are:

Dependency Injection

Dependency Injection (especially when using an Inversion of Control Container) provides loose coupling within an application or solution - With dependency injection you do not hard code your references to dependent objects, instead you code to interfaces and inject implementations of those interfaces.

Here is a quick code example of a Logging component of an application, that implements an ILogger interface

public class ConcreteLogger : ILogger
{

    public LogMessage(string message)
    {
        Log.Write(message);
    }
}

One of your classes then receives this concrete implementation of the logger interface.

public class MyClass
{
    private ILogger logger;

    public myClass(ILogger logger)
    {
        this.logger = logger;
    }

public void DoSomething()
{
    // Now if DoSomething needs to call logging it can call what ever ILogger was passed in
    this.logger.Log("We did something");
}

Service Orientation

Service Orientation provides loose coupling between different subsystems of a solution - With Service Orientation each part of a solution becomes its own stand alone services that publishes and interfaces (often a messaging based interface).

This means that an application when needing for example to talk to the Shipping subsystem of a solution only needs to know about the systems interfaces and its address, and does not need to know anything about its internal implementation. This can almost be seen as a broader application of basic OO principles.


Both of these provide two different types of loose coupling (that is one of the problems with your question, the term it self is loosely defined)

David Hall
This is also what I was thinking but I didn't know it is called Dependency Injection. Thanks for clarifying things. Very useful answer by the way. Giving examples made it more comprehensible.
Braveyard
Glad I gave you somewhere to start - I'm a bit of a dependency injection junkie, it really opened up lots of possibilities in my designs when I started using it. Do investigate IOC containers too, these take care of the heavy lifting of doing dependency injection.
David Hall
+1  A: 

Your brain presumably?

Program with interfaces in mind. Build you libraries as small islands of self-contained code with minimal and explicit interfaces. Limit scope of all symbols, but in particular mutable ones (eg. variables). Don't give away implementation details, unless they are relevant.

troelskn
+1  A: 

Others have already provided great answers but I want to add one thought: polymorphism as implemented using inheritance does not promote loose-coupling. Inheritance is a very strong coupling between base classes and derived classes.

Jason
A: 

The best tool by far is Microsoft CAB (which includes the previously mentioned Unity framework)
It includes tools for dependency injection, loosely-coupled event handling, and much more.

BlueRaja - Danny Pflughoeft
The link you provided shows that MS CAB falls under the "retired" section. Probably not good advice?
David Archer
The dependency injection is here ( http://msdn.microsoft.com/en-us/library/dd203101.aspx ) while the loosely-coupled event handling is here ( http://msdn.microsoft.com/en-us/library/dd458809.aspx ). It is the same thing, just split up. There were other parts of CAB I am not familiar with, but I'm sure it's all there under 'patterns and practices.'
BlueRaja - Danny Pflughoeft
+1  A: 

MEF is the best tool for loose coupling! Design your apps from the start to be very MEF/plugin friendly, and you'll find that your app will be very loosely coupled

Paul Betts