-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathproxy.py
More file actions
48 lines (36 loc) · 1.11 KB
/
proxy.py
File metadata and controls
48 lines (36 loc) · 1.11 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
"""
Proxy
- A class that functions as an interface to a particular resource.
The resource can be remote, expensive to construct, or may
require logging or some other added functionality
Motivation
- Same interface, entirely different behavior
- Types: logging, virtual, guarding
Proxy vs Decorator
- Proxy provides identical inferface, decorator provides enhanced
interface
- Decorator typically aggregates (or has reference to) what it is
decorating; proxy doesn't have to
- Proxy might not even be working with a materialized object
"""
class Car:
def __init__(self, driver):
self.driver = driver
def drive(self):
print(f'Car being driven by {self.driver.name}')
class CarProxy:
def __init__(self, driver):
self.driver = driver
self.car = Car(driver)
def drive(self):
if self.driver.age >= 16:
self.car.drive()
else:
print('Driver too young')
class Driver:
def __init__(self, name, age):
self.name = name
self.age = age
if __name__ == '__main__':
CAR = CarProxy(Driver('John', 19))
CAR.drive()