├── .gitignore
├── AbstractFactory
├── LinkPage.html
├── Main.py
└── factory
│ ├── __init__.py
│ ├── factory.py
│ ├── listfactory
│ ├── __init__.py
│ └── list_factory.py
│ └── tablefactory
│ ├── __init__.py
│ └── table_factory.py
├── Adapter
├── Adapter_1_Inheritance
│ ├── Main.py
│ └── adapter
│ │ ├── __init__.py
│ │ ├── banner.py
│ │ ├── print.py
│ │ └── print_banner.py
└── Adapter_2_delegation
│ ├── Main.py
│ └── adapter
│ ├── __init__.py
│ ├── banner.py
│ ├── print.py
│ └── print_banner.py
├── Bridge
├── Main.py
├── bridge
│ ├── __init__.py
│ ├── function
│ │ ├── __init__.py
│ │ ├── display_count_func.py
│ │ ├── display_func.py
│ │ └── display_random_func.py
│ └── implement
│ │ ├── __init__.py
│ │ ├── display_impl.py
│ │ ├── display_string_impl.py
│ │ └── display_textfile_impl.py
└── test.txt
├── Builder
├── Greeting.html
├── Main.py
└── builder
│ ├── __init__.py
│ ├── builder.py
│ ├── director.py
│ ├── htmlbuilder
│ ├── __init__.py
│ └── html_builder.py
│ └── textbuilder
│ ├── __init__.py
│ └── text_builder.py
├── Chain_of_Responsibility
├── Main.py
├── support.py
└── trouble.py
├── Command
├── Main.py
└── command
│ └── command.py
├── Composite
├── Main.py
└── entry.py
├── Decorator
├── step1
│ ├── Main.py
│ └── decorator
│ │ ├── __init__.py
│ │ ├── border.py
│ │ └── display.py
├── step2
│ ├── Main.py
│ └── decorator
│ │ ├── __init__.py
│ │ ├── border.py
│ │ └── display.py
└── step3
│ ├── Main.py
│ └── decorator
│ ├── __init__.py
│ ├── border.py
│ └── display.py
├── Facade
├── Main.py
├── maildata.ini
├── pagemaker
│ ├── __init__.py
│ ├── database.py
│ ├── html_writer.py
│ └── page_maker.py
├── welcome1.html
└── welcome2.html
├── FactoryMethod
├── Main.py
└── framework
│ ├── __init__.py
│ ├── factory.py
│ └── idcardfactory
│ ├── __init__.py
│ └── id_card_factory.py
├── Flyweight
├── Main.py
├── big0.txt
├── big1.txt
├── big2.txt
├── big3.txt
├── big4.txt
├── big5.txt
├── big6.txt
├── big7.txt
├── big8.txt
├── big9.txt
└── flyweight
│ ├── __init__.py
│ └── big_char_factory.py
├── Iterator
├── step1
│ ├── Main.py
│ └── iterator
│ │ ├── __init__.py
│ │ ├── aggregate.py
│ │ ├── book.py
│ │ └── iterator.py
└── step2
│ ├── Main.py
│ └── iterator
│ ├── __init__.py
│ └── book.py
├── Mediator
├── Main.py
└── mediator
│ ├── __init__.py
│ ├── colleague.py
│ └── mediator.py
├── Memento
├── Main.py
└── memento
│ ├── __init__.py
│ ├── gamer.py
│ └── memento.py
├── Observer
├── Main.py
└── observer
│ ├── __init__.py
│ ├── generator.py
│ └── observer.py
├── Prototype
├── Main.py
└── framework
│ ├── __init__.py
│ ├── decoprototype
│ ├── __init__.py
│ ├── message_box_prototype.py
│ └── underline_pen_prototype.py
│ ├── manager.py
│ └── prototype.py
├── Proxy
├── Main.py
└── proxy
│ ├── __init__.py
│ └── printer_proxy.py
├── README.md
├── Singleton
├── step1
│ └── Main.py
└── step2
│ └── Main.py
├── State
├── Main.py
└── state
│ ├── __init__.py
│ ├── context.py
│ └── state.py
├── Strategy
├── Main.py
└── strategy
│ ├── __init__.py
│ ├── hand.py
│ ├── player.py
│ └── strategy.py
├── TemplateMethod
├── Main.py
└── templatemethod
│ ├── __init__.py
│ └── display.py
└── Visitor
├── Main.py
└── visitor
├── __init__.py
├── element.py
└── visitor.py
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | __pycache__/
3 | *.py[co]
4 | log/
5 |
6 | # Unit test / coverage reports
7 | htmlcov/
8 | .tox/
9 | .coverage
10 | .coverage.*
11 | .cache
12 | nosetests.xml
13 | coverage.xml
14 | *.cover
15 | .hypothesis/
16 | .pytest_cache/
17 |
18 | # pyenv
19 | .python-version
20 |
21 | #vscode
22 | .vscode
23 |
--------------------------------------------------------------------------------
/AbstractFactory/LinkPage.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
LinkPage
4 |
5 | LinkPage
Hiroshi Yuki
13 |
14 |
--------------------------------------------------------------------------------
/AbstractFactory/Main.py:
--------------------------------------------------------------------------------
1 | import sys
2 | import inspect
3 | import factory
4 |
5 |
6 | def startMain(factoryObject):
7 | asahi = factoryObject.createLink("Asahi", "http://www.asahi.com")
8 | yomiuri = factoryObject.createLink("Yomiuri", "http://www.yomiuri.co.jp")
9 | us_yahoo = factoryObject.createLink("Yahoo", "http://www.yahoo.com")
10 | jp_yahoo = factoryObject.createLink("Yahoo!Japan", "http://www.yahoo.co.jp")
11 | google = factoryObject.createLink("Google", "http://www.google.com")
12 | excite = factoryObject.createLink("Excite", "http://www.excite.co.jp")
13 |
14 | traynews = factoryObject.createTray("Newspaper")
15 | traynews.add(asahi)
16 | traynews.add(yomiuri)
17 |
18 | trayyahoo = factoryObject.createTray("Yahoo!")
19 | trayyahoo.add(us_yahoo)
20 | trayyahoo.add(jp_yahoo)
21 |
22 | traysearch = factoryObject.createTray("Search Engine")
23 | traysearch.add(trayyahoo)
24 | traysearch.add(excite)
25 | traysearch.add(google)
26 |
27 | page = factoryObject.createPage("LinkPage", "Hiroshi Yuki")
28 | page.add(traynews)
29 | page.add(traysearch)
30 | page.output()
31 |
32 |
33 | if __name__ == '__main__':
34 | for _, plugin in inspect.getmembers(factory, inspect.isclass):
35 | if plugin.__name__ == sys.argv[1]:
36 | startMain(plugin())
37 |
--------------------------------------------------------------------------------
/AbstractFactory/factory/__init__.py:
--------------------------------------------------------------------------------
1 | from factory.listfactory.list_factory import ListFactory
2 | from factory.tablefactory.table_factory import TableFactory
3 |
4 | __all__ = [
5 | "ListFactory",
6 | "TableFactory"
7 | ]
--------------------------------------------------------------------------------
/AbstractFactory/factory/factory.py:
--------------------------------------------------------------------------------
1 | import sys
2 | from abc import ABCMeta, abstractmethod
3 |
4 |
5 | class Factory(metaclass=ABCMeta):
6 | @abstractmethod
7 | def createLink(self, caption, url):
8 | pass
9 |
10 | @abstractmethod
11 | def createTray(self, caption):
12 | pass
13 |
14 | @abstractmethod
15 | def createPage(self, title, author):
16 | pass
17 |
18 |
19 | class Item(metaclass=ABCMeta):
20 | def __init__(self, caption):
21 | self.caption = caption
22 |
23 | @abstractmethod
24 | def makeHtml(self):
25 | pass
26 |
27 |
28 | class Link(Item, metaclass=ABCMeta):
29 | def __init__(self, caption, url):
30 | super().__init__(caption)
31 | self.url = url
32 |
33 |
34 | class Tray(Item, metaclass=ABCMeta):
35 | def __init__(self, caption):
36 | super().__init__(caption)
37 | self.tray = []
38 |
39 | def add(self, item):
40 | self.tray.append(item)
41 |
42 |
43 | class Page(metaclass=ABCMeta):
44 | def __init__(self, title, author):
45 | self.title = title
46 | self.author = author
47 | self.content = []
48 |
49 | def add(self, item):
50 | self.content.append(item)
51 |
52 | def output(self):
53 | try:
54 | filename = self.title + '.html'
55 | writer = open(filename, 'w')
56 | writer.write(self.makeHtml())
57 | writer.close()
58 | print("[" + filename + "]" + " was created.")
59 | except Exception as e:
60 | print(e)
61 | sys.exit(1)
62 |
63 | @abstractmethod
64 | def makeHtml(self):
65 | pass
66 |
--------------------------------------------------------------------------------
/AbstractFactory/factory/listfactory/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ttsubo/study_of_design_pattern/e11dde0c03aa49b22b024adbcc393f7d35898479/AbstractFactory/factory/listfactory/__init__.py
--------------------------------------------------------------------------------
/AbstractFactory/factory/listfactory/list_factory.py:
--------------------------------------------------------------------------------
1 | from factory.factory import Factory, Link, Tray, Page
2 |
3 |
4 | class ListFactory(Factory):
5 | def createLink(self, caption, url):
6 | return ListLink(caption, url)
7 |
8 | def createTray(self, caption):
9 | return ListTray(caption)
10 |
11 | def createPage(self, title, author):
12 | return ListPage(title, author)
13 |
14 |
15 | class ListLink(Link):
16 | def __init__(self, caption, url):
17 | super().__init__(caption, url)
18 |
19 | def makeHtml(self):
20 | return ' {}\n'.format(self.url, self.caption)
21 |
22 |
23 | class ListTray(Tray):
24 | def __init__(self, caption):
25 | super().__init__(caption)
26 |
27 | def makeHtml(self):
28 | buf = []
29 | buf.append('\n')
30 | buf.append(self.caption + '\n')
31 | buf.append('\n')
32 |
33 | for item in self.tray:
34 | buf.append(item.makeHtml())
35 |
36 | buf.append('
\n')
37 | buf.append('\n')
38 | return ''.join(buf)
39 |
40 |
41 | class ListPage(Page):
42 | def __init__(self, title, author):
43 | super().__init__(title, author)
44 |
45 | def makeHtml(self):
46 | buf = []
47 | buf.append('''
48 |
49 | {}
50 | '''.format(self.title))
51 | buf.append('\n')
52 | buf.append('{}
'.format(self.title))
53 | buf.append('')
54 |
55 | for item in self.content:
56 | buf.append(item.makeHtml())
57 |
58 | buf.append('
')
59 | buf.append('
{}'.format(self.author))
60 | buf.append('\n\n')
61 | return ''.join(buf)
62 |
--------------------------------------------------------------------------------
/AbstractFactory/factory/tablefactory/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ttsubo/study_of_design_pattern/e11dde0c03aa49b22b024adbcc393f7d35898479/AbstractFactory/factory/tablefactory/__init__.py
--------------------------------------------------------------------------------
/AbstractFactory/factory/tablefactory/table_factory.py:
--------------------------------------------------------------------------------
1 | from factory.factory import Factory, Link, Tray, Page
2 |
3 |
4 | class TableFactory(Factory):
5 | def createLink(self, caption, url):
6 | return TableLink(caption, url)
7 |
8 | def createTray(self, caption):
9 | return TableTray(caption)
10 |
11 | def createPage(self, title, author):
12 | return TablePage(title, author)
13 |
14 |
15 | class TableLink(Link):
16 | def __init__(self, caption, url):
17 | super().__init__(caption, url)
18 |
19 | def makeHtml(self):
20 | return '{} | '.format(self.url, self.caption)
21 |
22 |
23 | class TableTray(Tray):
24 | def __init__(self, caption):
25 | super().__init__(caption)
26 |
27 | def makeHtml(self):
28 | buf = []
29 | buf.append('')
30 | buf.append('')
31 | buf.append('{} | '.format(len(self.tray), self.caption))
32 | buf.append(' \n')
33 | buf.append('\n')
34 |
35 | for item in self.tray:
36 | buf.append(item.makeHtml())
37 |
38 | buf.append(' ')
39 | buf.append(' | ')
40 | return ''.join(buf)
41 |
42 |
43 | class TablePage(Page):
44 | def __init__(self, title, author):
45 | super().__init__(title, author)
46 |
47 | def makeHtml(self):
48 | buf = []
49 | buf.append('''
50 |
51 | {}
52 | '''.format(self.title))
53 | buf.append('\n')
54 | buf.append('{}
'.format(self.title))
55 | buf.append('\n')
56 |
57 | for item in self.content:
58 | buf.append('{}
'.format(item.makeHtml()))
59 |
60 | buf.append('
')
61 | buf.append('
{}'.format(self.author))
62 | buf.append('\n\n')
63 | return ''.join(buf)
64 |
--------------------------------------------------------------------------------
/Adapter/Adapter_1_Inheritance/Main.py:
--------------------------------------------------------------------------------
1 | from adapter.print_banner import PrintBanner
2 |
3 |
4 | def startMain():
5 | p = PrintBanner("Hello")
6 | p.printWeak()
7 | p.printStrng()
8 |
9 |
10 | if __name__ == '__main__':
11 | startMain()
12 |
--------------------------------------------------------------------------------
/Adapter/Adapter_1_Inheritance/adapter/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ttsubo/study_of_design_pattern/e11dde0c03aa49b22b024adbcc393f7d35898479/Adapter/Adapter_1_Inheritance/adapter/__init__.py
--------------------------------------------------------------------------------
/Adapter/Adapter_1_Inheritance/adapter/banner.py:
--------------------------------------------------------------------------------
1 | class Banner(object):
2 | def __init__(self, string):
3 | self.__string = string
4 |
5 | def showWithParen(self):
6 | print("({0})".format(self.__string))
7 |
8 | def showWithAster(self):
9 | print("*{0}*".format(self.__string))
10 |
--------------------------------------------------------------------------------
/Adapter/Adapter_1_Inheritance/adapter/print.py:
--------------------------------------------------------------------------------
1 | from abc import ABCMeta, abstractmethod
2 |
3 |
4 | class Print(metaclass=ABCMeta):
5 | @abstractmethod
6 | def printWeak(self):
7 | pass
8 |
9 | @abstractmethod
10 | def printStrng(self):
11 | pass
12 |
--------------------------------------------------------------------------------
/Adapter/Adapter_1_Inheritance/adapter/print_banner.py:
--------------------------------------------------------------------------------
1 | from adapter.banner import Banner
2 | from adapter.print import Print
3 |
4 |
5 | class PrintBanner(Banner, Print):
6 | def __init__(self, string):
7 | super(PrintBanner, self).__init__(string)
8 |
9 | def printWeak(self):
10 | self.showWithParen()
11 |
12 | def printStrng(self):
13 | self.showWithAster()
14 |
--------------------------------------------------------------------------------
/Adapter/Adapter_2_delegation/Main.py:
--------------------------------------------------------------------------------
1 | from adapter.print_banner import PrintBanner
2 |
3 |
4 | def startMain():
5 | p = PrintBanner("Hello")
6 | p.printWeak()
7 | p.printStrng()
8 |
9 |
10 | if __name__ == '__main__':
11 | startMain()
12 |
--------------------------------------------------------------------------------
/Adapter/Adapter_2_delegation/adapter/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ttsubo/study_of_design_pattern/e11dde0c03aa49b22b024adbcc393f7d35898479/Adapter/Adapter_2_delegation/adapter/__init__.py
--------------------------------------------------------------------------------
/Adapter/Adapter_2_delegation/adapter/banner.py:
--------------------------------------------------------------------------------
1 | class Banner(object):
2 | def __init__(self, string):
3 | self.__string = string
4 |
5 | def showWithParen(self):
6 | print("({0})".format(self.__string))
7 |
8 | def showWithAster(self):
9 | print("*{0}*".format(self.__string))
10 |
--------------------------------------------------------------------------------
/Adapter/Adapter_2_delegation/adapter/print.py:
--------------------------------------------------------------------------------
1 | from abc import ABCMeta, abstractmethod
2 |
3 |
4 | class Print(metaclass=ABCMeta):
5 | @abstractmethod
6 | def printWeak(self):
7 | pass
8 |
9 | @abstractmethod
10 | def printStrng(self):
11 | pass
12 |
--------------------------------------------------------------------------------
/Adapter/Adapter_2_delegation/adapter/print_banner.py:
--------------------------------------------------------------------------------
1 | from adapter.banner import Banner
2 | from adapter.print import Print
3 |
4 |
5 | class PrintBanner(Print):
6 | def __init__(self, string):
7 | self.__banner = Banner(string)
8 |
9 | def printWeak(self):
10 | self.__banner.showWithParen()
11 |
12 | def printStrng(self):
13 | self.__banner.showWithAster()
14 |
--------------------------------------------------------------------------------
/Bridge/Main.py:
--------------------------------------------------------------------------------
1 | from bridge.function.display_func import DisplayFunc
2 | from bridge.function.display_count_func import DisplayCountFunc
3 | from bridge.function.display_random_func import DisplayRandomFunc
4 | from bridge.implement.display_string_impl import DisplayStringImpl
5 | from bridge.implement.display_textfile_impl import DisplayTextfileImpl
6 |
7 |
8 | def startMain():
9 | d1 = DisplayFunc(DisplayStringImpl("Hello Japan"))
10 | d2 = DisplayCountFunc(DisplayStringImpl("Hello Japan"))
11 | d3 = DisplayCountFunc(DisplayStringImpl("Hello Universe"))
12 | d4 = DisplayRandomFunc(DisplayStringImpl("Hello Universe"))
13 | d5 = DisplayFunc(DisplayTextfileImpl("test.txt"))
14 | d1.display()
15 | d2.display()
16 | d3.display()
17 | d3.multiDisplay(5)
18 | d4.randomDisplay(5)
19 | d5.display()
20 |
21 |
22 | if __name__ == '__main__':
23 | startMain()
24 |
--------------------------------------------------------------------------------
/Bridge/bridge/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ttsubo/study_of_design_pattern/e11dde0c03aa49b22b024adbcc393f7d35898479/Bridge/bridge/__init__.py
--------------------------------------------------------------------------------
/Bridge/bridge/function/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ttsubo/study_of_design_pattern/e11dde0c03aa49b22b024adbcc393f7d35898479/Bridge/bridge/function/__init__.py
--------------------------------------------------------------------------------
/Bridge/bridge/function/display_count_func.py:
--------------------------------------------------------------------------------
1 | from bridge.function.display_func import DisplayFunc
2 |
3 |
4 | class DisplayCountFunc(DisplayFunc):
5 | def __init__(self, impl):
6 | super(DisplayCountFunc, self).__init__(impl)
7 |
8 | def multiDisplay(self, times):
9 | self.open()
10 | for _ in range(times):
11 | self.print_body()
12 | self.close()
13 |
--------------------------------------------------------------------------------
/Bridge/bridge/function/display_func.py:
--------------------------------------------------------------------------------
1 | class DisplayFunc(object):
2 | def __init__(self, impl):
3 | self.impl = impl
4 |
5 | def open(self):
6 | self.impl.rawOpen()
7 |
8 | def print_body(self):
9 | self.impl.rawPrint()
10 |
11 | def close(self):
12 | self.impl.rawClose()
13 |
14 | def display(self):
15 | self.open()
16 | self.print_body()
17 | self.close()
18 |
--------------------------------------------------------------------------------
/Bridge/bridge/function/display_random_func.py:
--------------------------------------------------------------------------------
1 | import random
2 | from bridge.function.display_func import DisplayFunc
3 |
4 |
5 | class DisplayRandomFunc(DisplayFunc):
6 | def __init__(self, impl):
7 | super(DisplayRandomFunc, self).__init__(impl)
8 |
9 | def randomDisplay(self, times):
10 | self.open()
11 | t = random.randint(0, times)
12 | for _ in range(t):
13 | self.print_body()
14 | self.close()
15 |
--------------------------------------------------------------------------------
/Bridge/bridge/implement/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ttsubo/study_of_design_pattern/e11dde0c03aa49b22b024adbcc393f7d35898479/Bridge/bridge/implement/__init__.py
--------------------------------------------------------------------------------
/Bridge/bridge/implement/display_impl.py:
--------------------------------------------------------------------------------
1 | from abc import ABCMeta, abstractmethod
2 |
3 |
4 | class DisplayImpl(metaclass=ABCMeta):
5 | @abstractmethod
6 | def rawOpen(self):
7 | pass
8 |
9 | @abstractmethod
10 | def rawPrint(self):
11 | pass
12 |
13 | @abstractmethod
14 | def rawClose(self):
15 | pass
--------------------------------------------------------------------------------
/Bridge/bridge/implement/display_string_impl.py:
--------------------------------------------------------------------------------
1 | from bridge.implement.display_impl import DisplayImpl
2 |
3 |
4 | class DisplayStringImpl(DisplayImpl):
5 | def __init__(self, string):
6 | self.string = string
7 | self.width = len(string)
8 |
9 | def rawOpen(self):
10 | self.printLine()
11 |
12 | def rawPrint(self):
13 | print("|{0}|".format(self.string))
14 |
15 | def rawClose(self):
16 | self.printLine()
17 | print("")
18 |
19 | def printLine(self):
20 | line = '-' * self.width
21 | print("+{0}+".format(line))
22 |
--------------------------------------------------------------------------------
/Bridge/bridge/implement/display_textfile_impl.py:
--------------------------------------------------------------------------------
1 | from bridge.implement.display_impl import DisplayImpl
2 |
3 |
4 | class DisplayTextfileImpl(DisplayImpl):
5 | def __init__(self, filename):
6 | self.filename = filename
7 |
8 | def rawOpen(self):
9 | filename = self.filename
10 | self.f = open(filename, "r")
11 |
12 | def rawPrint(self):
13 | data = self.f.read()
14 | data = data.split('\n')
15 | for l in data:
16 | print(l)
17 |
18 | def rawClose(self):
19 | self.f.close()
20 |
--------------------------------------------------------------------------------
/Bridge/test.txt:
--------------------------------------------------------------------------------
1 | aaa
2 | bbb
3 | ccc
4 | ddd
5 | eee
6 | fff
7 | ggg
--------------------------------------------------------------------------------
/Builder/Greeting.html:
--------------------------------------------------------------------------------
1 | GreetingGreeting
From the morning to the afternoon
In the evening
- Good evening
- Good night
- Good bye