class Song {
constructor(title, artist, format) {
this.title = title;
this.artist = artist;
this.format = format;
}
}
// Decoder.js (Strategy Pattern)
class Decoder {
decode(format) {
return `Decoding ${format} format...`;
}
}
class Mp3Decoder extends Decoder {
decode(format) {
return `Decoding MP3 format: ${format}...`;
}
}
class WavDecoder extends Decoder {
decode(format) {
return `Decoding WAV format: ${format}...`;
}
}
// MusicPlayer.js (Observer Pattern - Subject)
class MusicPlayer {
constructor() {
this.observers = [];
}
play(song) {
const message = `Playing song: ${song.title} - ${song.artist}`;
this.notifyObservers();
return message;
}
registerObserver(observer) {
this.observers.push(observer);
}
removeObserver(observer) {
const index = this.observers.indexOf(observer);
if (index !== -1) {
this.observers.splice(index, 1);
}
}
notifyObservers() {
this.observers.forEach((observer) => console.log(observer.update()));
}
}
// Observer.js (Observer Pattern)
class Observer {
constructor() {
this.subject = null;
}
setSubject(subject) {
this.subject = subject;
this.subject.registerObserver(this);
}
update() {
// UI update logic when music playback changes
return "Updating UI...";
}
}
// DecoderFactory.js (Factory Pattern)
class DecoderFactory {
createDecoder(format) {
switch (format) {
case "MP3":
return new Mp3Decoder();
case "WAV":
return new WavDecoder();
default:
throw new Error(`Decoder not available for format: ${format}`);
}
}
}
// Playlist.js (Composite Pattern)
class Playlist {
constructor(name) {
this.name = name;
this.songs = [];
}
addSong(song) {
this.songs.push(song);
}
removeSong(song) {
const index = this.songs.findIndex(
(s) => s.title === song.title && s.artist === song.artist
);
if (index !== -1) {
this.songs.splice(index, 1);
}
}
}
// MusicFacade.js (Facade Pattern)
class MusicFacade {
constructor() {
this.streamingService = {
subscribed: false,
};
//this.paymentService = {}; // 수정된 부분
this.musicPlayer = new MusicPlayer();
this.decoderFactory = new DecoderFactory();
this.playlists = [];
}
subscribe() {
// //=this.paymentService.processPayment();
this.streamingService.subscribed = true;
return "Subscribed to the music streaming service.";
}
unsubscribe() {
this.streamingService.subscribed = false;
return "Unsubscribed from the music streaming service.";
}
playMusic(song) {
if (this.streamingService.subscribed) {
const decoder = this.decoderFactory.createDecoder(song.format);
const decodeMessage = decoder.decode(song.format);
const playMessage = this.musicPlayer.play(song);
return `${decodeMessage}\n${playMessage}`;
} else {
return "You need to subscribe to the music streaming service to play music.";
}
}
createPlaylist(name) {
const playlist = new Playlist(name);
this.playlists.push(playlist);
return `Created playlist: ${name}`;
}
addSongToPlaylist(song, playlistName) {
const playlist = this.playlists.find((p) => p.name === playlistName);
if (playlist) {
playlist.addSong(song);
return `Added song to playlist: ${playlistName}`;
} else {
return `Playlist not found: ${playlistName}`;
}
}
removeSongFromPlaylist(song, playlistName) {
const playlist = this.playlists.find((p) => p.name === playlistName);
if (playlist) {
playlist.removeSong(song);
return `Removed song from playlist: ${playlistName}`;
} else {
return `Playlist not found: ${playlistName}`;
}
}
}
// Usage
const musicFacade = new MusicFacade();
console.log(musicFacade.subscribe());
const observer = new Observer();
const song = new Song("Song Title", "Artist", "MP3");
observer.setSubject(musicFacade.musicPlayer);
musicFacade.musicPlayer.registerObserver(observer);
console.log(musicFacade.playMusic(song));
console.log(musicFacade.createPlaylist("My Playlist"));
console.log(musicFacade.addSongToPlaylist(song, "My Playlist"));
console.log(musicFacade.removeSongFromPlaylist(song, "My Playlist"));
console.log(musicFacade.unsubscribe());
Music Streaming Service Development
-
The Facade pattern is used to determine whether the user is a member or not.
-
The Strategy pattern is used to handle various music formats, such as MP3 and WAV.
-
The Observer pattern is used to monitor the music playback status, where the Subject interface is defined to update the UI whenever there are any changes.
-
The Factory pattern is used to create objects necessary for music playback. For example, when playing music in MP3 format, an MP3Decoder object is created, and when playing music in WAV format, a WavDecoder object is created.
-
The Composite pattern is used to construct music playlists.
Can you please review the design pattern I have created and provide feedback on any strange or incorrect parts? I would greatly appreciate your feedback.
Aucun commentaire:
Enregistrer un commentaire