345449.vhj5l3oj7.asia
ChainOfResponsibility.py
2001-12-26T16:00:00Z
2001-12-26T16:00:00Z
<br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c06:ChainOfResponsibility.py
# Carry the information into the strategy:
class Messenger: pass
# The Result object carries the result data and
# whether the strategy was successful:
class Result:
def __init__(self):
self.succeeded = 0
def isSuccessful(self):
return self.succeeded
def setSuccessful(self, succeeded):
self.succeeded = succeeded
class Strategy:
def __call__(messenger): pass
def __str__(self):
return "Trying " + self.__class__.__name__ \
+ " algorithm"
# Manage the movement through the chain and
# find a successful result:
class ChainLink:
def __init__(self, chain, strategy):
self.strategy = strategy
self.chain = chain
self.chain.append(self)
def next(self):
# Where this link is in the chain:
location = self.chain.index(self)
if not self.end():
return self.chain[location + 1]
def end(self):
return (self.chain.index(self) + 1 >=
len(self.chain))
def __call__(self, messenger):
r = self.strategy(messenger)
if r.isSuccessful() or self.end(): return r
return self.next()(messenger)
# For this example, the Messenger
# and Result can be the same type:
class LineData(Result, Messenger):
def __init__(self, data):
self.data = data
def __str__(self): return `self.data`
class LeastSquares(Strategy):
def __call__(self, messenger):
print self
linedata = messenger
# [ Actual test/calculation here ]
result = LineData([1.1, 2.2]) # Dummy data
result.setSuccessful(0)
return result
class NewtonsMethod(Strategy):
def __call__(self, messenger):
print self
linedata = messenger
# [ Actual test/calculation here ]
result = LineData([3.3, 4.4]) # Dummy data
result.setSuccessful(0)
return result
class Bisection(Strategy):
def __call__(self, messenger):
print self
linedata = messenger
# [ Actual test/calculation here ]
result = LineData([5.5, 6.6]) # Dummy data
result.setSuccessful(1)
return result
class ConjugateGradient(Strategy):
def __call__(self, messenger):
print self
linedata = messenger
# [ Actual test/calculation here ]
result = LineData([7.7, 8.8]) # Dummy data
result.setSuccessful(1)
return result
solutions = []
solutions = [
ChainLink(solutions, LeastSquares()),
ChainLink(solutions, NewtonsMethod()),
ChainLink(solutions, Bisection()),
ChainLink(solutions, ConjugateGradient())
]
line = LineData([
1.0, 2.0, 1.0, 2.0, -1.0,
3.0, 4.0, 5.0, 4.0
])
print solutions[0](line)
#:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
2001-12-26T16:00:00Z
CommandPattern.py
2001-12-26T16:00:00Z
2001-12-26T16:00:00Z
<br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c06:CommandPattern.py
class Command:
def execute(self): pass
class Loony(Command):
def execute(self):
print "You're a loony."
class NewBrain(Command):
def execute(self):
print "You might even need a new brain."
class Afford(Command):
def execute(self):
print "I couldn't afford a whole new brain."
# An object that holds commands:
class Macro:
def __init__(self):
self.commands = []
def add(self, command):
self.commands.append(command)
def run(self):
for c in self.commands:
c.execute()
macro = Macro()
macro.add(Loony())
macro.add(NewBrain())
macro.add(Afford())
macro.run()
#:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
2001-12-26T16:00:00Z
StrategyPattern.py
2001-12-26T16:00:00Z
2001-12-26T16:00:00Z
<br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c06:StrategyPattern.py
# The strategy interface:
class FindMinima:
# Line is a sequence of points:
def algorithm(self, line) : pass
# The various strategies:
class LeastSquares(FindMinima):
def algorithm(self, line):
return [ 1.1, 2.2 ] # Dummy
class NewtonsMethod(FindMinima):
def algorithm(self, line):
return [ 3.3, 4.4 ] # Dummy
class Bisection(FindMinima):
def algorithm(self, line):
return [ 5.5, 6.6 ] # Dummy
class ConjugateGradient(FindMinima):
def algorithm(self, line):
return [ 3.3, 4.4 ] # Dummy
# The "Context" controls the strategy:
class MinimaSolver:
def __init__(self, strategy):
self.strategy = strategy
def minima(self, line):
return self.strategy.algorithm(line)
def changeAlgorithm(self, newAlgorithm):
self.strategy = newAlgorithm
solver = MinimaSolver(LeastSquares())
line = [
1.0, 2.0, 1.0, 2.0, -1.0, 3.0, 4.0, 5.0, 4.0
]
print solver.minima(line)
solver.changeAlgorithm(Bisection())
print solver.minima(line)
#:~</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
2001-12-26T16:00:00Z