mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-29 04:53:59 +03:00
1ad852d193
* Update index page * Update providers index page * Make a little wording fix on containers index page * Refactor factory provider docs header * Update factory injection docs * Update factory init injections example and picture * Start work on underlying providers * Finish the docs for factory arguments to the underlying providers * Edit providers delegation section * Edit section about specialized factory provider * Edit abstract factory section * Edit FactoryAggregate docs * Add meta keywords and description
56 lines
1.1 KiB
Python
56 lines
1.1 KiB
Python
"""`FactoryAggregate` provider example."""
|
|
|
|
import dataclasses
|
|
import sys
|
|
|
|
from dependency_injector import providers
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class Game:
|
|
player1: str
|
|
player2: str
|
|
|
|
def play(self):
|
|
print(
|
|
f'{self.player1} and {self.player2} are '
|
|
f'playing {self.__class__.__name__.lower()}'
|
|
)
|
|
|
|
|
|
class Chess(Game):
|
|
...
|
|
|
|
|
|
class Checkers(Game):
|
|
...
|
|
|
|
|
|
class Ludo(Game):
|
|
...
|
|
|
|
|
|
game_factory = providers.FactoryAggregate(
|
|
chess=providers.Factory(Chess),
|
|
checkers=providers.Factory(Checkers),
|
|
ludo=providers.Factory(Ludo),
|
|
)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
game_type = sys.argv[1].lower()
|
|
player1 = sys.argv[2].capitalize()
|
|
player2 = sys.argv[3].capitalize()
|
|
|
|
selected_game = game_factory(game_type, player1, player2)
|
|
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
|