-
Notifications
You must be signed in to change notification settings - Fork 56
/
flyweight.py
55 lines (38 loc) · 1.18 KB
/
flyweight.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
import abc
class User:
def __init__(self, n):
self.name = n
class Website:
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def use(self, u):
"""users use website"""
class ConcreteWebsite(Website):
def __init__(self, wn):
self.website_name = wn
def use(self, u):
print u.name + " use " + self.website_name
class WebsiteFactory:
flyweights = {}
def get_website_category(self, n):
if self.flyweights.has_key(n) is False:
self.flyweights[n] = ConcreteWebsite(n)
return self.flyweights[n]
def get_website_count(self):
print len(self.flyweights.keys())
if __name__ == "__main__":
website_factory = WebsiteFactory()
alice = User("Alice")
bob = User("Bob")
website = website_factory.get_website_category("bbs")
website.use(alice)
website.use(bob)
website_factory.get_website_count()
website = website_factory.get_website_category("blog")
website.use(alice)
website.use(bob)
website_factory.get_website_count()
website = website_factory.get_website_category("bbs")
website.use(alice)
website.use(bob)
website_factory.get_website_count()