-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposite.py
68 lines (48 loc) · 1.43 KB
/
composite.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from abc import ABC, abstractmethod
class IComponent(ABC):
@abstractmethod
def play(self) -> None:
pass
@abstractmethod
def setPlaySpeed(self, speed) -> None:
pass
@abstractmethod
def getName(self) -> str:
pass
class Playlist(IComponent):
def __init__(self, playListName:str) -> None:
self.playListName = playListName
self.playlist = []
def add(self, component:IComponent) -> None:
self.playlist.append(component)
def remove(self, component:IComponent) -> None:
self.playlist.remove(component)
def play(self) -> None:
for component in self.playlist:
component.play()
def setPlaySpeed(self, speed:int) -> None:
for component in self.playlist:
component.setPlaySpeed(speed)
def getName(self) -> str:
return self.playListName
class Song(IComponent):
def __init__(self, songName:str, artist:str, speed:int=1) -> None:
self.songName = songName
self.artist = artist
self.speed = speed
def play(self) -> None:
print("Playing | {} - {}".format(self.songName,self.artist))
def setPlaySpeed(self, speed:int) -> None:
self.speed = speed
def getName(self) -> str:
return self.playListName
playlist_1 = Playlist("My Famous songs")
playlist_2 = Playlist("All my songs")
song_1 = Song("Bla bla bla", "Mohab")
song_2 = Song("Yada yada yada", "Mohab")
playlist_1.add(song_1)
playlist_1.add(song_2)
song_3 = Song("Hamda", "Mohab")
playlist_2.add(song_3)
playlist_2.add(playlist_1)
playlist_2.play()