Collaborating classes

The Law of Demeter is an object-oriented design principle that says you should never call methods on objects you receive from other calls. Or, put another way: Only talk to your immediate friends.


Figure 8.2: The Law of Demeter

We'll now modify our Flight class to accept an aircraft object when it is constructed, and we’ll follow the Law of Demeter by adding a method to report the aircraft model. This method will delegate to Aircraft on behalf of the client rather than allowing the client to "reach through" the Flight and interrogate the Aircraft object directly:

class Flight:
"""A flight with a particular passenger aircraft."""

def __init__(self, number, aircraft):
if not number[:2].isalpha():
raise ValueError("No airline code in '{}'".format(number))

if not number[:2].isupper():
raise ValueError("Invalid airline code'{}'".format(number))

if not (number[2:].isdigit() and int(number[2:]) <= 9999):
raise ValueError("Invalid route number'{}'".format(number))

self._number = number
self._aircraft = aircraft

def number(self):
return self._number

def airline(self):
return self._number[:2]

def aircraft_model(self):
return self._aircraft.model()

We've also added a docstring to the class. These work just like function and module docstrings, and must be the first non-comment line within the body of the class.

We can now construct a flight with a specific aircraft:

>>> from airtravel import *
>>> f = Flight("BA758", Aircraft("G-EUPT", "Airbus A319", num_rows=22,
... num_seats_per_row=6))
>>> f.aircraft_model()
'Airbus A319'

Notice that we construct the Aircraft object and directly pass it to the Flight constructor without needing an intermediate named reference for it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset