-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvisitor.py
More file actions
77 lines (53 loc) · 2.27 KB
/
Copy pathvisitor.py
File metadata and controls
77 lines (53 loc) · 2.27 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
68
69
70
71
72
73
74
75
76
77
from __future__ import annotations
from abc import ABC, abstractmethod
class ReportElement(ABC):
def accept(self, visitor: ReportVisitor):
visitor.visit(self)
class FixedPriceContract(ReportElement):
def __init__(self, price_per_year: int):
self.price_per_year = price_per_year
class TimeAndMaterialsContract(ReportElement):
def __init__(self, cost_per_hour: int, hours: int):
self.cost_per_hour = cost_per_hour
self.hours = hours
class SupportContract(ReportElement):
def __init__(self, cost_per_month: int):
self.cost_per_month = cost_per_month
class ReportVisitor(ABC):
@abstractmethod
def visit_fpc(self, contract: FixedPriceContract):
pass
@abstractmethod
def visit_tmc(self, contract: TimeAndMaterialsContract):
pass
@abstractmethod
def visit_sc(self, contract: SupportContract):
pass
class MonthlyCostReportVisitor(ReportVisitor):
def visit_fpc(self, contract: FixedPriceContract):
return contract.price_per_year / 12
def visit_tmc(self, contract: TimeAndMaterialsContract):
return contract.cost_per_hour * contract.hours
def visit_sc(self, contract: SupportContract):
return contract.cost_per_month
class YearlyCostReportVisitor(ReportVisitor):
def visit_fpc(self, contract: FixedPriceContract):
return contract.price_per_year
def visit_tmc(self, contract: TimeAndMaterialsContract):
return contract.cost_per_hour * contract.hours
def visit_sc(self, contract: SupportContract):
return contract.cost_per_month * 12
if __name__ == '__main__':
project_alpha = FixedPriceContract(10000)
project_beta = TimeAndMaterialsContract(150, 10)
project_gamma = SupportContract(500)
monthly_visitor = MonthlyCostReportVisitor()
monthly_cost = monthly_visitor.visit_fpc(project_alpha)
monthly_cost += monthly_visitor.visit_tmc(project_beta)
monthly_cost += monthly_visitor.visit_sc(project_gamma)
print(f"Monthly cost is: {monthly_cost}")
yearly_visitor = YearlyCostReportVisitor()
yearly_cost = yearly_visitor.visit_fpc(project_alpha)
yearly_cost += yearly_visitor.visit_tmc(project_beta)
yearly_cost += yearly_visitor.visit_sc(project_gamma)
print(f"Yearly cost is: {yearly_cost}")