220 likes | 392 Views
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.
E N D
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.
We introduce an operation called getTotal() through which a Sale object will carry out the responsibility. t = getTotal : Sale Sale time ... 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 1 Contains * 1 .. Product Sales Description * 1 LineItem Described - by description quantity price itemID • Using the Information Expert pattern (and referring to the Domain Model), SalesLineItem could be given that responsibility.
t = getTotal 1 *: st = getSubtotal lineItems [ i ] : : Sale SalesLineItem • We introduce an operation called getSubtotal() through which a SalesLineItem object will carry out the responsibility.
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.
1 *: st = getSubtotal t = getTotal lineItems [ i ] : : Sale SalesLineItem 1 . 1 : p = getPrice : Product Description • We introduce an operation called getPrice() through which a ProductDescription object will carry out the responsibility.
Sale time ... getTotal () Product Description SalesLineItem description quantity price itemID getSubtotal () () 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:
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.