Presentation is loading. Please wait.

Presentation is loading. Please wait.

Low Coupling High Cohesion

Similar presentations


Presentation on theme: "Low Coupling High Cohesion"— Presentation transcript:

1 Low Coupling High Cohesion
We use the same example: “creating a payment” Both patterns happen to suggest the same collaboration

2 Low Coupling When we need to assign a responsibility to a class, we should do so such that coupling remains low. Coupling: a measure of how strongly one element is connected to, has knowledge of, or relies on other elements. Low coupling: not dependent on too many other elements High coupling results in classes that are harder to understand in isolation changes to related classes force local changes classes that are harder to reuse

3 Low Coupling Example Assume we need to create a Payment instance … what class should do this?

4 Creator pattern suggests Register
Figure Register creates Payment (Collaboration diagram)

5 An alternative design is given in Figure 16.10
Which of the two designs, figures 16.9 and 16.10, supports lower coupling?

6 High Cohesion How do we assign responsibilities so that cohesion remains high? Cohesion: a measure of how strongly related and focused the responsibilities of an element are. a measure of how single purpose the attributes and behavior within a class are Better that attributes and behavior in classes be related. A class with highly related responsibilities and which does not do excessive amounts of work has high cohesion

7 High Cohesion When we need to assign a responsibility to a class, we should do so such that cohesion remains high cohesion: a measure of how strongly related and focussed the responsibilities of an object are. a measure of how single purpose the attributes and behavior within a class are It is better that attributes and behavior in classes be related.

8 High Cohesion Rule of Thumb (ROT): A class with high cohesion has a relatively small number of methods, with highly related functionality, and does not do too much work … it collaborates with others to get work done. Low cohesion results in classes that are hard to comprehend hard to reuse hard to maintain delicate - constantly affected by change

9 High Cohesion Example Assume we need to create a Payment instance … what class should do this?

10 First solution given in Figure 16
First solution given in Figure (note this Sequence Diagram represents the same logic as the Collaboration diagram in Figure 16.9)

11 Figure 16.12 gives a second solution
(note this Sequence Diagram represents the same logic as the Collaboration diagram in Figure 16.10) Which solution supports higher cohesion for Register?

12 Note, when considering which class to assign the responsibility of creating a payment, the text arrives at the same solution when applying both principles


Download ppt "Low Coupling High Cohesion"

Similar presentations


Ads by Google