A
A
Anton2017-06-18 17:39:39
Python
Anton, 2017-06-18 17:39:39

Did I do the Abstract Factory right?

Hello. I dealt with the abstract factory pattern, tried to write my own factory, the point is that there is a certain rpg game and at each level there can be 2 mobs and one boss, did I implement it correctly?

class LevelFactory(metaclass=abc.ABCMeta):
    @abc.abstractmethod
    def get_mob_1(self):
        pass

    @abc.abstractmethod
    def get_mob_2(self):
        pass

    @abc.abstractmethod
    def get_boss(self):
        pass


class Mob(object):
    def __init__(self, hp, damage):
        self.__hp = hp
        self.__damage = damage

    def __str__(self):
        return 'hp: {}, damage: {}'.format(self.__hp, self.__damage)


class Boss(object):
    def __init__(self, hp, damage, super_kick):
        self.hp = hp
        self.damage = damage
        self.super_kick = super_kick

    def __str__(self):
        return 'The boss. Damage: {}, hp: {}, super kick: {}'.format(self.damage, self.hp, self.super_kick)


class Level1Factory(LevelFactory):
    def get_boss(self):
        return Boss(300, 43, 'lightning')

    def get_mob_1(self):
        return Mob(45, 10)

    def get_mob_2(self):
        return Mob(60, 15)


class Level2Factory(LevelFactory):
    def get_boss(self):
        return Boss(590, 89, 'fire')

    def get_mob_1(self):
        return Mob(83, 29)

    def get_mob_2(self):
        return Mob(113, 45)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-06-19
Reytarovsky @Antonchik

The factory is done correctly, but you need to look at the code where it is used. Since your factory is responsible for creating several different objects, there is a risk of violating the principle of separation of interfaces

A
Aristarkh Deryapa, 2017-06-18
@isisTance

You can make the init function, and take the level of the level. Then a small if and profit)
ZY . If you get bored, try doing calculations with a level. Those. each level will give for example 5% to the boss's HP, or the number of enemies. Yes, it is suitable for infinite generation. And the position, for example, is random. I hope everything is clear)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question