12. Motivation “ 16 of 23 [GoF] patterns have a qualitatively simpler implementation in Lisp or Dylan than in C++, for at least some uses of each pattern” Peter Norvig (http://norvig.com/design-patterns/) “ Patterns are not needed in Python because design patterns are a sign of a deficiency of a language” … ... for the purpose that the design pattern addresses. How is observer implemented in Python? (equivalent to Boost.Signals, Qt Slot/Signals, .NET events) Coming from C++ or Java, if you already know the GoF patterns then it would be informative to see how they are implemented in Python. This talk documents part of my journey from C++ to Ptyhon.
13. Software vs Engineering design Physical construction – E.g. building a house Software construction The code is the design! “ Software may be cheap to build, but it is incredibly expensive to design” J W Reeves, What Is Software Design?, www.developerdotstar.com Design stage output
14. Software design How does one design software, compared to physical engineering design? Data + algorithms? - only a part of the solution Structure and Interpretation of Computer Programs. H Abelson, G Sussman, J Sussman. http://mitpress.mit.edu/sicp/full-text/book/book.html Software Design Concepts (wikipedia) Abstraction – categorize and group concepts Refinement – convert high level to program statements Modularity – isolate independent features Software architecture – overall structure of the software Control Hierarchy – program structure Structural partitioning – horizontal vs vertical ? Data structure – logical relationship among elements Software procedure – an operation within a module Information hiding - information contained within a module is inaccessible to others Not especially helpful - too abstract!
22. Patterns are not independent from the programming language. Example: subroutines in assembler. Gamma, Helm, Johnson, Vlissades (1995): Design patterns: elements of reusable object oriented software. Addison Wesley.
23. Pattern classes Purpose Creational Structural Behavioural Scope Class Factory Method Adapter (class) Interpreter Template Method Object Abstract factory Builder Prototype Singleton Adapter (object) Bridge Composite Decorator Facade Flyweight Proxy Chain of responsibility Command Iterator Mediator Memento Observer State Strategy Visitor Gamma, Helm, Johnson, Vlissades (1995): Design patterns: elements of reusable object oriented software. Addison Wesley. Invisible or simplified in Python due to: first class types first class functions other Fixed at compile time Can change at runtime Object creation Compostion of classes or objects Class and object interactions
37. If you need to do something special. E.g. # Put in const.py...: class _const : class ConstError (TypeError): pass def __setattr__ ( self ,name,value): if self .__dict__.has_key(name): raise self .ConstError, "Can't rebind const (%s)" %name self .__dict__[name]=value import sys sys.modules[__name__]=_const() # that's all -- now any client-code can import const # and bind an attribute ONCE: const.magic = 23 # but NOT re-bind it: const.magic = 88 # raises const.ConstError # you may also want to add the obvious __delattr__ Alex Martelli http://code.activestate.com/recipes/65207-constants-in-python/
42. Specify, queue and execute requests at different times. http://www.cs.mcgill.ca/~hv/classes/CS400/01.hchen/doc/command/command.html
43. Command – GoF style Rahul Verma, Chetan Giridhar. Design Patterns in Python. www.testingperspective.com class Command : """The Command Abstract class""" def __init__ ( self ): pass #Make changes def execute ( self ): #OVERRIDE raise NotImplementedError class FlipUpCommand (Command): """The Command class for turning on the light""" def __init__ ( self , light): self .__light = light def execute ( self ): self .__light.turnOn()
44. Command in Python def greet (who): print "Hello %s" % who greet_command = lambda : greet( "World" ) # pass the callable around, and invoke it later greet_command() class MoveFileCommand (object): def __init__ ( self , src, dest): self .src = src self .dest = dest self () def __call__ ( self ): os.rename( self .src, self .dest) def undo ( self ): os.rename( self .dest, self .src) undo_stack = [] undo_stack.append(MoveFileCommand( 'foo.txt' , 'bar.txt' )) undo_stack.append(MoveFileCommand( 'bar.txt' , 'baz.txt' )) # foo.txt is now renamed to baz.txt undo_stack.pop().undo() # Now it's bar.txt undo_stack.pop().undo() # and back to foo.txt http://stackoverflow.com/questions/1494442/general-command-pattern-and-command-dispatch-pattern-in-python (Ants Aasma) Simple case: Just use a callable More complex case: Use a command object but no need for a base class
59. Pass parameters to the observer e.g. http://code.activestate.com/recipes/131499-observer-pattern/
60.
61.
62. Not the same as Python decorators Objects enclose other objects that share similar interfaces. The decorating object appears to mask or modify or annotate the enclosed object. http://en.wikipedia.org/wiki/File:Decorator_UML_class_diagram.svg
68. Known uses: TCP, GUIs http://en.wikipedia.org/wiki/File:State_Design_Pattern_UML_Class_Diagram.svg
69. State GoF style class State(object): """Base state. This is to share functionality""" def scan( self ): """Scan the dial to the next station""" self .pos += 1 if self .pos == len( self .stations): self .pos = 0 print "Scanning… Station is" , self .stations[ self .pos], self .name class AmState(State): def __init__( self , radio): self .radio = radio self .stations = [ "1250" , "1380" , "1510" ] self .pos = 0 self .name = "AM" def toggle_amfm( self ): print "Switching to FM" self .radio.state = self .radio.fmstate class FmState(State): def __init__( self , radio): self .radio = radio self .stations = [ "81.3" , "89.1" , "103.9" ] self .pos = 0 self .name = "FM" def toggle_amfm( self ): print "Switching to AM" self .radio.state = self .radio.amstate class Radio(object): """A radio. It has a scan button, and an AM/FM toggle switch.""" def __init__( self ): """We have an AM state and an FM state""" self .amstate = AmState( self ) self .fmstate = FmState( self ) self .state = self .amstate def toggle_amfm( self ): self .state.toggle_amfm() def scan( self ): self .state.scan() # Test radio = Radio() actions = [radio.scan] * 2 + [radio.toggle_amfm] + [radio.scan] * 2 actions = actions * 2 for action in actions: action() Jeff ? http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ Scanning... Station is 1380 AM Scanning... Station is 1510 AM Switching to FM Scanning... Station is 89.1 FM Scanning... Station is 103.9 FM Scanning... Station is 81.3 FM Scanning... Station is 89.1 FM Switching to AM Scanning... Station is 1250 AM Scanning... Station is 1380 AM Note lack of state methods “Abstract” state Context Concrete states
70.
71. State in Python (method) class Sequencer(): def __init__( self ): self ._action_impl = self .action1 self .count = 1 def action( self ): self ._action_impl() def next( self ): self .count += 1 if self .count > 3 : self .count = 1 self ._action_impl = getattr( self , "action" +str( self .count)) def action1( self ): print "1" def action2( self ): print "2" def action3( self ): print "3" s = Sequencer() actions = [s.action] + [s.next] actions = actions * 3 for f in actions: f() >>> 1 2 3 >>> Switch methods output Use Bridge so that the binding of Sequencer.action doesn't change
72. State in Python (class) >>> First 1 Second 2 Third 3 First 4 First 1 Second 2 Second 3 class Base (): def __init__ ( self ): self .state = 0 def action ( self ): self .state += 1 print self .__class__.__name__, self .state def change_state ( self , next_class): self .__class__ = next_class class Third (Base): def transition ( self ): self .change_state( First ) class Second (Base): def transition ( self ): self .change_state( Third ) class First (Base): def transition ( self ): self .change_state( Second ) state = First() state.action() state.transition() state.action() state.transition() state.action() state.transition() state.action() state = First() actions = [state.action] + [state.transition] actions = actions * 3 for action in actions: action() output This doesn't work because state is always First
78. Factory Method GOF style class Person : def __init__ ( self ): self .name = None self .gender = None def getName ( self ): return self .name def getGender ( self ): return self .gender class Male (Person): def __init__ ( self , name): print "Hello Mr." + name class Female (Person): def __init__ ( self , name): print "Hello Miss." + name class Factory : def getPerson ( self , name, gender): if gender == 'M' : return Male(name) if gender == 'F' : return Female(name) From: dpip.testingperspective.com factory = Factory() person = factory.getPerson( " Chetan " , "M" ) person = factory.getPerson( " Money " , "F" ) >>> Hello Mr.Chetan Hello Miss.Money
79. Factory method in Python class Male (object): def __init__ ( self , name): print "Hello Mr." + name class Female (object): def __init__ ( self , name): print "Hello Ms." + name factory = dict(F=Female, M=Male) if __name__ == '__main__' : person = factory[ "F" ]( "Money" ) person = factory[ "M" ]( " Powers " ) >>> Hello Ms.Money Hello Mr.Powers Adapted from: http://www.rmi.net/~lutz/talk.html # variable length arg lists def factory (aClass, *args, **kwargs): return aClass(*args, **kwargs) class Spam : def __init__ ( self ): print self .__class__.__name__ def doit ( self , message): print message class Person : def __init__ ( self , name, job): self .name = name self .job = job print self .__class__.__name__, name, job object1 = factory(Spam) object2 = factory(Person, " Guido " , "guru" ) >>> Spam Person Guido guru
80.
81. Abstract Factory GoF style class PetShop : def __init__ ( self , animal_factory= None ): """pet_factory is our abstract factory. We can set it at will.""" self .pet_factory = animal_factory def show_pet ( self ): """Creates and shows a pet using the abstract factory""" pet = self .pet_factory.get_pet() print "This is a lovely" , pet print "It says" , pet.speak() print "It eats" , self .pet_factory.get_food() class Dog : def speak ( self ): return "woof" def __str__ ( self ): return "Dog" class Cat : def speak ( self ): return " meow " def __str__ ( self ): return "Cat" http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ class DogFactory : def get_pet ( self ): return Dog() def get_food ( self ): return "dog food" class CatFactory : def get_pet ( self ): return Cat() def get_food ( self ): return "cat food" # Create the proper family def get_factory (): return random.choice([DogFactory, CatFactory])() # Show pets with various factories shop = PetShop() for i in range( 3 ): shop.pet_factory = get_factory() shop.show_pet() print "=" * 10 >>> This is a lovely Dog It says woof It eats dog food ========== This is a lovely Cat It says meow It eats cat food ========== This is a lovely Dog It says woof It eats dog food ==========
Personal: needed an observer implementation in Python like Boost.Signal, initially couldn't find one. My engineering background lead me to search for generic design principles in software.
Polymorphism
Creational patterns: patterns that can be used to create objects. Structural patterns: patterns that can be used to combine objects and classes in order to build structured objects. Behavioral patterns: patterns that can be used to build a computation and to control data flows. Norvig: 16 of 23 patterns are either invisible or simpler, due to: First-class types (6): Abstract-Factory, Flyweight, Factory-Method, State, Proxy, Chain-Of-Responsibility First-class functions (4): Command, Strategy, Template-Method, Visitor Macros (2): Interpreter, Iterator Method Combination (2): Mediator, Observer Multimethods (1): Builder Modules (1): Facade