-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathstrategy-after.py
More file actions
89 lines (62 loc) · 2.61 KB
/
strategy-after.py
File metadata and controls
89 lines (62 loc) · 2.61 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
78
79
80
81
82
83
84
85
86
87
88
89
import string
import random
from typing import List
from abc import ABC, abstractmethod
def generate_id(length=8):
# helper function for generating an id
return ''.join(random.choices(string.ascii_uppercase, k=length))
class SupportTicket:
def __init__(self, customer, issue):
self.id = generate_id()
self.customer = customer
self.issue = issue
class TicketOrderingStrategy(ABC):
@abstractmethod
def create_ordering(self, list: List[SupportTicket]) -> List[SupportTicket]:
pass
class FIFOOrderingStrategy(TicketOrderingStrategy):
def create_ordering(self, list: List[SupportTicket]) -> List[SupportTicket]:
return list.copy()
class FILOOrderingStrategy(TicketOrderingStrategy):
def create_ordering(self, list: List[SupportTicket]) -> List[SupportTicket]:
list_copy = list.copy()
list_copy.reverse()
return list_copy
class RandomOrderingStrategy(TicketOrderingStrategy):
def create_ordering(self, list: List[SupportTicket]) -> List[SupportTicket]:
list_copy = list.copy()
random.shuffle(list_copy)
return list_copy
class BlackHoleStrategy(TicketOrderingStrategy):
def create_ordering(self, list: List[SupportTicket]) -> List[SupportTicket]:
return []
class CustomerSupport:
def __init__(self, processing_strategy: TicketOrderingStrategy):
self.tickets = []
self.processing_strategy = processing_strategy
def create_ticket(self, customer, issue):
self.tickets.append(SupportTicket(customer, issue))
def process_tickets(self):
# create the ordered list
ticket_list = self.processing_strategy.create_ordering(self.tickets)
# if it's empty, don't do anything
if len(ticket_list) == 0:
print("There are no tickets to process. Well done!")
return
# go through the tickets in the list
for ticket in ticket_list:
self.process_ticket(ticket)
def process_ticket(self, ticket: SupportTicket):
print("==================================")
print(f"Processing ticket id: {ticket.id}")
print(f"Customer: {ticket.customer}")
print(f"Issue: {ticket.issue}")
print("==================================")
# create the application
app = CustomerSupport(RandomOrderingStrategy())
# register a few tickets
app.create_ticket("John Smith", "My computer makes strange sounds!")
app.create_ticket("Linus Sebastian", "I can't upload any videos, please help.")
app.create_ticket("Arjan Egges", "VSCode doesn't automatically solve my bugs.")
# process the tickets
app.process_tickets()