Skip to content

Commit 2076cbc

Browse files
authored
Merge pull request #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+
from abc import ABC, abstractmethod
30+
from typing import List
2931

30-
class Graphic:
31-
def render(self):
32-
raise NotImplementedError("You should implement this.")
32+
33+
class Graphic(ABC):
34+
@abstractmethod
35+
def render(self) -> None:
36+
raise NotImplementedError("You should implement this!")
3337

3438

3539
class CompositeGraphic(Graphic):
36-
def __init__(self):
37-
self.graphics = []
40+
def __init__(self) -> None:
41+
self.graphics: List[Graphic] = []
3842

39-
def render(self):
43+
def render(self) -> None:
4044
for graphic in self.graphics:
4145
graphic.render()
4246

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

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

4953

5054
class Ellipse(Graphic):
51-
def __init__(self, name):
55+
def __init__(self, name: str) -> None:
5256
self.name = name
5357

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

5761

5862
def main():

0 commit comments

Comments
 (0)