Apply Expert, Creator, Controller, Low Coupling, High Cohesion Overview A critical skill is designing or thinking in objects. This can be practiced based on explainable principles. We will start applying these. You will be able to: Apply Expert, Creator, Controller, Low Coupling, High Cohesion Design for low representational gap Design for visibility Define use case realizations Relate the UP artifacts interaction diagrams designing objects design class diagrams mapping code 1
MOTIVATION: Introduction Now what happens? What object should receive this message? What objects should interact to fulfill the request, and how? interaction diagrams designing objects design class diagrams mapping code 2
DEFINITION: Responsibility-Driven Design (RDD) Detailed object design is usually done from the point of view of the metaphor of: Objects have responsibilities Objects collaborate In RDD we do object design such that we will ask questions such as: What are the responsibilities of this object? Who does it collaborate with? interaction diagrams designing objects design class diagrams mapping code 3
DEFINITION: Responsibilities Responsibilities are an abstraction. The responsibility for persistence. Large-grained responsibility. The responsibility for the sales tax calculation. More fine-grained responsibility. They are implemented with methods in objects. 1 method in 1 object 5 methods in 1 object 50 methods across 10 objects interaction diagrams designing objects design class diagrams mapping code 4
“Façade” “Information Expert” … They provide a vocabulary of design. DEFINITION: Patterns Patterns are named problem-solution pairs to common problems, typically showing a popular, robust solution. “Façade” “Information Expert” … They provide a vocabulary of design. interaction diagrams designing objects design class diagrams mapping code 5
DEFINITION: GRASP Principles What guiding principles to help us assign responsibilities? These principles are captured in the GRASP patterns. General Responsibility Assignment Software Patterns. Very fundamental, basic principles of object design. interaction diagrams designing objects design class diagrams mapping code 6
The 9 GRASP Principles Expert Creator Controller Low Coupling High Cohesion Polymorphism Pure Fabrication Indirection Protected Variations interaction diagrams designing objects design class diagrams mapping code 7
PATTERN: Information Expert What is most basic, general principle of responsibility assignment? Assign a responsibility to the object that has the information necessary to fulfill it. “That which has the information, does the work.” E.g., What software object calculates sales tax? What information is needed to do this? What object or objects has the majority of this information. interaction diagrams designing objects design class diagrams mapping code 8
Designing for Low Representational Gap interaction diagrams designing objects design class diagrams mapping code 9
Designing for Low Representational Gap Contraindication: We do not design for low representational gap regarding actors. E.g., we don’t make a software class Clerk do all the work in the software system. Why?? interaction diagrams designing objects design class diagrams mapping code 10
Designing for Visibility Fact: To send a message to B, A must have visibility to B. It doesn’t happen by “magic.” Kinds of visibility: Attribute Parameter Local Global interaction diagrams designing objects design class diagrams mapping code 11
DEFINITION: Attribute Visibility interaction diagrams designing objects design class diagrams mapping code 12
DEFINITION: Parameter Visibility interaction diagrams designing objects design class diagrams mapping code 13
DEFINITION: Local Visibility interaction diagrams designing objects design class diagrams mapping code 14
What object creates an X? PATTERN: Creator What object creates an X? Ignores special-case patterns such as Factory. Choose an object C, such that: C contains or aggregates X C closely uses X C has the initializing data for X The more, the better. interaction diagrams designing objects design class diagrams mapping code 15
PATTERN: Controller What object in the domain (or application coordination layer) receives requests for work from the UI layer? interaction diagrams designing objects design class diagrams mapping code 16
Solution: Choose a class whose name suggests: PATTERN: Controller Solution: Choose a class whose name suggests: The overall “system,” device, or subsystem A kind of Façade class Or, represents the use case scenario or session interaction diagrams designing objects design class diagrams mapping code 17
Cohesion Coupling PATTERN: Low Coupling Assign responsibilities so that coupling remains low. What does low coupling mean? Coupling Cohesion interaction diagrams designing objects design class diagrams mapping code 18
PATTERN: High Cohesion Assign responsibilities so that cohesion remains high? What does high cohesion mean? Coupling Cohesion interaction diagrams designing objects design class diagrams mapping code 19
DEFINITION: Use Case Realizations In the vocabulary of the UP and use case modeling, when we design the objects to handle the systems operation for a scenario, we are designing a use case realization. interaction diagrams designing objects design class diagrams mapping code 20