Помогите пожалуйста исправить ошибку. Я пытаюсь применить на практике шаблон проектирования "Фабрика".
from datetime import datetime, date, time
class GameElement(object):
def __init__(self, name):
self.name = name
class Boat(GameElement):
def __init__(self, name, peoples, speed, color, oars):
GameElement.__init__(self, name)
self.peoples = peoples
self.speed = speed
self.color = color
self.oars = oars
Boat.quantity += 1
quantity = 0
def __str__(self):
return str(self.__class__.__name__) + ': ' + str(self.__dict__)
class Raft(GameElement):
def __init__(self, name, peoples, speed, color, sails):
GameElement.__init__(self, name)
self.peoples = peoples
self.speed = speed
self.color = color
self.sails = sails
Raft.quantity += 1
quantity = 0
def __str__(self):
return str(self.__class__.__name__) + ': ' + str(self.__dict__)
class BoatsGenerator(object):
def factory(self, name, peoples, speed, color, oars):
print(str(name) + '::' + 'created boat')
#return Boat(name, peoples, speed, color, oars)
class RaftsGenerator(object):
def factory(name, peoples, speed, color, sails):
print(str(name) + '::' + 'created raft')
#return Raft(name, peoples, speed, color, sails)
class World(object):
def __new__(cls,*dt,**mp):
if cls.obj is None:
cls.obj = object.__new__(cls,*dt,**mp)
World.quantity += 1
boat1 = BoatsGenerator.factory(name='beda', peoples=8, speed=110, color='red', oars=2)
raft1 = RaftsGenerator.factory(name='abibas', peoples=3, speed=5, color='maroon', sails=0)
print(boat1)
print(raft1)
return cls.obj
obj = None
quantity = 0
def __str__(self):
return str(self.__class__.__name__) + ': ' + str(self.__dict__)
world1 = World()
world1.creared_at = datetime.now().strftime("%A, %d. %B %Y %I:%M%p")
print(world1)
print('worlds quantity: ' + str(World.quantity))
В этом коде есть классы для объектов Boat и Raft, а так же фабрики для каждого из них(BoatsGenerator, RaftsGenerator). Сама процедура создания происходит в инициализаторе класса World. В результате должно получиться 2 объекта, но у меня консоль выводит сообщение об ошибке:
kalinin@kalinin ~/python/boats3 $ python index.py
Traceback (most recent call last):
File "index.py", line 74, in <module>
world1 = World()
File "index.py", line 58, in __new__
boat1 = BoatsGenerator.factory(name='beda', peoples=8, speed=110, color='red', oars=2)
TypeError: unbound method factory() must be called with BoatsGenerator instance as first argument (got nothing instead)