tk2023 object-oriented software engineering

Post on 07-Jan-2016

33 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

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

TRANSCRIPT

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.

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.

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.

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.

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.

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.

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

Sale

time

SalesLineItem

quantity

ProductDescription

descriptionpriceitemID

Described-by*

Contains

1..*

1

1

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

Sale

time...

getTotal ()

:Sale

t = getTotal

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

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.

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

1 *: st = getSubtotal

: Sale

t = getTotallineItems[ i ] :

SalesLineItem

Sale

time...

getTotal ()

SalesLineItem

quantity

getSubtotal()

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.

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

Sale

time...

getTotal()

SalesLineItem

quantity

getSubtotal()

ProductDescription

descriptionpriceitemID

getPrice()

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

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.

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.

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?

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.

top related