1

I'm new to java patterns and I'm trying to figure out how it fits in to the REAL WORLD. Most sites and books on patterns seems to be written by non-programmers.

I'm trying to define how patterns help with coupling, and this is my definition so far. What I would like to know is what patterns are really useful for loose coupling, and are they worth the effort. Also, is my definition/understanding correct so far:

"Coupling is the degree, two or more different objects, accesses and/or interacts with each other."

Tight coupling between two objects:

  • Referencing/Instantiation : Many reverence to the other object, in many places, in one or both objects (many to many references)

  • Complexity : Usually many parameters required accessing functions, or the sequence of accessing different functions. No common interface for related objects.

  • Responsibility : Doing work that should rather be done in the object being accessed, or another object. Accessing nested functions directly.

  • Performance : Biggest reason why tight coupling is sometimes required, but should be minimized.

Loose coupling between two objects:

  • Referencing/Instantiation : Few, but at least one reference in one object but not in both (one to few references)
    Patterns that help : Factory, Singleton, Builder, Composite

  • Complexity : Few, well defined parameters (usually defined by an interface), with least possible sequence of functions (exp. open, fetch, close)
    Patterns that help : Adaptor, Bridge, Decorator, Facade, Command 

  • Responsibility : Only do work the object is responsible for doing and try to only access functions one level down.
    Patterns that help : Decorator, Chain of Responsibility, MVC

  • Performance : Identify where performance needs to be, and keep those classes together - maybe even as nested classes, per definition tightly coupled.

EarlB
  • 111
  • 2
  • 7
  • See some of the other posts on the topic (http://blog.ploeh.dk/2012/02/02/LooseCouplingandtheBigPicture/) – megawac Oct 17 '13 at 11:18

1 Answers1

1

The pattern that is considered the best for loose coupling is IoC: Inversion of Control, made using the Dependency Injection.

Basically if a class Alfa needs another object of class Beta, you have to instanciate manually Beta and pass it to Alfa (injection):

Beta myDependencyObject ....;
Alfa myObjectWithDependency(myDependencyObject);

In this way you expose the dependecy that Alfa has with Beta.

dynamic
  • 46,985
  • 55
  • 154
  • 231