-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.py
123 lines (93 loc) · 3.74 KB
/
state.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# State design pattern
# -------------------
# The State pattern is a behavioral design pattern that allows an object to
# alter its behavior when its internal state changes. It appears as if the
# object changed its class.
# The State pattern is used to create an object which represents a particular
# state and whose behavior changes based on that state. This can be a cleaner
# way for an object to change its behavior at runtime without resorting to
# large monolithic conditional statements and thus improve maintainability.
# Problem:
# --------
# Imagine a social media platform that allows users to post content and
# comment on other users' posts. The platform has two types of users: regular
# users and premium users. Regular users can only post and comment in lowercase
# letters, while premium users can post and comment in uppercase letters. The
# platform also allows users to subscribe to other users' posts and comments.
# When a user subscribes to another user, they will be notified whenever the
# other user posts or comments.
from abc import ABC, abstractmethod
class Subscriber(ABC):
@abstractmethod
def notify_post(self, content):
pass
@abstractmethod
def notify_comment(self, content):
pass
class RegularUserState:
def __init__(self):
self.subscribers = []
def post(self, content):
print(f"Regular user posted: {content}")
for subscriber in self.subscribers:
subscriber.notify_post(content)
def comment(self, content):
print(f"Regular user commented: {content}")
for subscriber in self.subscribers:
subscriber.notify_comment(content)
def subscribe(self, target):
self.subscribers.append(target)
def unsubscribe(self, target):
self.subscribers.remove(target)
class PremiumUserState:
def __init__(self):
self.subscribers = []
def post(self, content):
print(f"Premium user posted: {content.upper()}")
for subscriber in self.subscribers:
subscriber.notify_post(content.upper())
def comment(self, content):
print(f"Premium user commented: {content.upper()}")
for subscriber in self.subscribers:
subscriber.notify_comment(content.upper())
def subscribe(self, target):
self.subscribers.append(target)
def unsubscribe(self, target):
self.subscribers.remove(target)
class User:
def __init__(self, name, state):
self.name = name
self.state = state
def set_state(self, state):
self.state = state
def post(self, content):
self.state.post(content)
def comment(self, content):
self.state.comment(content)
def subscribe(self, target):
self.state.subscribe(target)
def unsubscribe(self, target):
self.state.unsubscribe(target)
# Usage
class ConsoleSubscriber(Subscriber):
def __init__(self, name):
self.name = name
def notify_post(self, content):
print(f"{self.name} notified of post: {content}")
def notify_comment(self, content):
print(f"{self.name} notified of comment: {content}")
alice = User("Alice", RegularUserState())
bob = ConsoleSubscriber("Bob")
alice.subscribe(bob)
alice.post("Hello World")
# Output: Regular user posted: Hello World, Bob notified of post: Hello World
alice.comment("Nice post!")
# Output: Regular user commented: Nice post!, Bob notified of comment: Nice post!
alice.set_state(PremiumUserState())
alice.post("Hello World")
# Output: PREMIUM USER POSTED: HELLO WORLD, Bob notified of post: HELLO WORLD
alice.comment("Nice post!")
# Output: PREMIUM USER COMMENTED: NICE POST!, Bob notified of comment: NICE POST!
alice.unsubscribe(bob)
alice.post("Another post")
# Output: Premium user posted: ANOTHER POST