20
TK2023 Object- Oriented Software Engineering CHAPTER 13b GRASP Patterns: Information Expert

TK2023 Object-Oriented Software Engineering

  • Upload
    curry

  • View
    33

  • Download
    0

Embed Size (px)

DESCRIPTION

TK2023 Object-Oriented Software Engineering. CHAPTER 13b GRASP Patterns: Information Expert. INTRODUCTION. During object design, decisions are made regarding the assignment of responsibilities to software classes. - PowerPoint PPT Presentation

Citation preview

Page 1: TK2023 Object-Oriented Software Engineering

TK2023 Object-Oriented Software Engineering

CHAPTER 13b

GRASP Patterns: Information Expert

Page 2: TK2023 Object-Oriented Software Engineering

INTRODUCTION

During object design, decisions are made regarding the assignment of responsibilities to software classes.

Correct decisions will lead to systems which tend to be easier to understand, maintain and extend. Their components tend to be reusable for future applications.

Page 3: TK2023 Object-Oriented Software Engineering

GRASP PATTERNS: INFORMATION 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.

Page 4: TK2023 Object-Oriented Software Engineering

EXAMPLE OF APPLICATION In the POS application, when the Cashier has

entered all items bought by the Customer, the System displays the grand total of the current sale. So,

Who should be responsible for knowing the grand total of a sale?

Using the Information Expert pattern, we should be looking for the class of objects that has the information needed to determine the total.

Page 5: TK2023 Object-Oriented Software Engineering

Do we look in the Domain Model or the Design Model? If there are relevant classes in the Design Model,

look there first. Otherwise, look in the Domain Model. Attempt to

use (or expand) its representations to inspire the creation of corresponding design classes.

Page 6: TK2023 Object-Oriented Software Engineering

For this example, let’s assume that our Design Model is minimal. So, we look in the Domain Model.

What information do we need to determine the grand total?

We need to know about all the SalesLineItem instances of a sale and the sum of their subtotals.

Page 7: TK2023 Object-Oriented Software Engineering

According to the Information Expert pattern, Sale is suitable for that responsibility.

Sale

time

SalesLineItem

quantity

ProductDescription

descriptionpriceitemID

Described-by*

Contains

1..*

1

1

Page 8: TK2023 Object-Oriented Software Engineering

We introduce an operation called getTotal() through which a Sale object will carry out the responsibility.

Sale

time...

getTotal ()

:Sale

t = getTotal

Page 9: TK2023 Object-Oriented Software Engineering

For a Sale object to calculate the grand total, it needs the subtotal for each line item.

Who should be responsible for knowing the subtotal of a line item?

What information is required to determine the line item subtotal?

i. quantity

ii. price

Page 10: TK2023 Object-Oriented Software Engineering

Sale

time

SalesLineItem

quantity

ProductDescription

descriptionpriceitemID

Described-by*

Contains

1..*

1

1

Using the Information Expert pattern (and referring to the Domain Model), SalesLineItem could be given that responsibility.

Page 11: TK2023 Object-Oriented Software Engineering

We introduce an operation called getSubtotal() through which a SalesLineItem object will carry out the responsibility.

1 *: st = getSubtotal

: Sale

t = getTotallineItems[ i ] :

SalesLineItem

Page 12: TK2023 Object-Oriented Software Engineering

Sale

time...

getTotal ()

SalesLineItem

quantity

getSubtotal()

Page 13: TK2023 Object-Oriented Software Engineering

For a SalesLineItem object to calculate its subtotal, it needs the price of the item.

Who should be responsible for knowing the price of an item?

The ProductDescription object has the information for carrying out the responsibility. So, by the Information Expert pattern, that object is the information expert for that responsibility.

Page 14: TK2023 Object-Oriented Software Engineering

We introduce an operation called getPrice() through which a ProductDescription object will carry out the responsibility.

:ProductDescription

1.1: p = getPrice

1 *: st = getSubtotal

: Sale

t = getTotallineItems[ i ] :SalesLineItem

Page 15: TK2023 Object-Oriented Software Engineering

Sale

time...

getTotal()

SalesLineItem

quantity

getSubtotal()

ProductDescription

descriptionpriceitemID

getPrice()

Page 16: TK2023 Object-Oriented Software Engineering

In conclusion, to fulfill the responsibility of knowing and answering the total of a sale, we assigned three responsibilities to three design classes of objects as follows:

Design Class Responsibility

Sale knows sale total

SalesLineItem knows line item subtotal

ProductDescription knows product price

Page 17: TK2023 Object-Oriented Software Engineering

DISCUSSION

Information Expert is frequently used in the assignment of responsibilities; it is a basic guiding principle used continuously in object design.

Expert usually leads to designs where a software object does those operations that are normally not done to the inanimate real-world thing it represents.

Page 18: TK2023 Object-Oriented Software Engineering

Note that the fulfillment of a responsibility often requires information that is spread across different classes of objects. This leads to objects interacting via messages to share the work.

Real-world analogy: We commonly give responsibility to individuals who have the information / resources necessary to fulfill a task.

Page 19: TK2023 Object-Oriented Software Engineering

CONTRAINDICATIONS

In some situations, a solution suggested by Information Expert is undesirable, usually because of problems in coupling and cohesion.

Example: Who should be responsible for saving a Sale in a database?

Page 20: TK2023 Object-Oriented Software Engineering

BENEFITS

Information encapsulation is maintained since objects use their own information to fulfill tasks. This usually supports low coupling, which leads to more robust and maintainable systems.

Behaviour is distributed across the classes that have the required information, thus encouraging more cohesive class definitions that are easier to understand and maintain.