13d High Cohesion

2,174 views
1,992 views

Published on

Published in: Technology
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
2,174
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
57
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

13d High Cohesion

  1. 1. TK2023 Object-Oriented Software Engineering CHAPTER 13d GRASP Patterns: High Cohesion
  2. 2. GRASP PATTERNS: HIGH COHESION <ul><li>Problem </li></ul><ul><li>How to keep objects focused, understandable, and manageable, and as a side effect, support Low Coupling? </li></ul>
  3. 3. <ul><li>In object design, cohesion is a measure of how strongly related and focused the responsibilities of an element are. </li></ul><ul><li>An element with highly related responsibilities that does not do a tremendous amount of work has high cohesion. The element can be a class, subsystem, and so on. </li></ul>
  4. 4. <ul><li>A class with low cohesion does many unrelated things or does too much work. </li></ul><ul><li>Classes with low cohesion are undesirable because they are </li></ul><ul><ul><li>hard to understand </li></ul></ul><ul><ul><li>hard to reuse </li></ul></ul><ul><ul><li>hard to maintain </li></ul></ul><ul><ul><li>delicate; constantly affected by change </li></ul></ul><ul><li>Low cohesion classes usually represent a very “large grain” of abstraction or have been assigned responsibilities that should have been delegated to other objects. </li></ul>
  5. 5. <ul><li>Solution </li></ul><ul><li>Assign a responsibility so that cohesion remains high. Use this principle to evaluate alternatives. </li></ul>
  6. 6. EXAMPLE OF APPLICATION <ul><li>In the POS application, when the Cashier enters a payment, a Payment object needs to be created and associated with the current Sale . </li></ul><ul><ul><ul><li>Who should be responsible for creating a Payment instance and associate it with Sale ? </li></ul></ul></ul>
  7. 7. <ul><li>The Creator pattern suggests assigning that responsibility to Register . </li></ul>DESIGN 1 : Register p : Payment : Sale 1. makePayment( ) 1.1. Payment( ) 1.2. addPayment( p ) {new}
  8. 8. <ul><li>If we continue to make the Register class responsible for doing some or most of the work related to more and more system operations, it will become increasingly burdened with tasks and become incohesive. </li></ul>
  9. 9. <ul><li>The following design delegates the responsibility for creating the Payment instance to Sale . </li></ul>DESIGN 2 : Register : Payment : Sale 1. makePayment( ) 1.1. makePayment( ) 1.1.1. Payment( ) {new}
  10. 10. <ul><li>In Design 1, the Register class is less cohesive. </li></ul><ul><li>Design 2 supports not only high cohesion but also low coupling. Thus, it is more desirable than Design 1. </li></ul>
  11. 11. DISCUSSION <ul><li>Like Low Coupling, High Cohesion is an evaluative principle that is applied while evaluating design decisions. </li></ul><ul><li>As a rule of thumb, 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 other objects to share the effort if the task is large. </li></ul>
  12. 12. <ul><li>Real-world analogy: A person who takes on too many unrelated responsibilities (ones that should be delegated to others) then the person is not effective. </li></ul>
  13. 13. CONTRAINDICATIONS <ul><li>There are certain cases where lower cohesion is accepted. Examples: </li></ul><ul><ul><li>Grouping of responsibilities or code into one class or component to simplify maintenance by one person. </li></ul></ul><ul><ul><li>Creating fewer and larger, less cohesive server objects to reduce remote calls and improve performance. </li></ul></ul>
  14. 14. BENEFITS <ul><li>Benefits of High Cohesion: </li></ul><ul><ul><li>Increases clarity and ease of comprehension </li></ul></ul><ul><ul><li>Simplifies maintenance and enhancements </li></ul></ul><ul><ul><li>Often supports low coupling </li></ul></ul><ul><ul><li>Increases reuse of fine-grained, highly related functionality because a cohesive class can be used for a very specific purpose. </li></ul></ul>

×