Forum Stats

  • 3,874,742 Users
  • 2,266,769 Discussions
  • 7,911,961 Comments

Discussions

Cohesion and coupling examples

User_19BPU
User_19BPU Member Posts: 1,086 Blue Ribbon
edited May 14, 2012 4:56AM in New To Java
Hi,


Increased cohesion and decreased coupling do lead to good software design.

Decoupling allows you to change the implementation without affecting other parts of your software.
Cohesion ensures that the implementation more specific to functionality and at the same time easier to maintain.
he most effective method of decreasing coupling and increasing cohesion is design by interface.

Example of Cohesion:-

Overloading a method of a class with more functionality will result in low cohesion. Hence to avoid that break the methods in to
small chunks (packets) and perform the operations which will result in better re usability and maintainability of the code.


Example of Coupling:-

Abstraction using a interface to invoke the respective methods in an action class without directly exposing the Implementation class by creating
an instance.

Please correct me if I have gone wrong. If so please do clarify.

Thanks.

Answers

  • User_19BPU
    User_19BPU Member Posts: 1,086 Blue Ribbon
    Guys any response to this. I hope that I have asked in the correct topic.
  • 912896
    912896 Member Posts: 5
    Your thinking is correct.
  • >
    Overloading a method of a class with more functionality will result in low cohesion.
    >
    That isn't what overloading is. Overloading isn't used to add more functionality. It is used as one of the ways to implement ploymorphism.

    Overloaded methods typically have the same functionality but a different number or type of parameters.
    >
    break the methods in to small chunks (packets)
    >
    The number of size of chunks isn't necessarily related to the level of cohesion. A 'chunk' should be big enough to perform the required functionality and no bigger.

    See the wiki for some definitions of these terms.

    For Cohesion - http://en.wikipedia.org/wiki/Cohesion_(computer_science)
    >
    In computer programming, cohesion is a measure of how strongly-related each piece of functionality expressed by the source code of a software module is.
    >
    So a class with mutiple types of functionality could, in some cases, have low cohesion but even then it can depend on the purpose of the class. For example a file utility class may have high cohesion with no overloading if its methods are all related to fiile handling.

    For Coupling - http://en.wikipedia.org/wiki/Coupling_(computer_programming)
    >
    In software engineering, coupling or dependency is the degree to which each program module relies on each one of the other modules.
  • jschellSomeoneStoleMyAlias
    jschellSomeoneStoleMyAlias Member Posts: 24,877 Gold Badge
    797836 wrote:
    Increased cohesion and decreased coupling do lead to good software design.
    As in all things - when done in moderation.
    Decoupling allows you to change the implementation without affecting other parts of your software.
    Cohesion ensures that the implementation more specific to functionality and at the same time easier to maintain.
    he most effective method of decreasing coupling and increasing cohesion is design by interface.
    When done correctly. It is possible to do exactly the above and actually do the opposite of the stated goals.
  • 937081
    937081 Member Posts: 2
    In simple terms

    Coupling means : the degree to which your one Class knows about other 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.
    If on the other hand class A apart from interacting class B by means of its interface also interacts through the
    non-interface stuff of class B then they are said to be tightly coupled.
    Suppose the developer changes the class B‘s non-interface part i.e non API stuff then in case of loose coupling
    class A does not breakdown but tight coupling causes the class A to break.


    Cohesion means : Your one class have focus on only one objective.
    e.g. Login class contains only login related logic,
    whereas UserInfo class contains only logic of fetching customer information from database.

    Benefits of Higher cohesion
    1. Highly cohesive class are easier to maintain and frequently changed.
    2. Such classes are more usable than others as they are designed with well focused purpose.
This discussion has been closed.