Jump to content

Loose coupling

From Wikipedia, the free encyclopedia

Incomputingandsystems design,aloosely coupledsystem is one

  1. in which components are weakly associated (have breakable relationships) with each other, and thus changes in one component least affect existence or performance of another component.
  2. in which each of itscomponentshas, or makes use of, little or no knowledge of the definitions of other separate components. Subareas include thecouplingofclasses,interfaces, data, and services.[1]Loose coupling is the opposite of tight coupling.

Advantages and disadvantages

[edit]

Components in a loosely coupled system can be replaced with alternative implementations that provide the same services. Components in a loosely coupled system are less constrained to the same platform,language,operating system,or build environment.

If systems are decoupled in time, it is difficult to also providetransactional integrity;additional coordination protocols are required.Data replicationacross different systems provides loose coupling (in availability), but creates issues in maintainingconsistency(data synchronization).

In integration

[edit]

Loose coupling in broaderdistributed systemdesign is achieved by the use of transactions, queues provided bymessage-oriented middleware,and interoperability standards.[2]

Four types of autonomy, which promote loose coupling, are:reference autonomy,time autonomy,format autonomy,andplatform autonomy.[3]

Loose coupling is an architectural principle and design goal inservice-oriented architectures.Eleven forms of loose coupling and their tight coupling counterparts are listed in:[4]

  • physical connections via mediator,
  • asynchronous communicationstyle,
  • simple common types only indata model,
  • weak type system,
  • data-centricand self-contained messages,
  • distributed control of process logic,
  • dynamic binding(of service consumers and providers),
  • platform independence,
  • business-level compensation rather than system-level transactions,
  • deployment at different times,
  • implicit upgrades in versioning.

Enterprise Service Bus(ESB) middleware was invented to achieve loose coupling in multiple dimensions.[5]However, overengineered and mispositioned ESBs can also have the contrary effect and create undesired tight coupling and a central architectural hotspot.

Event-driven architecturealso aims at promoting loose coupling.[6]

Methods for decreasing coupling

[edit]

Loose coupling ofinterfacescan be enhanced by publishing data in a standard format (such asXMLorJSON).

Loose coupling between program components can be enhanced by using standard data types in parameters. Passing customized data types or objects requires both components to have knowledge of the custom data definition.

Loose coupling of services can be enhanced by reducing the information passed into a service to the key data. For example, a service that sends a letter is most reusable when just the customer identifier is passed and the customer address is obtained within the service. This decouples services because services do not need to be called in a specific order (e.g. GetCustomerAddress, SendLetter).

In programming

[edit]

Coupling refers to the degree of direct knowledge that one component has of another. Loose coupling in computing is interpreted asencapsulationversus non-encapsulation.

An example of tight coupling is when a dependent class contains a pointer directly to a concrete class which provides the required behavior. The dependency cannot be substituted, or its "signature" changed, without requiring a change to the dependent class. Loose coupling occurs when the dependent class contains a pointer only to an interface, which can then be implemented by one or many concrete classes. This is known asdependency inversion.The dependent class's dependency is to a "contract" specified by the interface; a defined list of methods and/or properties that implementing classes must provide. Any class that implements the interface can thus satisfy the dependency of a dependent class without having to change the class. This allows for extensibility in software design. A new class implementing an interface can be written to replace a current dependency in some or all situations, without requiring a change to the dependent class; the new and old classes can be interchanged freely. Strong coupling does not allow this.

This is aUMLdiagram illustrating an example ofloosecoupling between a dependent class and a set of concrete classes, which provide the required behavior:

For comparison, this diagram illustrates the alternative design withstrongcoupling between the dependent class and a provider:

Other forms

[edit]

Computer programming languages having notions of either functions as the core module (seeFunctional programming) or functions as objects provide excellent examples of loosely coupled programming. Functional languages have patterns ofContinuations,Closure,or generators. SeeClojureandLispas examples of functional programming languages. Object-oriented languages likeSmalltalkandRubyhave code blocks, whereasEiffelhas agents. The basic idea is to objectify (encapsulate as an object) a function independent of any other enclosing concept (e.g. decoupling an object function from any direct knowledge of the enclosing object). SeeFirst-class functionfor further insight into functions as objects, which qualifies as one form of first-class function.

For example, in an object-oriented language, when a function of an object is referenced as an object (freeing it from having any knowledge of its enclosing host object) the new function object can be passed, stored, and called at a later time. Recipient objects (to whom these functional objects are given) can safely execute (call) the contained function at their own convenience without any direct knowledge of the enclosing host object. In this way, a program can execute chains or groups of functional objects, while safely decoupled from having any direct reference to the enclosing host object.

Phone numbers are an excellent analog and can easily illustrate the degree of this decoupling.

For example, some entity provides another with a phone number to get a particular job done. When the number is called, the calling entity is effectively saying, "Please do this job for me." The decoupling or loose coupling is immediately apparent. The entity receiving the number may have no knowledge of where the number came from (e.g. a reference to the supplier of the number). On the other side, the caller is decoupled from specific knowledge of who they are calling, where they are, and knowing how the receiver of the call operates internally.

Carrying the example a step further, the caller might say to the receiver of the call, "Please do this job for me. Call me back at this number when you are finished." The 'number' being offered to the receiver is referred to as a "Call-back". Again, the loose coupling or decoupled nature of this functional object is apparent. The receiver of the call-back is unaware of what or who is being called. It only knows that it can make the call and decides for itself when to call. In reality, the call-back may not even be to the one who provided the call-back in the first place. This level of indirection is what makes function objects an excellent technology for achieving loosely coupled programs.

Communication between loosely coupled components may be based on a flora of mechanisms, like the mentionedasynchronous communicationstyle or thesynchronous message passingstyle[7]

Measuring data element coupling

[edit]

The degree of the loose coupling can be measured by noting the number of changes indata elementsthat could occur in the sending or receiving systems and determining if the computers would still continue communicating correctly. These changes include items such as:

  1. Adding new data elements to messages
  2. Changing the order of data elements
  3. Changing the names of data elements
  4. Changing the structures of data elements
  5. Omitting data elements

See also

[edit]

References

[edit]
  1. ^Loosely Coupled: The Missing Pieces of Web ServicesbyDoug Kaye
  2. ^Pautasso C., Wilde E.,Why is the Web Loosely Coupled?Archived2021-10-12 at theWayback Machine,Proc. of WWW 2009
  3. ^F. LeymannLoose Coupling and Architectural ImplicationsArchived2016-10-02 at theWayback Machine,ESOCC 2016 keynote
  4. ^N. Josuttis, SOA in Practice. O'Reilly, 2007,ISBN978-0-596-52955-0.
  5. ^M. Keen et al,Patterns: Implementing an SOA using an Enterprise Service Bus,IBM, 2004
  6. ^How EDA extends SOA and why it is importantJack van Hoof
  7. ^Mielle, Grégoire."Microservices patterns: synchronous vs asynchronous communication".Microservices patterns: synchronous vs asynchronous communication.greeeg.Retrieved18 February2022.