Policy mode:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/58/31/wKiom1SrlUHDBojBAACahYpVfhY750.jpg "title=" Policy mode:. jpg "alt=" wkiom1srluhdbojbaacahypvfhy750.jpg "/>
The code is as follows:
#!/usr/bin/env python
#-*-Coding:utf-8-*-
Class Strategy:
"Abstract Algorithm Class"
def algorithminterface (self):
"Abstract Method"
Pass
Class Concretestrategya (strategy):
"Specific algorithm Class A"
def algorithminterface (self):
"Concrete Implementation Method"
Print (' Algorithm A ')
Class Concretestrategyb (strategy):
"Specific algorithm Class A"
def algorithminterface (self):
"Concrete Implementation Method"
Print (' Algorithm B ')
Class CONCRETESTRATEGYC (strategy):
"Specific algorithm Class A"
def algorithminterface (self):
"Concrete Implementation Method"
Print (' Algorithm C ')
Class Context:
"Context class"
def __init__ (self, Strategy):
Self.strategy = Strategy
def contextinterface (self):
"Context Interface"
Self.strategy.algorithmInterface ()
if __name__ = = ' __main__ ':
"Same calling method different policy"
Context = Context (Concretestrategya ())
Context.contextinterface ()
Context = Context (Concretestrategyb ())
Context.contextinterface ()
Context = Context (CONCRETESTRATEGYC ())
Context.contextinterface ()
Study reproduced in: www.pythonfan.org
Python implementation policy mode