For each type of coupling, give an example of two components coupled in that way.

Design → Design Fundamentals →

Can explain coupling

Coupling is a measure of the degree of dependence between components, classes, methods, etc. Low coupling indicates that a component is less dependent on other components. High coupling (aka tight coupling or strong coupling) is discouraged due to the following disadvantages:

  • Maintenance is harder because a change in one module could cause changes in other modules coupled to it (i.e. a ripple effect).
  • Integration is harder because multiple components coupled with each other have to be integrated at the same time.
  • Testing and reuse of the module is harder due to its dependence on other modules.

In the example below, design A appears to have more coupling between the components than design B.

For each type of coupling, give an example of two components coupled in that way.


Can reduce coupling

X is coupled to Y if a change to Y can potentially require a change in X.

If the Foo class calls the method Bar#read(), Foo is coupled to Bar because a change to Bar can potentially (but not always) require a change in the Foo class e.g. if the signature of Bar#read() is changed, Foo needs to change as well, but a change to the Bar#write() method may not require a change in the Foo class because Foo does not call Bar#write().

Some examples of coupling: A is coupled to B if,

  • A has access to the internal structure of B (this results in a very high level of coupling)
  • A and B depend on the same global variable
  • A calls B
  • A receives an object of B as a parameter or a return value
  • A inherits from B
  • A and B are required to follow the same data format or communication protocol


Can identify types of coupling

Some examples of different coupling types:

  • Content coupling: one module modifies or relies on the internal workings of another module e.g., accessing local data of another module
  • Common/Global coupling: two modules share the same global data
  • Control coupling: one module controlling the flow of another, by passing it information on what to do e.g., passing a flag
  • Data coupling: one module sharing data with another module e.g. via passing parameters
  • External coupling: two modules share an externally imposed convention e.g., data formats, communication protocols, device interfaces.
  • Subclass coupling: a class inherits from another class. Note that a child class is coupled to the parent class but not the other way around.
  • Temporal coupling: two actions are bundled together just because they happen to occur at the same time e.g. extracting a contiguous block of code as a method although the code block contains statements unrelated to each other


COUPLING

An indication of the strength of interconnections between program units.

Highly coupled have program units dependent on each other. Loosely coupled are made up of units that are independent or almost independent.

Modules are independent if they can function completely without the presence of the other. Obviously, can't have modules completely independent of each other. Must interact so that can produce desired outputs. The more connections between modules, the more dependent they are in the sense that more info about one modules is required to understand the other module.

Three factors: number of interfaces, complexity of interfaces, type of info flow along interfaces.

Want to minimize number of interfaces between modules, minimize the complexity of each interface, and control the type of info flow. An interface of a module is used to pass information to and from other modules.

In general, modules tightly coupled if they use shared variables or if they exchange control info.

Loose coupling if info held within a unit and interface with other units via parameter lists. Tight coupling if shared global data.

If need only one field of a record, don't pass entire record. Keep interface as simple and small as possible.

Two types of info flow: data or control.

  • Passing or receiving back control info means that the action of the module will depend on this control info, which makes it difficult to understand the module.
  • Interfaces with only data communication result in lowest degree of coupling, followed by interfaces that only transfer control data. Highest if data is hybrid.
Ranked highest to lowest:
  1. Content coupling: if one directly references the contents of the other.

    When one module modifies local data values or instructions in another module. (can happen in assembly language)

    if one refers to local data in another module.

    if one branches into a local label of another.

  2. Common coupling: access to global data.

    modules bound together by global data structures.

  3. Control coupling: passing control flags (as parameters or globals) so that one module controls the sequence of processing steps in another module.

  4. Stamp coupling: similar to common coupling except that global variables are shared selectively among routines that require the data. E.g., packages in Ada. More desirable than common coupling because fewer modules will have to be modified if a shared data structure is modified. Pass entire data structure but need only parts of it.

  5. Data coupling: use of parameter lists to pass data items between routines.

COHESION

Measure of how well module fits together.

A component should implement a single logical function or single logical entity. All the parts should contribute to the implementation.

Many levels of cohesion:

  1. Coincidental cohesion: the parts of a component are not related but simply bundled into a single component.

    harder to understand and not reusable.

  2. Logical association: similar functions such as input, error handling, etc. put together. Functions fall in same logical class. May pass a flag to determine which ones executed.

    interface difficult to understand. Code for more than one function may be intertwined, leading to severe maintenance problems. Difficult to reuse

  3. Temporal cohesion: all of statements activated at a single time, such as start up or shut down, are brought together. Initialization, clean up.

    Functions weakly related to one another, but more strongly related to functions in other modules so may need to change lots of modules when do maintenance.

  4. Procedural cohesion: a single control sequence, e.g., a loop or sequence of decision statements. Often cuts across functional lines. May contain only part of a complete function or parts of several functions.

    Functions still weakly connected, and again unlikely to be reusable in another product.

  5. Communicational cohesion: operate on same input data or produce same output data. May be performing more than one function. Generally acceptable if alternate structures with higher cohesion cannot be easily identified.

    still problems with reusability.

  6. Sequential cohesion: output from one part serves as input for another part. May contain several functions or parts of different functions.

  7. Informational cohesion: performs a number of functions, each with its own entry point, with independent code for each function, all performed on same data structure. Different than logical cohesion because functions not intertwined.

  8. Functional cohesion: each part necessary for execution of a single function. e.g., compute square root or sort the array.

    Usually reusable in other contexts. Maintenance easier.

  9. Type cohesion: modules that support a data abstraction.

    Not strictly a linear scale. Functional much stronger than rest while first two much weaker than others. Often many levels may be applicable when considering two elements of a module. Cohesion of module considered as highest level of cohesion that is applicable to all elements in the module.


Adam Carlson

What are different types of coupling give one example of each type?

Types of coupling.
Content coupling: Is when one class modifies the content of another class. ... .
Common coupling: Is when two classes access the same shared data (e.g., a global variable)..
Control coupling: When one function controls the flow of another function..

What is coupling give an example?

Coupling is the degree to which one class knows about another class. Let us consider two classes class A and class B. If class A knows class B through its interface only i.e. it interacts with class B through its API then class A and class B are said to be loosely coupled.

What is coupling name two types of coupling?

Couplings fall into two main categories: Material Flexing and Mechanical Flexing. The material flexible types obtain their flexibility from stretching or compressing a resilient material, such as rubber, or from the flexing of thin metallic discs or grid.

What are the two types of coupling in object oriented systems?

Common coupling -- this is a type of coupling in which you have multiple modules having access to a shared global data. Stamp coupling -- this is a type of coupling in which data structure is used to pass information from one component in the system to another.