Refactoring Aircraft

Let's return to our Aircraft class:

class Aircraft:

def __init__(self, registration, model, num_rows,
num_seats_per_row):

self._registration = registration
self._model = model
self._num_rows = num_rows
self._num_seats_per_row = num_seats_per_row

def registration(self):
return self._registration

def model(self):
return self._model

def seating_plan(self):
return (range(1, self._num_rows + 1),
"ABCDEFGHJK"[:self._num_seats_per_row])

The design of this class is somewhat flawed, in that objects instantiated using it depend on being supplied with a seating configuration that matches the aircraft model. For the purposes of this exercise we can assume that the seating arrangement is fixed per aircraft model.

Better, and simpler, perhaps to get rid of the Aircraft class entirely and make separate classes for each specific model of aircraft with a fixed seating configuration. Here's an Airbus A319:

class AirbusA319:

def __init__(self, registration):
self._registration = registration

def registration(self):
return self._registration

def model(self):
return "Airbus A319"

def seating_plan(self):
return range(1, 23), "ABCDEF"

And here's a Boeing 777:

class Boeing777:

def __init__(self, registration):
self._registration = registration

def registration(self):
return self._registration

def model(self):
return "Boeing 777"

def seating_plan(self):
# For simplicity's sake, we ignore complex
# seating arrangement for first-class
return range(1, 56), "ABCDEGHJK"

These two aircraft classes have no explicit relationship to each other, or to our original Aircraft class, beyond having identical interfaces (with the exception of the initializer, which now takes fewer arguments). As such we can use these new types in place of each other.

Let's change our make_flight() method to make_flights() so we can use them:

def make_flights():
f = Flight("BA758", AirbusA319("G-EUPT"))
f.allocate_seat('12A', 'Guido van Rossum')
f.allocate_seat('15F', 'Bjarne Stroustrup')
f.allocate_seat('15E', 'Anders Hejlsberg')
f.allocate_seat('1C', 'John McCarthy')
f.allocate_seat('1D', 'Richard Hickey')

g = Flight("AF72", Boeing777("F-GSPS"))
g.allocate_seat('55K', 'Larry Wall')
g.allocate_seat('33G', 'Yukihiro Matsumoto')
g.allocate_seat('4B', 'Brian Kernighan')
g.allocate_seat('4A', 'Dennis Ritchie')

return f, g

The different types of aircraft both work fine when used with Flight because they both quack like ducks. Or fly like planes. Or something:

>>> from airtravel import *
>>> f, g = make_flights()
>>> f.aircraft_model()
'Airbus A319'
>>> g.aircraft_model()
'Boeing 777'
>>> f.num_available_seats()
127
>>> g.num_available_seats()
491
>>> g.relocate_passenger('55K', '13G')
>>> g.make_boarding_cards(console_card_printer)
+--------------------------------------------------------------------+
| | | |
| Name:Brian Kernighan Flight:AF72 Seat:4B Aircraft:Boeing 777 | | |
| | | |
+--------------------------------------------------------------------+

+--------------------------------------------------------------------+
| |
| Name:Dennis Ritchie Flight:AF72 Seat:4A Aircraft:Boeing 777 |
| |
+--------------------------------------------------------------------+

+-----------------------------------------------------------------+
| |
| Name:Larry Wall Flight:AF72 Seat: 13G Aircraft:Boeing 777 |
| |
+-----------------------------------------------------------------+

+--------------------------------------------------------------------+
| | | |
| Name:Yukihiro Matsumoto Flight:AF72 Seat:33G Aircraft:Boeing 777| | |
| | | |
+--------------------------------------------------------------------+

Duck typing and polymorphism is very important in Python. In fact it's the basis for the collection protocols we discussed such as iteratoriterable and sequence.

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

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