Delegation Pattern
[Previous]  [Next] 
|
User Manual [Previous]  [Next] Delegation PatternDelegation is one of the most fundamental patterns, that underpins many more sophisticated patterns found in the Gang of Four book. To simplify code, reduce coupling, and adhere to the 'Law of Demeter', it encourages you to create one-line methods that do nothing other than call methods in neighbouring classes. Other code in your class then calls the delegation methods, rather than also calling the methods in the neighbouring classes. That way, very few methods (just the delegation methods) actually need to traverse associations In Umple there is no special syntax for delegation, however the notation for derived attributes has the effect of creating delegation methods in the manner shown in the example below. Delegation methods can also be used to help build occurrences of other patterns such as
Note that delegation methods as shown here are 'read only'. They generate a get method with the name of the derived attribute, not a set method. Example// This system has many delegation methods so that information in one class can // be obtained in a neighbouring class easily. The delegation is created using // derived attributes, which generate get methods for use by other code. class Airline { code; name; 1 -- * RegularFlight; } // A regular flight flies every day and has a flight number and other fixed data. class RegularFlight { Integer number; Time depTime; * outgoing -- 1 Airport origin; * incoming-- 1 Airport destination; fullNumber = {getAirline().getCode()+getNumber()} } // A specific flight actually has passengers class SpecificFlight { * -- 1 RegularFlight; Date depDate; // Four delegation attributes to save multiple methods in this // class from having to traverse the association flightNumber = {getRegularFlight().getFullNumber()} Airport origin = {getRegularFlight().getOrigin()} Airport destination = {getRegularFlight().getDestination()} Time plannedDepTime = {getRegularFlight().getDepTime()} } // A passenger on a flight class Booking { lazy seat; * -- 1 SpecificFlight; * -- 1 Passenger; // Second-level delegation - data comes from RegularFlight flightNumber = {getSpecificFlight().getFlightNumber()} Airport destination = {getSpecificFlight().getDestination()} // Simple delegation passengerName = {getPassenger().getName()} Time plannedDepTime = {getSpecificFlight().getPlannedDepTime()} //Simple method using the data gathered by delegation public String toString() { return getFlightNumber()+" "+getPlannedDepTime()+" "+ getDestination().getName()+" "+getPassengerName()+" "+getSeat(); } } class Passenger { lazy Integer freqFlyerNumber; name; } class Airport { code; name; } // Mixin with main program to test the above class Airline { public static void main(String [ ] args) { Airport ott = new Airport("YOW","Ottawa"); Airport tor = new Airport("YYZ","Toronto Pearson"); Airline a = new Airline("AO","Air Ottawa"); RegularFlight r = a.addRegularFlight(100,java.sql.Time.valueOf("13:12:00"),ott,tor); SpecificFlight f = r.addSpecificFlight(java.sql.Date.valueOf("2017-03-15")); Passenger p = new Passenger("John"); Booking b = new Booking(f,p); System.out.println(b); } } Load the above code into UmpleOnline |