Skip to content

Commit fe13f51

Browse files
committed
Update Strategy pattern
1 parent 56e70e1 commit fe13f51

File tree

15 files changed

+555
-119
lines changed

15 files changed

+555
-119
lines changed

patterns/behavioural/strategy/README.md

Lines changed: 321 additions & 113 deletions
Large diffs are not rendered by default.

patterns/behavioural/strategy/problem/support/app.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import random
22
from dataclasses import dataclass, field
3-
from typing import List, Literal, Dict, Any
3+
from typing import List, Any
44
from enum import Enum, auto
55

66
from .ticket import SupportTicket
@@ -20,7 +20,9 @@ def add_ticket(self, ticket: SupportTicket) -> None:
2020
self.tickets.append(ticket)
2121

2222
def process_tickets(
23-
self, processing_strategy: ProcessingTypes = ProcessingTypes.FIFO, **kwargs: Any
23+
self,
24+
processing_strategy: ProcessingTypes = ProcessingTypes.FIFO,
25+
**kwargs: Any
2426
) -> None:
2527
if len(self.tickets) == 0:
2628
print("There are no tickets to process. Well done!")
@@ -48,4 +50,4 @@ def process_tickets(
4850

4951
return
5052

51-
raise NotImplementedError()
53+
raise NotImplementedError

patterns/behavioural/strategy/problem/support/ticket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SupportTicket:
99
id: str = field(init=False)
1010

1111
def __post_init__(self) -> None:
12-
self.id = str(uuid.uuid4())
12+
self.id = uuid.uuid4().hex
1313

1414
def __str__(self) -> str:
1515
return (

patterns/behavioural/strategy/solution_01/support/app.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ class CustomerSupport:
3939
def add_ticket(self, ticket: SupportTicket) -> None:
4040
self.tickets.append(ticket)
4141

42-
def process_tickets(self, processing_strategy: TicketOrderingStrategy) -> None:
42+
def process_tickets(
43+
self,
44+
processing_strategy: TicketOrderingStrategy
45+
) -> None:
4346
if len(self.tickets) == 0:
4447
print("There are no tickets to process. Well done!")
4548
return

patterns/behavioural/strategy/solution_04/support/app.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ def random_strategy(tickets: List[SupportTicket]) -> List[SupportTicket]:
2323

2424
return random_strategy
2525

26-
2726
@dataclass
2827
class CustomerSupport:
2928
tickets: List[SupportTicket] = field(default_factory=list)

patterns/behavioural/strategy/solution_05/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from functools import partial
2+
3+
from .support import CustomerSupport, random_strategy, SupportTicket
4+
5+
6+
def main() -> None:
7+
app = CustomerSupport()
8+
9+
tickets = [
10+
SupportTicket("John Smith", "My computer makes strange sounds!"),
11+
SupportTicket("Linus Sebastian", "I can't upload any videos, please help."),
12+
SupportTicket("Arjan Codes", "VSCode doesn't automatically solve my bugs."),
13+
]
14+
15+
for ticket in tickets:
16+
app.add_ticket(ticket)
17+
18+
random_strategy_with_seed = partial(random_strategy, seed=5)
19+
20+
app.process_tickets(random_strategy_with_seed)
21+
22+
23+
if __name__ == "__main__":
24+
main()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from .app import (
2+
CustomerSupport,
3+
random_strategy,
4+
fifo_strategy,
5+
filo_strategy,
6+
)
7+
from .ticket import SupportTicket
8+
9+
__all__ = [
10+
"CustomerSupport",
11+
"random_strategy",
12+
"fifo_strategy",
13+
"filo_strategy",
14+
"SupportTicket",
15+
]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import random
2+
from typing import List, Optional, Callable
3+
from dataclasses import dataclass, field
4+
5+
from .ticket import SupportTicket
6+
7+
TicketOrderingStrategy = Callable[[List[SupportTicket]], List[SupportTicket]]
8+
9+
10+
def fifo_strategy(tickets: List[SupportTicket]) -> List[SupportTicket]:
11+
return tickets.copy()
12+
13+
14+
def filo_strategy(tickets: List[SupportTicket]) -> List[SupportTicket]:
15+
return list(reversed(tickets))
16+
17+
18+
def random_strategy(tickets: List[SupportTicket], seed: Optional[int] = None) -> List[SupportTicket]:
19+
if seed is not None:
20+
random.seed(seed)
21+
return random.sample(tickets, len(tickets))
22+
23+
24+
@dataclass
25+
class CustomerSupport:
26+
tickets: List[SupportTicket] = field(default_factory=list)
27+
28+
def add_ticket(self, ticket: SupportTicket) -> None:
29+
self.tickets.append(ticket)
30+
31+
def process_tickets(self, processing_strategy: TicketOrderingStrategy) -> None:
32+
if len(self.tickets) == 0:
33+
print("There are no tickets to process. Well done!")
34+
return
35+
36+
ticket_list = processing_strategy(self.tickets)
37+
for ticket in ticket_list:
38+
ticket.process()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import uuid
2+
from dataclasses import dataclass, field
3+
4+
5+
@dataclass
6+
class SupportTicket:
7+
customer: str
8+
issue: str
9+
id: str = field(init=False)
10+
11+
def __post_init__(self) -> None:
12+
self.id = str(uuid.uuid4())
13+
14+
def __str__(self) -> str:
15+
return (
16+
"=================================="
17+
f"Processing ticket id: {self.id}"
18+
f"Customer: {self.customer}"
19+
f"Issue: {self.issue}"
20+
"=================================="
21+
)
22+
23+
def process(self) -> str:
24+
return str(self)

0 commit comments

Comments
 (0)