![](/assets/images/project_default_logo.png)
![](/assets/images/default-avatar-128.png)
Add SRP code.
@e96735723f9a176dba46491a18cd91a45fe18329
+++ .gitignore
... | ... | @@ -0,0 +1,5 @@ |
1 | +## Mac ## | |
2 | +.DS_Store | |
3 | + | |
4 | +## python ## | |
5 | +*.pyc |
--- python_code/__init__.pyc
Binary file is not shown |
--- python_code/bad/__init__.pyc
Binary file is not shown |
+++ python_code/bad/single_responsibility.py
... | ... | @@ -0,0 +1,62 @@ |
1 | +#!/usr/bin/env python | |
2 | +# -*- coding: utf-8 -*- | |
3 | + | |
4 | +from abc import ABCMeta, abstractmethod | |
5 | + | |
6 | +class IEmail(object): | |
7 | + __metaclass__ = ABCMeta | |
8 | + | |
9 | + @abstractmethod | |
10 | + def setSender(self, sender): | |
11 | + pass | |
12 | + | |
13 | + @abstractmethod | |
14 | + def setReceiver(self, receiver): | |
15 | + pass | |
16 | + | |
17 | + @abstractmethod | |
18 | + def setContent(self, content): | |
19 | + pass | |
20 | + | |
21 | +class Email(IEmail): | |
22 | + | |
23 | + def __init__(self, protocol, content_type): | |
24 | + self.protocol = protocol | |
25 | + self.content_type = content_type | |
26 | + self.__sender = None | |
27 | + self.__receiver = None | |
28 | + self.__content = None | |
29 | + | |
30 | + def setSender(self, sender): | |
31 | + if self.protocol == 'IM': | |
32 | + self.__sender = ''.join(["I'm ", sender]) | |
33 | + else: | |
34 | + self.__sender = sender | |
35 | + | |
36 | + def setReceiver(self, receiver): | |
37 | + if self.protocol == 'IM': | |
38 | + self.__receiver = ''.join(["I'm ", receiver]) | |
39 | + else: | |
40 | + self.__receiver = receiver | |
41 | + | |
42 | + def setContent(self, content): | |
43 | + if self.content_type == 'MyML': | |
44 | + self.__content = '\n'.join(['<myML>', content, '</myML>']) | |
45 | + else: | |
46 | + self.__content = content | |
47 | + | |
48 | + def __repr__(self): | |
49 | + | |
50 | + template = "Sender: {sender}\nReceiver: {receiver}\nContent:\n{content}" | |
51 | + | |
52 | + return template.format(sender = self.__sender, receiver = self.__receiver, content = self.__content) | |
53 | + | |
54 | +def main(): | |
55 | + email = Email('IM', 'MyML') | |
56 | + email.setSender('qmal') | |
57 | + email.setReceiver('james') | |
58 | + email.setContent('Hello, there!') | |
59 | + print email | |
60 | + | |
61 | +if __name__ == '__main__': | |
62 | + main() |
+++ python_code/good/single_responsibility.py
... | ... | @@ -0,0 +1,79 @@ |
1 | +#!/usr/bin/env python | |
2 | +# -*- coding: utf-8 -*- | |
3 | +# In this implementation, it provide the same functionality as `python_code.bad.single_responsibility`. | |
4 | +# There is only one reason to modify the code of `Email` if you want to support different protocol. For | |
5 | +# different content, you only need to define a new subtype of `IContent`. It's not the same in the | |
6 | +# implementation of `python_code.bad.single_responsibility` since there are 2 senario which you have to | |
7 | +# modify the code for `Email`: different content types and different protocols. | |
8 | + | |
9 | +from abc import ABCMeta, abstractmethod | |
10 | + | |
11 | +class IEmail(object): | |
12 | + __metaclass__ = ABCMeta | |
13 | + | |
14 | + @abstractmethod | |
15 | + def setSender(self, sender): | |
16 | + pass | |
17 | + | |
18 | + @abstractmethod | |
19 | + def setReceiver(self, receiver): | |
20 | + pass | |
21 | + | |
22 | + @abstractmethod | |
23 | + def setContent(self, content): | |
24 | + pass | |
25 | + | |
26 | +class IContent(object): | |
27 | + __metaclass__ = ABCMeta | |
28 | + | |
29 | + @abstractmethod | |
30 | + def getString(self): | |
31 | + pass | |
32 | + | |
33 | +class MyContent(IContent): | |
34 | + | |
35 | + def __init__(self, content): | |
36 | + self.content = content | |
37 | + | |
38 | + def getString(self): | |
39 | + return "<MyML>{}</MyML>".format(self.content) | |
40 | + | |
41 | +class Email(IEmail): | |
42 | + | |
43 | + def __init__(self, protocol): | |
44 | + self.protocol = protocol | |
45 | + self.__sender = None | |
46 | + self.__receiver = None | |
47 | + self.__content = None | |
48 | + | |
49 | + def setSender(self, sender): | |
50 | + if self.protocol == 'IM': | |
51 | + self.__sender = ''.join(["I'm ", sender]) | |
52 | + else: | |
53 | + self.__sender = sender | |
54 | + | |
55 | + def setReceiver(self, receiver): | |
56 | + if self.protocol == 'IM': | |
57 | + self.__receiver = ''.join(["I'm ", receiver]) | |
58 | + else: | |
59 | + self.__receiver = receiver | |
60 | + | |
61 | + def setContent(self, content): | |
62 | + self.__content = content.getString() | |
63 | + | |
64 | + def __repr__(self): | |
65 | + | |
66 | + template = "Sender: {sender}\nReceiver: {receiver}\nContent:\n{content}" | |
67 | + | |
68 | + return template.format(sender = self.__sender, receiver = self.__receiver, content = self.__content) | |
69 | + | |
70 | +def main(): | |
71 | + email = Email('IM') | |
72 | + email.setSender('qmal') | |
73 | + email.setReceiver('james') | |
74 | + content = MyContent('Hello, there!') | |
75 | + email.setContent(content) | |
76 | + print email | |
77 | + | |
78 | +if __name__ == '__main__': | |
79 | + main() |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?