This article reproduced from http://www.cnblogs.com/wuyuegb2312/archive/2013/04/09/3008320.html, and deleted some of the content
"Dahua design pattern" Python version of Code implementation
First, simple factory model
Model characteristics: The factory according to the conditions to produce different functions of the class.
Program instance: Arithmetic calculator, according to user's input produce corresponding operation class, use this operation class to handle the concrete operation.
Code Features: Switch...case ... The branch uses a dictionary instead.
Use the exception mechanism to handle a divisor of 0.
Class Operation:def GetResult (self): Pass class Operationadd (Operation): Def getresult (self): RE Turn Self.op1+self.op2 class Operationsub (Operation): Def getresult (self): Return SELF.OP1-SELF.OP2 class Operationmul (Operation): Def getresult (self): Return SELF.OP1*SELF.OP2 class Operationdiv (Operation): de
F GetResult (self): Try:result = self.op1/self.op2 return result except:
Print "error:divided by zero."
return 0 Class Operationundef (Operation): Def getresult (self): print "Undefine Operation."
return 0 Class operationfactory:operation = {} operation["+"] = Operationadd ();
operation["-"] = Operationsub ();
operation["*"] = Operationmul ();
operation["/"] = Operationdiv ();
def createoperation (self,ch): if ch in self.operation:op = self.operation[ch] Else: OP = OperationunDef () return op if __name__ = = "__main__": op = Raw_input ("operator:") OPA = input ("A:") OPB = Inpu T ("B:") factory = Operationfactory () cal = Factory.createoperation (OP) cal.op1 = Opa Cal.op2 = OPB PR int Cal. GetResult ()
second, the strategy model
Pattern Features: Define algorithm families and encapsulate them separately, and they can be replaced without affecting the client.
Program Example: Shopping center cashier software, need to according to different sales strategy for charging
Code features: Unlike the same example 1, the dictionary is used here to avoid pitfalls where the keyword does not cause bugs in the dictionary.
Class Cashsuper:def Acceptcash (Self,money): Return 0 Class Cashnormal (cashsuper): Def acceptcash (Self,mo Ney): Return Money class Cashrebate (cashsuper): Discount = 0 def __init__ (self,ds): Self.discount
= ds def acceptcash (Self,money): Return money * Self.discount class Cashreturn (cashsuper): total = 0;
ret = 0; def __init__ (self,t,r): self.total = t Self.ret = R def acceptcash (Self,money): if (money>= Self.total): Return Money-self.ret Else:return Money class Cashcontext:def __init __ (self,csuper): Self.cs = Csuper def getresult (Self,money): Return Self.cs.AcceptCash (Money) if __na me__ = = "__main__": Money = Input ("Money:") strategy = {} Strategy[1] = Cashcontext (Cashnormal ()) strategy [2] = Cashcontext (cashrebate (0.8)) strategy[3] = Cashcontext (Cashreturn (300,100)) CType = input ("Type:[1]for Norma L,[2]for80% discount [3]for 300-100. ") If ctype in strategy:cc = Strategy[ctype] else:print "undefine type.
Use normal mode. " CC = strategy[1] print "You'll pay:%d"% (CC. GetResult (Money))
third, decoration mode
Pattern Features: Dynamically adding additional responsibilities to the object
Program Example: show a person a piece of dress process.
Code Features: None
Class Person:
def __init__ (self,tname):
self.name = Tname
def show (self):
print "dressed%s"% ( Self.name)
class finery:
componet = None
def __init__ (self): pass
def decorate (self,ct) :
self.componet = CT
def show (self):
if (self.componet!=none):
self.componet.Show ()
class Tshirts (Finery):
def __init__ (self):
pass
def Show (self):
print "Big T-shirt"
Self.componet.Show ()
class Bigtrouser (finery):
def __init__ (self):
pass
def Show (self):
Print "Big trouser"
self.componet.Show ()
if __name__ = = "__main__":
p = person ("Somebody")
BT = Bigtrouser ()
ts = tshirts ()
BT. Decorate (p)
TS. Decorate (BT)
ts. Show ()
Four, the agent mode
Pattern Feature: Provides a proxy for other objects to control access to this object.
Program Example: The same pattern feature description.
Code Features: None
Class Interface:
def request (self): return
0
class Realsubject (Interface):
def request (self):
Print "Real request."
Class Proxy (Interface):
def Request (self):
self.real = Realsubject ()
self.real.Request ()
if __name __ = = "__main__":
p = Proxy ()
p.request ()
v. Factory method Model