strategy pattern - How to access fields with StrategyPattern in Python? -


i'm trying use strategy pattern include different behaviours different sizes of simulation.

i came across this implementation first example of book head first design patterns.

however, don't understand , how should access data initialised in simulation.

from abc import abcmeta, abstractmethod ###########################################################################    ##### # abstract simulation class , 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 small simulation"         self.data = 'small data'  class bigsimulation(simulation):     def __init__(self):         simulation.__init__(self, run(), plot())         print "i'm big simulation"         self.data = 'big data'  class largesimulation(simulation):     def __init__(self):         simulation.__init__(self, runlarge(), plot())         print "i'm large simulation"         self.data = 'large data'  ################################################################################ # run behavior interface , 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 , 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() 

the output

i'm small simulation i'm big simulation i'm 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' 

how should initialise , access data?

your run class doesn't have data attribute, hence exception.

class run(runbehavior): # class not have data attribute     def run(self):         print "i'm running standard"         print self.data 

to access simulation's data within run, can pass in run's init():

class run(runbehavior):     def __init__(self, data):        self.data = data     def run(self):        print "i'm running standard"        print self.data 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -