-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathFacade.py
More file actions
68 lines (52 loc) · 1.99 KB
/
Facade.py
File metadata and controls
68 lines (52 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# Written by: DGC
# An example of how the client interacts with a complex series of objects
# (car engine, battery and starter motor) through a facade (the car)
#==============================================================================
class Engine(object):
def __init__(self):
# how much the motor is spinning in revs per minute
self.spin = 0
def start(self, spin):
if (spin > 2000):
self.spin = spin // 15
#==============================================================================
class StarterMotor(object):
def __init__(self):
# how much the starter motor is spinning in revs per minute
self.spin = 0
def start(self, charge):
# if there is enough power then spin fast
if (charge > 50):
self.spin = 2500
#==============================================================================
class Battery(object):
def __init__(self):
# % charged, starts flat
self.charge = 0
#==============================================================================
class Car(object):
# the facade object that deals with the battery, engine and starter motor.
def __init__(self):
self.battery = Battery()
self.starter = StarterMotor()
self.engine = Engine()
def turn_key(self):
# use the battery to turn the starter motor
self.starter.start(self.battery.charge)
# use the starter motor to spin the engine
self.engine.start(self.starter.spin)
# if the engine is spinning the car is started
if (self.engine.spin > 0):
print("Engine Started.")
else:
print("Engine Not Started.")
def jump(self):
self.battery.charge = 100
print("Jumped")
#==============================================================================
if (__name__ == "__main__"):
corsa = Car()
corsa.turn_key()
corsa.jump()
corsa.turn_key()