1 / 14

TK2023 Object-Oriented Software Engineering

TK2023 Object-Oriented Software Engineering. CHAPTER 13d GRASP Patterns: High Cohesion. GRASP PATTERNS: HIGH COHESION. Problem How to keep objects focused, understandable, and manageable, and as a side effect, support Low Coupling?.

garymaxwell
Télécharger la présentation

TK2023 Object-Oriented Software Engineering

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. 1. makePayment( ) 1.1. Payment( ) p : Payment : Register {new} 1.2. addPayment(p) : Sale DESIGN 1 • The Creator pattern suggests assigning that responsibility to Register.

  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. 1. makePayment( ) : Register 1.1. makePayment( ) 1.1.1. Payment( ) : Sale : Payment {new} DESIGN 2 • The following design delegates the responsibility for creating the Payment instance to Sale.

  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) becomes an ineffective person.

  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.

More Related