Skip to content

Commit ad6fd4b

Browse files
author
Mateus Furquim
committed
Add Protocol to factory pattern
1 parent cc54961 commit ad6fd4b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

patterns/creational/factory.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
*TL;DR
2222
Creates objects without having to specify the exact class.
2323
"""
24+
from typing import Dict
25+
from typing import Protocol
26+
from typing import Type
27+
28+
29+
class Localizer(Protocol):
30+
def localize(self, msg: str) -> str:
31+
pass
2432

2533

2634
class GreekLocalizer:
@@ -41,10 +49,10 @@ def localize(self, msg: str) -> str:
4149
return msg
4250

4351

44-
def get_localizer(language: str = "English") -> object:
52+
def get_localizer(language: str = "English") -> Localizer:
4553

4654
"""Factory"""
47-
localizers = {
55+
localizers: Dict[str, Type[Localizer]] = {
4856
"English": EnglishLocalizer,
4957
"Greek": GreekLocalizer,
5058
}

0 commit comments

Comments
 (0)