277294.ijbsn.asia
Games.py
2001-12-26T16:00:00Z
2001-12-26T16:00:00Z
<br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c05:Games.py
# An example of the Abstract Factory pattern.
class Obstacle:
def action(self): pass
class Player:
def interactWith(self, obstacle): pass
class Kitty(Player):
def interactWith(self, obstacle):
print "Kitty has encountered a",
obstacle.action()
class KungFuGuy(Player):
def interactWith(self, obstacle):
print "KungFuGuy now battles a",
obstacle.action()
class Puzzle(Obstacle):
def action(self):
print "Puzzle"
class NastyWeapon(Obstacle):
def action(self):
print "NastyWeapon"
# The Abstract Factory:
class GameElementFactory:
def makePlayer(self): pass
def makeObstacle(self): pass
# Concrete factories:
class KittiesAndPuzzles(GameElementFactory):
def makePlayer(self): return Kitty()
def makeObstacle(self): return Puzzle()
class KillAndDismember(GameElementFactory):
def makePlayer(self): return KungFuGuy()
def makeObstacle(self): return NastyWeapon()
class GameEnvironment:
def __init__(self, factory):
self.factory = factory
self.p = factory.makePlayer()
self.ob = factory.makeObstacle()
def play(self):
self.p.interactWith(self.ob)
g1 = GameEnvironment(KittiesAndPuzzles())
g2 = GameEnvironment(KillAndDismember())
g1.play()
g2.play()
#:~</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
Games2.py
2001-12-26T16:00:00Z
2001-12-26T16:00:00Z
<br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c05:Games2.py
# Simplified Abstract Factory.
class Kitty:
def interactWith(self, obstacle):
print "Kitty has encountered a",
obstacle.action()
class KungFuGuy:
def interactWith(self, obstacle):
print "KungFuGuy now battles a",
obstacle.action()
class Puzzle:
def action(self): print "Puzzle"
class NastyWeapon:
def action(self): print "NastyWeapon"
# Concrete factories:
class KittiesAndPuzzles:
def makePlayer(self): return Kitty()
def makeObstacle(self): return Puzzle()
class KillAndDismember:
def makePlayer(self): return KungFuGuy()
def makeObstacle(self): return NastyWeapon()
class GameEnvironment:
def __init__(self, factory):
self.factory = factory
self.p = factory.makePlayer()
self.ob = factory.makeObstacle()
def play(self):
self.p.interactWith(self.ob)
g1 = GameEnvironment(KittiesAndPuzzles())
g2 = GameEnvironment(KillAndDismember())
g1.play()
g2.play()
#:~</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
ShapeFactory1.py
2001-12-26T16:00:00Z
2001-12-26T16:00:00Z
<br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c05:shapefact1:ShapeFactory1.py
# A simple static factory method.
from __future__ import generators
import random
class Shape(object):
# Create based on class name:
def factory(type):
#return eval(type + "()")
if type == "Circle": return Circle()
if type == "Square": return Square()
assert 1, "Bad shape creation: " + type
factory = staticmethod(factory)
class Circle(Shape):
def draw(self): print "Circle.draw"
def erase(self): print "Circle.erase"
class Square(Shape):
def draw(self): print "Square.draw"
def erase(self): print "Square.erase"
# Generate shape name strings:
def shapeNameGen(n):
types = Shape.__subclasses__()
for i in range(n):
yield random.choice(types).__name__
shapes = \
[ Shape.factory(i) for i in shapeNameGen(7)]
for shape in shapes:
shape.draw()
shape.erase()
#:~</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
ShapeFactory2.py
2001-12-26T16:00:00Z
2001-12-26T16:00:00Z
<br/><TEXTAREA name="code" class="py" rows="16" cols="100">#: c05:shapefact2:ShapeFactory2.py
# Polymorphic factory methods.
from __future__ import generators
import random
class ShapeFactory:
factories = {}
def addFactory(id, shapeFactory):
ShapeFactory.factories.put[id] = shapeFactory
addFactory = staticmethod(addFactory)
# A Template Method:
def createShape(id):
if not ShapeFactory.factories.has_key(id):
ShapeFactory.factories[id] = \
eval(id + '.Factory()')
return ShapeFactory.factories[id].create()
createShape = staticmethod(createShape)
class Shape(object): pass
class Circle(Shape):
def draw(self): print "Circle.draw"
def erase(self): print "Circle.erase"
class Factory:
def create(self): return Circle()
class Square(Shape):
def draw(self):
print "Square.draw"
def erase(self):
print "Square.erase"
class Factory:
def create(self): return Square()
def shapeNameGen(n):
types = Shape.__subclasses__()
for i in range(n):
yield random.choice(types).__name__
shapes = [ ShapeFactory.createShape(i)
for i in shapeNameGen(7)]
for shape in shapes:
shape.draw()
shape.erase()
#:~</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