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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
| class MediaPlayer:
"""
The MediaPlayer defines the standard interface for playing audio files.
"""
def play(self, audio_type: str, file_name: str) -> None:
raise NotImplementedError("This method should be overridden.")
class AdvancedMediaPlayer:
"""
The AdvancedMediaPlayer defines an interface for playing advanced media formats.
"""
def play_mp4(self, file_name: str) -> None:
raise NotImplementedError("This method should be overridden.")
def play_vlc(self, file_name: str) -> None:
raise NotImplementedError("This method should be overridden.")
class Mp4Player(AdvancedMediaPlayer):
"""
Mp4Player is a concrete implementation of AdvancedMediaPlayer for playing MP4 files.
"""
def play_mp4(self, file_name: str) -> None:
print(f"Playing mp4 file: {file_name}")
def play_vlc(self, file_name: str) -> None:
pass
class VlcPlayer(AdvancedMediaPlayer):
"""
VlcPlayer is a concrete implementation of AdvancedMediaPlayer for playing VLC files.
"""
def play_mp4(self, file_name: str) -> None:
pass
def play_vlc(self, file_name: str) -> None:
print(f"Playing vlc file: {file_name}")
class MediaAdapter(MediaPlayer):
"""
MediaAdapter bridges the interface between MediaPlayer and AdvancedMediaPlayer.
"""
def __init__(self, audio_type: str) -> None:
self.advanced_media_player = None
if audio_type == "mp4":
self.advanced_media_player = Mp4Player()
elif audio_type == "vlc":
self.advanced_media_player = VlcPlayer()
def play(self, audio_type: str, file_name: str) -> None:
if audio_type == "mp4":
self.advanced_media_player.play_mp4(file_name)
elif audio_type == "vlc":
self.advanced_media_player.play_vlc(file_name)
class AudioPlayer(MediaPlayer):
"""
AudioPlayer is a concrete implementation of MediaPlayer for playing standard audio files.
"""
def play(self, audio_type: str, file_name: str) -> None:
if audio_type == "mp3":
print(f"Playing mp3 file: {file_name}")
elif audio_type in ("mp4", "vlc"):
# Use MediaAdapter for unsupported formats
adapter = MediaAdapter(audio_type)
adapter.play(audio_type, file_name)
else:
print(f"Invalid media format: {audio_type}")
if __name__ == "__main__":
audio_player = AudioPlayer()
audio_player.play("mp3", "song.mp3")
audio_player.play("mp4", "video.mp4")
audio_player.play("vlc", "movie.vlc")
audio_player.play("avi", "animation.avi")
|