Skip to content

Commit 2076cbc

Browse files
authored
Merge pull request faif#336 from rednafi/master
Added type hints to composite pattern
2 parents 4be7de7 + c1ce8b8 commit 2076cbc

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

‎patterns/structural/composite.py‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,37 @@
2626
Describes a group of objects that is treated as a single instance.
2727
"""
2828

29+
fromabcimportABC, abstractmethod
30+
fromtypingimportList
2931

30-
classGraphic:
31-
defrender(self):
32-
raiseNotImplementedError("You should implement this.")
32+
33+
classGraphic(ABC):
34+
@abstractmethod
35+
defrender(self) ->None:
36+
raiseNotImplementedError("You should implement this!")
3337

3438

3539
classCompositeGraphic(Graphic):
36-
def__init__(self):
37-
self.graphics= []
40+
def__init__(self)->None:
41+
self.graphics: List[Graphic]= []
3842

39-
defrender(self):
43+
defrender(self)->None:
4044
forgraphicinself.graphics:
4145
graphic.render()
4246

43-
defadd(self, graphic):
47+
defadd(self, graphic: Graphic) ->None:
4448
self.graphics.append(graphic)
4549

46-
defremove(self, graphic):
50+
defremove(self, graphic: Graphic) ->None:
4751
self.graphics.remove(graphic)
4852

4953

5054
classEllipse(Graphic):
51-
def__init__(self, name):
55+
def__init__(self, name: str) ->None:
5256
self.name=name
5357

54-
defrender(self):
55-
print("Ellipse:{}".format(self.name))
58+
defrender(self)->None:
59+
print(f"Ellipse: {self.name}")
5660

5761

5862
defmain():

0 commit comments

Comments
(0)