2020-09-01 04:26:21 +03:00
|
|
|
"""`FactoryAggregate` provider example."""
|
|
|
|
|
|
|
|
import dataclasses
|
|
|
|
import sys
|
|
|
|
|
2020-09-03 23:46:03 +03:00
|
|
|
from dependency_injector import containers, providers
|
2020-09-01 04:26:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass
|
|
|
|
class Game:
|
|
|
|
player1: str
|
|
|
|
player2: str
|
|
|
|
|
|
|
|
def play(self):
|
|
|
|
print(
|
2021-09-30 22:32:21 +03:00
|
|
|
f"{self.player1} and {self.player2} are "
|
|
|
|
f"playing {self.__class__.__name__.lower()}"
|
2020-09-01 04:26:21 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Chess(Game):
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class Checkers(Game):
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
class Ludo(Game):
|
|
|
|
...
|
|
|
|
|
|
|
|
|
2020-09-03 23:46:03 +03:00
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
game_factory = providers.FactoryAggregate(
|
|
|
|
chess=providers.Factory(Chess),
|
|
|
|
checkers=providers.Factory(Checkers),
|
|
|
|
ludo=providers.Factory(Ludo),
|
|
|
|
)
|
2020-09-01 04:26:21 +03:00
|
|
|
|
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
if __name__ == "__main__":
|
2020-09-01 04:26:21 +03:00
|
|
|
game_type = sys.argv[1].lower()
|
|
|
|
player1 = sys.argv[2].capitalize()
|
|
|
|
player2 = sys.argv[3].capitalize()
|
|
|
|
|
2020-09-03 23:46:03 +03:00
|
|
|
container = Container()
|
|
|
|
|
|
|
|
selected_game = container.game_factory(game_type, player1, player2)
|
2020-09-01 04:26:21 +03:00
|
|
|
selected_game.play()
|
|
|
|
|
|
|
|
# $ python factory_aggregate.py chess John Jane
|
|
|
|
# John and Jane are playing chess
|
|
|
|
#
|
|
|
|
# $ python factory_aggregate.py checkers John Jane
|
|
|
|
# John and Jane are playing checkers
|
|
|
|
#
|
|
|
|
# $ python factory_aggregate.py ludo John Jane
|
|
|
|
# John and Jane are playing ludo
|