February R. McFadyen1 Polymorphism Indirection Pure Fabrication Protected Variations (Law of Demeter) More GRASP Patterns
February R. McFadyen2 Protected Variations Problem: How do we design systems so that changes in its elements do not have an unfavourable impact on other elements? Solution: Identify points of predicted variation/instability and assign responsibilities to create a stable interface around them Example: Law of Demeter (LoD) Special case of this pattern. If objects traverse long object structure paths and send messages to distant, indirect (stranger) objects, the system is fragile with respect to changes in the object structures - a common point of instability in systems. LoD helps us avoid creating such designs
February R. McFadyen3 Law of Demeter Also called Don’t Talk to Strangers Each class should only use a limited set of other classes: only units “closely” related to the current unit. “Each class should only talk (send messages) to its friends.” “Don’t talk to strangers.”
February R. McFadyen4 Law of Demeter FRIENDS
February R. McFadyen5 Don’t Talk to Strangers PaymentRegisterSale getTenderedAmount() paymentAmount() endSale() enterItem() makePayment()... becomeComplete() makeLineItem() makePayment() getTotal() getPayment... The class diagram shows that Register knows about Sale, and Sale knows about Payments that have been made towards it add a method to get a payment Suppose Register needs to find out the amount of the payment
February R. McFadyen6 Don’t Talk to Strangers Assume: Register has a paymentAmount method which returns the current amount tendered for the payment Sale has a method, getPayment, which returns the Payment instance associated with the Sale Consider: In order to return the payment amount, we could have a paymentAmount method in Register such as: public void paymentAmount() { Payment payment = sale.getPayment() Money amount = payment. getTenderedAmount () } A little different from the text’s example
February R. McFadyen7 Don’t Talk to Strangers :Payment :Register:Sale The previous has messages: Register will have a dependency on Payment This increases the coupling in our system getPayment() getTenderedAmount ()
February R. McFadyen8 Don’t Talk to Strangers :Payment :Register:Sale If getPayment() in Sale would invoke getTenderedAmount() in Payment, and return the payment amount, then we can de- couple Register from Payment make the solution more robust, less sensitive to changes, less coupling Register will get the payment amount it is after, but it won’t know how it was obtained - see Parnas’ concept of information hiding on Objects are only sending messages to their friends getTenderedAmount () getPayment()
February R. McFadyen9 Law of Demeter presentation: Karl J. Lieberherr; Northeastern University other resources Article on Information hiding
February R. McFadyen10 Example: Applying LoD as system changes BusRoute BusStopList BusStop BusList BusPersonList Person passengers buses busStops waiting 0..*
February R. McFadyen11 BusRoute BusStopList BusStop BusList BusPersonList Person passengers buses busStops waiting 0..* Find all persons waiting at any bus stop on a bus route Collaborating classes:
February R. McFadyen12 class BusRoute { BusStopList busstops; void printWaitingPassengers () { busstops->printWaitingPassengers (); } } class BusStopList { BusStop stops[]; void printWaitingPassengers () { for (int i = 0; i < stops.length; i++) stops[i].printWaitingPassengers (); } Applying Law of Demeter - Partial Java Solution
February R. McFadyen13 class BusStop { PersonList waiting; void printWaitingPassengers () { waiting.print (); } } class PersonList { Person people[]; void print () { for (int i = 0; i < people.length; i++) people[i].print (); } } class Person { String name; void print () { System.stdout.println (name); } } Applying Law of Demeter - Partial Java Solution
February R. McFadyen14 BusRoute BusStopList BusStop BusList BusPersonList Person passengers buses busStops waiting 0..* Suppose the class model is modified to incorporate Villages. VillageList Village villages 0..* What software changes are needed and still adhere to LoD?