87,922
社区成员
发帖
与我相关
我的任务
分享
class Player {
constructor (nodeId, videoId) {
this.nodeId = nodeId
this.videoId = videoId
}
play () {
console.log('play...', this.nodeId, this.videoId)
}
pause () {
console.log('pause...', this.nodeId, this.videoId)
}
}
class TxPlayer extends Player {
constructor (options) {
super(options.containerId, options.vid)
}
play () {
super.play()
}
pause () {
super.pause()
}
}
class EgamePlayer extends Player {
constructor (options) {
super(options.domId, options.videoId)
}
start () {
super.play()
}
stop () {
super.pause()
}
}
/**
* @description `TxPlayer播放器`
*/
class ITxPlayer implements IPlayer {
/** `TxPlayer的实例化对象` */
private player;
/**
* 构造器
* @param {Record<string, any>} options 配置
*/
constructor (options: Record<string, any>) {
this.player = new TxPlayer(options)
}
play (): void {
this.player.play()
console.log('TXPlayer play')
}
pause (): void {
this.player.pause()
console.log('TXPlayer pause')
}
}
/**
* @description `EgamePlayer播放器`
*/
class IEgamePlayer implements IPlayer {
/** `EgamePlayer的实例化对象` */
private player;
/**
* 构造器
* @param {Record<string, any>} options 配置
*/
constructor (options: Record<string, any>) {
this.player = new EgamePlayer(options)
}
play (): void {
this.player.start()
console.log('EgamePlayer play')
}
pause (): void {
this.player.stop()
console.log('EgamePlayer pause')
}
}
对外统一的播放器类Player
/**
* @description `统一的播放器类Player`
*/
class Player implements IPlayer {
/** `对应播放器实例化对象` */
private player: IPlayer;
/**
* 构造器
* @param {number} mode 类型
* @param {Record<string, any>} options 配置
*/
constructor (mode: number, options: Record<string, any>) {
// 这里只是简单的用mode参数来代表“不同的视频类型”
this.getInstance(mode, options)
}
/**
* @description `根据类型获取播放器实例`
* @param {number} mode 类型
* @param {Record<string, any>} options 配置
*/
private getInstance (mode: number, options: Record<string, any>) {
switch (mode) {
case 1:
this.player = new ITxPlayer(options)
break
case 2:
this.player = new IEgamePlayer(options)
break
default:
this.player = new ITxPlayer(options)
break
}
}
play (): void {
this.player.play()
}
pause (): void {
this.player.pause()
}
}
export default Player
/**
* @description `播放器接口`:有需要扩展的方法时继续申明,并在对应的播放器类中实现即可
*/
interface IPlayer {
/**
* @description `播放`
*/
play (): void
/**
* @description `暂停`
*/
pause (): void
}
实际使用时,下面两个类是不需要写的,就是简单的申明了一下要调用的两个播放器(TxPlayer,EgamePlayer);我本地没有这俩货,为了后面代码不报错
class TxPlayer {
constructor (options: Record<string, any>) {
}
play () {}
pause () {}
}
class EgamePlayer {
constructor (options: Record<string, any>) {
}
start () {}
stop () {}
}