Skip to content

Typo fix and Strategy update #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ def create_pizza(pizza_type):

if __name__ == '__main__':
for pizza_type in ('HamMushroom', 'Deluxe', 'Hawaiian'):
print('Price of {0} is {1}'.format(pizza_type, PizzaFactory.create_pizza(pizza_type).get_price())
print('Price of {0} is {1}'.format(pizza_type, PizzaFactory.create_pizza(pizza_type).get_price()))

43 changes: 23 additions & 20 deletions strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,53 @@ def __init__(self, func=None):
self.execute = func

def execute(self):
print "Original execution"
print self


def executeReplacement1(self):
def execute_replacement1():
print "Strategy 1"


def executeReplacement2(self):
def execute_replacement2():
print "Strategy 2"

if __name__ == "__main__":

strat0 = StrategyExample()
strat1 = StrategyExample(executeReplacement1)
strat2 = StrategyExample(executeReplacement2)
strat1 = StrategyExample(execute_replacement1)
strat2 = StrategyExample(execute_replacement2)

strat0.execute()
strat1.execute()
strat2.execute()

# -------------------- With classes --------------------

# Interface
class StrategicAlternative(object):
def the_method(self, some_arg, obj):
raise NotImplementedError("Must subclass me")

class AUsefulThing(object):
def __init__(self, aStrategicAlternative):
self.howToDoX = aStrategicAlternative

def doX(self, someArg):
self. howToDoX.theAPImethod(someArg, self)
class Strategy(object):
def __init__(self, the_class):
self.strategy = the_class


class StrategicAlternative(object):
pass
def do_something(self, some_arg):
self.strategy.the_method(some_arg, self)


class AlternativeOne(StrategicAlternative):
def theAPIMethod(self, someArg, theUsefulThing):
pass # an implementation
def the_method(self, some_arg, obj):
print self, some_arg


class AlternativeTwo(StrategicAlternative):
def theAPImethod(self, someArg, theUsefulThing):
pass # another implementation
def the_method(self, some_arg, obj):
print self, some_arg


t = Strategy(AlternativeOne())
t.do_something('Strategy 1')

t = AUsefulThing(AlternativeOne())
t.doX('arg')
t = Strategy(AlternativeTwo())
t.do_something('Strategy 2')