Skip to content

Instantly share code, notes, and snippets.

View atsuya046's full-sized avatar

Nobuya Oshiro atsuya046

View GitHub Profile
@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))
@atsuya046
atsuya046 / composite.py
Created February 28, 2014 22:33
GoF design pattern with Python - Composite
# -*- coding: utf-8 -*-
"""
A class which defines a composit object which can store
hierarchical dictionaries with names.
This class is same as a hierarchical dictionary, but it
provides method to add/accesss/modify children by name,
like a Composit.
@atsuya046
atsuya046 / interpreter.py
Created March 2, 2014 01:42
GoF design pattern with Python - Interpreter
# -*- coding: utf-8 -*-
import os
import fnmatch
class Expression(object):
def __and__(self, other):
return And(self, other)
def __or__(self, other):
@atsuya046
atsuya046 / abstract_factory.py
Created March 13, 2014 14:06
GoF design pattern with Python - AbstractFactory
# -*- coding: utf-8 -*-
# http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
"""Implementation of the abstract factory pattern"""
import random
class PetShop:
"""A pet shop"""
@atsuya046
atsuya046 / visitor.py
Created March 13, 2014 14:07
GoF design pattern with Python - Visitor
"""http://peter-hoffmann.com/2010/extrinsic-visitor-pattern-python-inheritance.html"""
class Node(object):
pass
class A(Node):
pass
class B(Node):
pass