13d High Cohesion
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

13d High Cohesion

on

  • 2,158 views

 

Statistics

Views

Total Views
2,158
Views on SlideShare
2,158
Embed Views
0

Actions

Likes
0
Downloads
44
Comments
1

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

13d High Cohesion Presentation Transcript

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