This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""http://python-3-patterns-idioms-test.readthedocs.org/en/latest/Singleton.html""" | |
class OnlyOne: | |
class __OnlyOne: | |
def __init__(self, arg): | |
self.val = arg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding utf-8 -*- | |
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/""" | |
class JapaneseGetter: | |
"""A simple localizer a la gettext""" | |
def __init__(self): | |
self.trans = dict(dog="犬", cat="猫") | |
def get(self, msgid): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""http://code.activestate.com/recipes/131499-observer-pattern/""" | |
class Subject(object): | |
def __init__(self): | |
self._observers = [] | |
def attach(self, observer): | |
if not observer in self._observers: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
class Handler: | |
def successor(self, successor): | |
self.successor = successor | |
class ConcreteHandler1(Handler): | |
def handle(self, request): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import time | |
class SalesManager: | |
def work(self): | |
print("Sales Manager working...") | |
def talk(self): | |
print("Sales Manager ready to talk") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ | |
Implementation of the iterator pattern with a generator""" | |
def count_to(count): | |
"""Counts by word numbers, up to a maximum of five""" | |
numbers = ["one", "two", "three", "four", "five"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/""" | |
class State(object): | |
"""Base state. This is to share functionality""" | |
def scan(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import os | |
class MoveFileCommand(object): | |
def __init__(self, src, dest): | |
self.src = src | |
self.dest = dest | |
def execute(self): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""http://code.activestate.com/recipes/413838-memento-closure/""" | |
import copy | |
def Memento(obj, deep=False): | |
state = (copy.copy, copy.deepcopy)[bool(deep)](obj.__dict__) | |
def Restore(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Bridge_Pattern#Python""" | |
# ConcreteImplementor 1/2 | |
class DrawingAPI1(object): | |
def draw_circle(self, x, y, radius): | |
print('API1.circle at {}:{} radius {}'.format(x, y, radius)) | |
OlderNewer