GRASP*: Designing Objects with Responsibilities

GRASP*: Designing Objects with Responsibilities

GRASP*: Designing Objects with Responsibilities * General Responsibility Assignment Software Patterns Ch. 17 1 Responsibilities and Methods The focus of object design is to identify classes and objects, decide what methods belong where and how these objects should interact. Responsibilities are related to the obligations of an object in terms of its behavior. Two types of responsibilities: Doing: Doing something itself (e.g. creating an object, doing a calculation) Initiating action in other objects. Controlling and coordinating activities in other objects.

Knowing: Knowing about private encapsulated data. Knowing about related objects. Knowing about things it can derive or calculate. 2 Responsibilities and Methods Responsibilities are assigned to classes during object design. For example, we may declare the following: a Sale is responsible for creating SalesLineItems (doing) a Sale is responsible for knowing its total (knowing) Responsibilities related to knowing are often inferable from the Domain Model (because of the attributes and associations it illustrates) 3

Responsibilities and Methods The translation of responsibilities into classes and methods is influenced by the granularity of responsibility. A responsibility is not the same thing as a method, but methods are implemented to fulfill responsibilities. Methods either act alone, or collaborate with other methods and objects. 4 Responsibilities and Interaction Diagrams Sale objects have been given the responsibility to create Payments, handled with the makePayment method.

:Sale makePayment() create() :Payment 5 Patterns We will emphasize principles (expressed in patterns) to guide choices in where to assign responsibilities. A pattern is a named description of a problem and a solution that can be applied to new contexts; it provides advice in how to apply it in varying circumstances. For example, Pattern name: Information Expert Problem: What is the most basic principle by which to assign responsibilities

to objects? Solution: Assign a responsibility to the class that has the information needed to fulfil it. 6 Information Expert (or Expert) Problem: what is a general principle of assigning responsibilities to objects? Solution: Assign a responsibility to the information expert - the class that has the information necessary to fulfill the responsibility. In the NextGen POS application, who should be responsible for knowing the grand total of a sale? By Information Expert we should look for that class that has the information needed to determine the total. 7

Information Expert (or Expert) Do we look in the Domain Model or the Design Model to analyze the classes that have the information needed? A: Both. Assume there is no or minimal Design Model. Look to the Domain Model for information experts. 8 Information Expert (or Expert) Sale date time Contains 1..* SalesLineItem quantity

Described-by * 1 Product Specification description price itemID It is necessary to know about all the SalesLineItem instances of a sale and the sum of the subtotals.

A Sale instance contains these, i.e. it is an information expert for this responsibility. 9 Information Expert (or Expert) t := getTotal() This is a partial interaction diagram. :Sale 10

Information Expert (or Expert) t := getTotal() What information is needed to determine the line item subtotal? quantity and price. :Sale 1 *: st := getSubtotal() :SalesLineItem SalesLineItem should determine the subtotal. This means that Sale needs to send getSubtotal() messages to

each of the SalesLineItems and sum the results. 11 Information Expert (or Expert) t := getTotal() :Sale 1 *: st := getSubtotal() :SalesLineItem To fulfil the responsibility of knowing and answering its subtotal, a SalesLineItem needs to know the product price.

The ProductSpecification is the information expert on answering its price. 1.1: p := getPrice() :ProductSpecification 12 Information Expert (or Expert) Class Sale Responsibility Knows Sale total SalesLineItem

ProductSpecification Knows line item total Knows product price To fulfil the responsibility of knowing and answering the sales total, three responsibilities were assigned to three design classes The fulfillment of a responsibility often requires information that is spread across different classes of objects. This implies that there are many partial experts who will collaborate in the task. 13

Creator Problem: Who should be responsible for creating a new instance of some class? Solution: Assign class B the responsibility to create an instance of class A if one or more of the following is true: 1. 2. 3. 4. B aggregates A objects. B contains A objects. B records instances of A objects. B has the initializing data that will be passed to A when it is created (thus B is an

Expert with respect to creating A). 14 Creator Sale date time Contains 1..* SalesLineItem quantity Described-by * 1

Product Specification description price itemID In the POS application, who should be responsible for creating a SalesLineItem instance? Since a Sale contains many SalesLineItem objects, the Creator pattern suggests that Sale is a good candidate.

15 Creator This assignment of responsibilities requires that a makeLineItem method be defined in Sale. :Sale makeLineItem(quantity) create(quantity) :SalesLineItem 16

Low Coupling Coupling: it is a measure of how strongly one element is connected to, has knowledge of, or relies upon other elements. A class with high coupling depends on many other classes (libraries, tools). Problems because of a design with high coupling: Changes in related classes force local changes. Harder to understand in isolation; need to understand other classes. Harder to reuse because it requires additional presence of other classes. Problem: How to support low dependency, low change impact and increased reuse? Solution: Assign a responsibility so that coupling remains low. 17 Low Coupling :Register

:Payment :Sale Assume we need to create a Payment instance and associate it with the Sale. What class should be responsible for this? By Creator, Register is a candidate. 18 Low Coupling makePayment()

1: create() :Register p:Payment 2:addPayment(p) :Sale Sale also coupled to knowledge of a Payment. Register could then send an addPayment message to Sale, passing along the new Payment as a parameter. The assignment of

responsibilities couples the Register class to knowledge of the Payment class. 19 Low Coupling makePayment() 1: makePayment() :Register :Sale 1.1. create() An alternative solution is to create Payment and

