Skip to content

Instantly share code, notes, and snippets.

View atsuya046's full-sized avatar

Nobuya Oshiro atsuya046

View GitHub Profile
@atsuya046
atsuya046 / singleton.py
Created January 30, 2014 22:34
GoF design pattern - Singleton.py
#!/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
# -*- 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):
@atsuya046
atsuya046 / observer.py
Created February 2, 2014 14:04
GoF design pattern - Observer
# -*- 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:
@atsuya046
atsuya046 / chain_of_responsibility.py
Created February 3, 2014 15:15
GoF design pattern - Chain of Responsibility
# -*- coding: utf-8 -*-
class Handler:
def successor(self, successor):
self.successor = successor
class ConcreteHandler1(Handler):
def handle(self, request):
@atsuya046
atsuya046 / proxy.py
Created February 4, 2014 04:33
GoF design pattern - Proxy
# -*- coding: utf-8 -*-
import time
class SalesManager:
def work(self):
print("Sales Manager working...")
def talk(self):
print("Sales Manager ready to talk")
@atsuya046
atsuya046 / iterator.py
Created February 5, 2014 13:47
GoF design pattern with Python - Iterator
# -*- 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"]
@atsuya046
atsuya046 / state.py
Created February 11, 2014 12:54
GoF design pattern - State
# -*- 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):
@atsuya046
atsuya046 / command.py
Created February 12, 2014 04:44
GoF design pattern - Command
# -*- coding: utf-8 -*-
import os
class MoveFileCommand(object):
def __init__(self, src, dest):
self.src = src
self.dest = dest
def execute(self):
@atsuya046
atsuya046 / memento.py
Created February 13, 2014 04:45
GoF design pattern - Memento
# -*- 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():
@atsuya046
atsuya046 / bridge.py
Created February 17, 2014 16:04
GoF design pattern - Bridge
# -*- 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))