class MusicPlayer {
constructor(){
this.type = '';
this.menu = '';
this.playerList = '';
this.mainWindows = '';
this.controlStrip = '';
this.collectionList = '';
}
get type(){
return this.type;
}
set type(type){
this.type = type;
}
// 省略其他 get 和 set 方法
get menu(){
return this.menu;
}
set menu(menu){
this.menu = menu;
}
get playerList(){
return this.playerList;
}
set playerList(playerList){
this.playerList = playerList;
}
get mainWindows(){
return this.mainWindows;
}
set mainWindows(mainWindows){
this.mainWindows = mainWindows;
}
get controlStrip(){
return this.controlStrip;
}
set controlStrip(controlStrip){
this.controlStrip = controlStrip;
}
get collectionList(){
return this.collectionList;
}
set collectionList(collectionList){
this.collectionList = collectionList;
}
}
class PlayerBuilder {
constructor() {
this._player = new MusicPlayer();
}
buildType(){
throw new Error("This method must be overwritten!");
}
buildMenu(){
throw new Error("This method must be overwritten!");
}
buildPlayerList(){
throw new Error("This method must be overwritten!");
}
buildMainWindows(){
throw new Error("This method must be overwritten!");
}
buildControlStrip(){
throw new Error("This method must be overwritten!");
}
buildCollectionList(){
throw new Error("This method must be overwritten!");
}
createPlayer(){
return this._player;
}
}
class FullPlayerBuilder extends PlayerBuilder{
constructor(){
super();
}
buildType() {
this._player.type = "完整模式";
}
buildMenu() {
this._player.menu = "菜单";
}
buildPlayerList() {
this._player.playerList = "播放列表";
}
buildMainWindows() {
this._player.mainWindows = "主界面";
}
buildControlStrip() {
this._player.controlStrip = "控制条";
}
buildCollectionList() {
this._player.collectionList = "收藏列表";
}
}
class SimplePlayerBuilder extends PlayerBuilder{
buildType() {
this._player.type = "精简模式";
}
buildMenu() {
this._player.menu = null;
}
buildPlayerList() {
this._player.playerList = null;
}
buildMainWindows() {
this._player.mainWindows = "主界面";
}
buildControlStrip() {
this._player.controlStrip = "控制条";
}
buildCollectionList() {
this._player.collectionList = null;
}
}
class MemoryPlayerBuilder extends PlayerBuilder{
buildType() {
this._player.type = "记忆模式";
}
buildMenu() {
this._player.menu = null;
}
buildPlayerList() {
this._player.playerList = null;
}
buildMainWindows() {
this._player.mainWindows = "主界面";
}
buildControlStrip() {
this._player.controlStrip = "控制条";
}
buildCollectionList() {
this._player.collectionList = "收藏列表";
}
}
class PlayerController {
construct(pb) {
pb.buildType();
pb.buildMenu();
pb.buildPlayerList();
pb.buildMainWindows();
pb.buildControlStrip();
pb.buildCollectionList();
return pb.createPlayer();
}
}
let builder = new FullPlayerBuilder();
let controller = new PlayerController();
let player = controller.construct(builder);
console