associate it with the Sale. No coupling between Register and Payment. :Payment 20 Low Coupling Some of the places where coupling occurs: Attributes: X has an attribute that refers to a Y instance. Methods: e.g. a parameter or a local variable of type Y is found in a method of X.

Subclasses: X is a subclass of Y. Types: X implements interface Y. There is no specific measurement for coupling, but in general, classes that are generic and simple to reuse have low coupling. There will always be some coupling among objects, otherwise, there would be no collaboration. 21 High Cohesion Cohesion: it is a measure of how strongly related and focused the responsibilities of an element are. A class with low cohesion does many unrelated activities or does too much work. Problems because of a design with low cohesion:

Hard to understand. Hard to reuse. Hard to maintain. Delicate, affected by change. Problem: How to keep complexity manageable? Solution: Assign a responsibility so that cohesion remains high. 22 High Cohesion :Register :Sale

makePayment() create() p:Payment Assume we need to create a Payment instance and associate it with Sale. What class should be responsible for this? By Creator, Register is a candidate. Register may become bloated if it is assigned more and more system operations. addPayment(p)

23 High Cohesion :Register :Sale makePayment() makePayment() create() :Payment An alternative design delegates the Payment creation responsibility to

the Sale, which supports higher cohesion in the Register. This design supports high cohesion and low coupling. 24 High Cohesion Scenarios that illustrate varying degrees of functional cohesion 1. Very low cohesion: class responsible for many things in many different areas.

e.g.: a class responsible for interfacing with a data base and remote-procedurecalls. 2. Low cohesion: class responsible for complex task in a functional area. e.g.: a class responsible for interacting with a relational database. 25 High Cohesion 3. High cohesion: class has moderate responsibility in one functional area and it collaborates with other classes to fulfill a task.

e.g.: a class responsible for one section of interfacing with a data base. Rule of thumb: a class with high cohesion has a relative low number of methods, with highly related functionality, and doesnt do much work. It collaborates and delegates. 26 Controller Problem: Who should be responsible for handling an input system event? Solution: Assign the responsibility for receiving or handling a system event message to a class representing one of he following choices: Represents the overall system. Represents a use case scenario. A Controller is a non-user interface object that defines the method for the system operation. Note that windows, applets, etc. typically receive events and delegate

them to a controller. 27 Fig. 17.9 Fig. 17.10 Fig. 17.11 Fig. 17.21 presses button : Cashier actionPerformed( actionEvent ) UI Layer

:SaleJFrame enterItem(itemID, qty) Domain Layer : ??? system operation message Which class of object should be responsible for receiving this system event message? It is sometimes called the controller or coordinator. It does not normally do the work, but delegates it to other objects. The controller is a kind of "facade" onto the domain layer from the interface layer.

Recently Viewed Presentations

  • Christianity in Episcopal Tints 1. Our History Christianity

    Christianity in Episcopal Tints 1. Our History Christianity

    John Keble, 1792-1866, English . Churchman and poet, one of the. Leaders of the Oxford Movement. Catholic Revival or Oxford Movement. The movement emphasized: The church is a . divine society, with a . sacramental relationship to God
  • 幻灯片 1 - University of Rochester

    幻灯片 1 - University of Rochester

    Inoue, et al., "Organic Thin-file transistors with high electron mobility based on perfluoropentacene," Japan Society of Applied Physis, Vol. 44, No. 6A pp. 3663-3668, 2005 Gao Z Q, Lee C S, Bello I, Lee S T, Chen R M, Luh...
  • 1-1 Management The complete story By Norris Dorsey

    1-1 Management The complete story By Norris Dorsey

    The Internet communication technologies are driving massive change initial enthusiasm for e-business has dwindled 25% of publicly-held Web companies became profitable in 2002 most profitable Web companies sell information-based products that don't require shipping old economy types now using the...
  • POINTLESS Round 1 Round 2 Finish Words with

    POINTLESS Round 1 Round 2 Finish Words with

    The car is started by Gavin. Pasta used to be made by Mum for Mia. Jenny is going to kiss him. Gavin starts the car. Mum used to make pasta for Mia. 26. 27. 24. 21. 56. 2. We asked...
  • 1. dia - unipa.it

    1. dia - unipa.it

    The available tools have largely remained in a time warp, consisting of analyses of national censuses, small-scale surveys, or lone researchers with a notebook observing interactions within small groups.
  • Edinburg Consolidated Independent School District Federal ...

    Edinburg Consolidated Independent School District Federal ...

    A PofA & AofG, doesn't convey legal guardianship. YES. A student is placed in a Residential Treatment Center (RTC), or a neglected/delinquent center. NO - A student who is a ward of the state and placed by the state entity...
  • Conditions of Microfracturing in the Southwestern Adirondacks by

    Conditions of Microfracturing in the Southwestern Adirondacks by

    The cut side was then polished. After this, they were taken off the glass side by dissolving the Crystalbond in acetone and cut into sections that had prominent quartz bands in them. These sections were put into boxes and labeled....
  • Now More than Ever John VanDenBerg - NH Dept of Education

    Now More than Ever John VanDenBerg - NH Dept of Education

    Mastricola Elementary School Behavior Matrix Playground Expectations Amherst Street Elementary School Playground Follow adult directions Be a good sport Use kind words Play fair Follow your Pledge Return everything you borrowed Walk quietly into the building Use playground equipment properly...