1

i know loosely coupled and tightly coupled information. But i suspend when can i decide where and what time use? i dont understand when i need loosely coupled and tightly coupled ?

look please : http://www.dofactory.com/Patterns/PatternAdapter.aspx#_self1

if you look adapter class :


  /// 
  /// The 'Adapter' class
  /// 
  class Adapter : Target
  {
    private Adaptee _adaptee = new Adaptee();

    public override void Request()
    {
      // Possibly do some other work
      //  and then call SpecificRequest
      _adaptee.SpecificRequest();
    }
  }

Above usage like tightly coupled! i think that tightly coupled is bad usage. But adapter pattern used tightly coupled. When i need tightly and loosely coupled?

loki
  • 2,926
  • 8
  • 62
  • 115
  • Tightly coupled classes becomes a bad design if your application grows as an enterprise level application. To understand you can refer to the PRISM (or Composite Application Block) by microsoft. It is one such design using loosely coupled classes. – Priyank Thakkar Oct 11 '12 at 12:04

5 Answers5

5

Remember that Gang Of Four patterns were born before Dependency Injection which is mentioned a lot as loose coupling.

So, the example Adapter pattern you show is to trying to show how pattern works and does not care much loose coupling.

If you want your code testable and replaceable, loose coupling should be used, example:

class CustomerService
{
    private ICustomerRepository _customerRepository;
    public CustomerService(ICustomerRepository customerRepository)
    {
        _customerRepository = customerRepository;
    }
}

To inject customerRepository via constructor, it makes easy for you to mock ICustomerRepository in order to do Unit Test.

cuongle
  • 74,024
  • 28
  • 151
  • 206
2

Generally, you want to aim for loose-coupling, which means working with abstractions when possible.

The adapter pattern will help you do this when you can't easily replace a class (Adaptee) with an appropriate abstraction.

The Adapter is a wrapper around the problem Adaptee class. The Adapter derives from the abstract Target class, and calling code should only deal in terms of the Target, so it can remain loosely-coupled.

Stewart Ritchie
  • 926
  • 7
  • 13
1

This will help you to understand the difference between them, yet when you need a loosely coupled classes, it highly depends on the design of your application. What is the difference between loose coupling and tight coupling in the object oriented paradigm?

Community
  • 1
  • 1
Priyank Thakkar
  • 4,752
  • 19
  • 57
  • 93
1

When i need tightly and loosely coupled?

You should always strive for a loosely-coupled design. There may be edge cases that require tight-coupling or you've inherited a project that has it, but I think your mantra should be design and develop with testability in mind and this means low coupling. I have yet to work on a project that was too "loosely-coupled", but certainly have worked on ones that were "tightly-coupled" and the latter was no fun.

Big Daddy
  • 5,160
  • 5
  • 46
  • 76
0

It's better to go with loosely coupled design for the production code. This makes your code easy to be tested independently of your dependent components, and it's easy to replace the components your code is using.

For a detailed explanation, visit http://rangahc.blogspot.com/2015/05/difference-between-tight-coupling-and-loose-coupling.html

ragingasiancoder
  • 616
  • 6
  • 17
Ranganatha
  • 1,157
  • 14
  • 32