1 """FactoryMethodPattern in DesignPatterns Example
   2 """
   3 
   4 import unittest
   5 
   6 class Room:
   7     def __init__(self,anId):
   8         self._id=anId
   9     def getConnectedRooms(self):
  10         return self._door.getTheOtherRoom(self._id)        
  11     def setDoor(self,aDoor):
  12         self._door=aDoor
  13     def lookAround(self):
  14         return "it's a room"
  15 
  16 class FireRoom(Room):
  17     def lookAround(self):
  18         return "It's very hot"
  19 class IceRoom(Room):
  20     def lookAround(self):
  21         return "It's very cold"
  22 
  23 class Door:
  24     def __init__(self,aRoom1,aRoom2):
  25         self._room1=aRoom1
  26         self._room2=aRoom2
  27         self._room1.setDoor(self)
  28         self._room2.setDoor(self)
  29     def getTheOtherRoom(self,aRoomId):
  30         if aRoomId==self._room1._id:
  31             return self._room2
  32         else:
  33             return self._room1
  34 
  35 class FireDoor(Door):
  36     pass
  37 class IceDoor(Door):
  38     pass
  39 
  40 class Maze:
  41     def __init__(self):
  42         self.rooms=[]
  43 ##        r1=Room(1)             # FactoryMethod 
  44 ##        r2=Room(2)
  45         r1 = self.createRoom(1)
  46         r2 = self.createRoom(2)
  47 ##        self.d=Door(r1,r2)     # FactoryMethod 
  48         self.d = self.createDoor(r1,r2)
  49         self.addRoom(r1)
  50         self.addRoom(r2)
  51     def addRoom(self,aRoom):
  52         self.rooms.append(aRoom)
  53     def getRooms(self):
  54         return self.rooms
  55     def getDoors(self):
  56         return [self.d,]
  57     def createRoom(self,anId):
  58         raise NotImplementedError
  59     def createDoor(self,aRoom1, aRoom2):
  60         raise NotImplementedError
  61         
  62 class FireMaze(Maze):
  63     def createRoom(self,anId):
  64         return FireRoom(anId)
  65     def createDoor(self, aRoom1, aRoom2):
  66         return FireDoor(aRoom1, aRoom2)
  67 
  68 class IceMaze(Maze):
  69     def createRoom(self,anId):
  70         return IceRoom(anId)
  71     def createDoor(self, aRoom1, aRoom2):
  72         return IceDoor(aRoom1, aRoom2)
  73     
  74 def makeMaze(aMazeType):
  75     if aMazeType == 'fire':
  76         return FireMaze()
  77     elif aMazeType == 'ice':
  78         return IceMaze()
  79 
  80 class TestMaze(unittest.TestCase):
  81     def testRoom(self):
  82         r1=Room(1)
  83         self.assert_(r1)
  84     def testDoor(self):
  85         r1=Room(1)
  86         r2=Room(2)
  87         d=Door(r1,r2)
  88         self.assert_(d)
  89         rooms=r1.getConnectedRooms()
  90         self.assertEquals(r2,rooms)
  91     def testMakeFireMaze(self):
  92         m=makeMaze('fire')
  93         r = m.getRooms()[0]
  94         self.assert_(isinstance(r,FireRoom))
  95         d = m.getDoors()[0]
  96         self.assert_(isinstance(d,FireDoor))
  97         expected = "It's very hot"
  98         self.assertEquals(expected ,r.lookAround())
  99 
 100     def testMakeIceMaze(self):
 101         m=makeMaze('ice')
 102         r = m.getRooms()[0]
 103         self.assert_(isinstance(r,IceRoom))
 104         d = m.getDoors()[0]
 105         self.assert_(isinstance(d,IceDoor))
 106         expected = "It's very cold"
 107         self.assertEquals(expected ,r.lookAround())
 108 
 109 if __name__=='__main__':
 110     unittest.main(argv=('','-v'))
web biohackers.net