如何使用Python中的StrategyPattern访问字段?
我试图使用策略模式来包含不同大小模拟的不同行为如何使用Python中的StrategyPattern访问字段?,python,strategy-pattern,Python,Strategy Pattern,我试图使用策略模式来包含不同大小模拟的不同行为 from abc import ABCMeta, abstractmethod ########################################################################### ##### # Abstract Simulation class and concrete Simulation type classes. ###################################
from abc import ABCMeta, abstractmethod
########################################################################### #####
# Abstract Simulation class and concrete Simulation type classes.
################################################################################
class Simulation:
def __init__(self, run, plot):
self._run_behavior = run
self._plot_behavior = plot
def run(self):
return self._run_behavior.run()
def plot(self):
return self._plot_behavior.plot()
class SmallSimulation(Simulation):
def __init__(self):
Simulation.__init__(self, Run(), Plot())
print "I'm a small simulation"
self.data = 'Small Data'
class BigSimulation(Simulation):
def __init__(self):
Simulation.__init__(self, Run(), Plot())
print "I'm a big simulation"
self.data = 'Big Data'
class LargeSimulation(Simulation):
def __init__(self):
Simulation.__init__(self, RunLarge(), Plot())
print "I'm a large simulation"
self.data = 'Large Data'
################################################################################
# Run behavior interface and behavior implementation classes.
################################################################################
class RunBehavior:
__metaclass__ = ABCMeta
@abstractmethod
def run(self):
pass
class Run(RunBehavior):
def run(self):
print "I'm running standard"
print self.data
class RunLarge(RunBehavior):
def run(self):
print "I'm running multilevel"
################################################################################
# Plot behavior interface and behavior implementation classes.
################################################################################
class PlotBehavior:
__metaclass__ = ABCMeta
@abstractmethod
def plot(self):
pass
class Plot(PlotBehavior):
def plot(self):
print "I'm plotting results"
################################################################################
# Test Code.
################################################################################
if __name__ == '__main__':
smallSimulation = SmallSimulation()
bigSimulation = BigSimulation()
largeSimulation = LargeSimulation()
print('='*20)
print('Execution')
smallSimulation.run()
bigSimulation.run()
largeSimulation.run()
print('='*20)
print('Plotting')
smallSimulation.plot()
bigSimulation.plot()
largeSimulation.plot()
我是从这本书的第一个例子中发现的
然而,我不明白在哪里以及如何访问在模拟中初始化的数据
from abc import ABCMeta, abstractmethod
########################################################################### #####
# Abstract Simulation class and concrete Simulation type classes.
################################################################################
class Simulation:
def __init__(self, run, plot):
self._run_behavior = run
self._plot_behavior = plot
def run(self):
return self._run_behavior.run()
def plot(self):
return self._plot_behavior.plot()
class SmallSimulation(Simulation):
def __init__(self):
Simulation.__init__(self, Run(), Plot())
print "I'm a small simulation"
self.data = 'Small Data'
class BigSimulation(Simulation):
def __init__(self):
Simulation.__init__(self, Run(), Plot())
print "I'm a big simulation"
self.data = 'Big Data'
class LargeSimulation(Simulation):
def __init__(self):
Simulation.__init__(self, RunLarge(), Plot())
print "I'm a large simulation"
self.data = 'Large Data'
################################################################################
# Run behavior interface and behavior implementation classes.
################################################################################
class RunBehavior:
__metaclass__ = ABCMeta
@abstractmethod
def run(self):
pass
class Run(RunBehavior):
def run(self):
print "I'm running standard"
print self.data
class RunLarge(RunBehavior):
def run(self):
print "I'm running multilevel"
################################################################################
# Plot behavior interface and behavior implementation classes.
################################################################################
class PlotBehavior:
__metaclass__ = ABCMeta
@abstractmethod
def plot(self):
pass
class Plot(PlotBehavior):
def plot(self):
print "I'm plotting results"
################################################################################
# Test Code.
################################################################################
if __name__ == '__main__':
smallSimulation = SmallSimulation()
bigSimulation = BigSimulation()
largeSimulation = LargeSimulation()
print('='*20)
print('Execution')
smallSimulation.run()
bigSimulation.run()
largeSimulation.run()
print('='*20)
print('Plotting')
smallSimulation.plot()
bigSimulation.plot()
largeSimulation.plot()
输出是
I'm a small simulation
I'm a big simulation
I'm a large simulation
====================
Execution
I'm running standard
Traceback (most recent call last):
File "strategy.py", line 84, in <module>
smallSimulation.run()
File "strategy.py", line 16, in run
return self._run_behavior.run()
File "strategy.py", line 52, in run
print self.data
AttributeError: 'Run' object has no attribute 'data'
我是一个小仿真
我是个大人物
我是一个大的模拟
====================
处决
我在跑标准的
回溯(最近一次呼叫最后一次):
文件“strategy.py”,第84行,在
smallSimulation.run()
文件“strategy.py”,第16行,在运行中
返回self.\u run\u behavior.run()
文件“strategy.py”,第52行,在运行中
打印自助数据
AttributeError:“运行”对象没有属性“数据”
如何初始化和访问数据?您的run类没有数据属性,因此出现异常
class Run(RunBehavior):
# This class does NOT have a data attribute
def run(self):
print "I'm running standard"
print self.data
要在Run
中访问模拟的数据
,可以在Run的init()中传递它:
数据
应该从哪里来?如果数据独立于模拟类型,可能会将其传递给策略进行修改。如果它依赖于策略类型,那么就要有一个接口,用于向系统的其他部分表示它。如果它应该提供基本行为,请为此定义一个接口。@PadraicCunningham数据应该在具体模拟类型类(SmallSimulation、BigSimulation和LargeSimulation)初始化期间生成。@PeterWood数据取决于模拟类型,“run”必须相应地修改它。我知道StrategyPattern是基于行为而不是数据定义接口的。这个解决方案在数据封装方面正确吗?否决的人能否解释一下原因?我认为这并不违反数据封装。我的意思是,你必须以这样或那样的方式获取数据……谢谢,这就解决了目前的问题。正如@PeterWood和本文所指出的,我正在使用一个接口来传递数据以运行。