首页 > 解决方案 > 如何创建一个扩展未预先确定的其他类的类

问题描述

我正在尝试创建一个应该能够扩展几个基类的类。在下面的示例中,我想使用石头或木头作为教室的基类。为此,我尝试创建一个可以选择适当基类的材料类。但我不让它工作。

const EventEmitter = require('events').EventEmitter; class Stone extends EventEmitter{ constructor(weight, color){ super(); this.color = color; this.weight = weight; this.hard = true this.start(); } testEmitterFunction(){ this.emit('test'); } start(){ setInterval(this.testFunc.bind(this), 500); } } class Wood{ constructor(weight, color){ this.color = color; this.weight = weight; this.hard = false; } } class Material{ constructor(mat, weight, color){ switch(mat){ case 'wood': return new Wood(weight, color); case 'stone': return new Stone(weight, color); } } } class House extends Material{ constructor(mat, weight, color, name){ super(mat, weight, color) this.name = name; this.on('test', (arg) => { console.log('1') }); this.test(); } test(){ console.log('test house function'); } } class House2 extends Stone{ constructor(weight, color, name){ super(weight, color) this.name = name; this.on('test', (arg) => { console.log('2') }); this.test(); } test(){ console.log('test house2 function'); } } const home = new House('stone', 8, 'green', 'homesweethome'); const home2 = new House2(8, 'green', 'homesweethome');

我希望实例 home 具有与实例 home2 相同的行为。但在这种情况下,执行 console.log('test house function') 的测试功能不起作用。我尝试了其他解决方案,但 EventEmitter 不起作用或石头属性不可用。

标签: javascriptnode.jsoopecmascript-6

解决方案


正如我在评论中提到的那样,使用组合而不是继承可能会更好地完成您想要做的事情。

作为一般的简化,如果你可以说“我的 X 是 Y 的一种类型”或“我的 X 是 Y”,这是有道理的,那就是继承,但如果你说“我的 X 是由 Y 组成”或“我的 X 包含Y" 那么你应该使用组合。应用于您的案例,石头和木头都是一种材料。房子是一种材料吗?我不会这么说,但房子是由石头或木头制成的,或者更确切地说,房子是由材料制成的,这意味着我们应该为此使用合成。

如果您想保留将字符串传递给House设置材质的构造函数的能力,那么您仍然可以这样做。请参阅House#setMaterial底部的代码示例,尽管将来工厂模式可能对您更有效。

您的结构的另一个问题是它杀死了polymorphism。如果您想要在两者中执行相同操作的方法,Stone并且Wood说“破坏”,那么您必须复制粘贴相同的代码,但如果它们都继承自通用 Material 类型,那么您只需要创建方法一次在基类中。

我也希望能够为我的房子使用来自石头的 EventEmitter。即 house.on(...) 而不是 house.stone.on(...)

在使用事件发射器时,我建议您在可能的最高级别创建一个,然后将其传递给需要它的组件。在这种情况下,House 可以将事件发射器传递给材质或任何其他组件(例如房间)。由于 Javascript 的疯狂,House 可以成为事件发射器并将自己传递给材料。请参阅下面类中的House#setEmitter函数House。观察它是如何在多态函数中使用的Material#Break

/** Unimportant */
class EventEmitter {
  constructor(){ this.map = {} }
  on(e, cb){
    if(!this.map[e]) this.map[e] = []
    this.map[e].push(cb)
  }
  emit(event,...data){
    if(!this.map[event]) return
    this.map[event].forEach(cb=>cb(...data))
  }
}
/**/

class Material {
  constructor(name = 'Something', weight = 5, color = 'black', hard = true){
    this.weight = weight
    this.color = color
    this.hard = hard
    this.name = name
  }
  setEmitter(emitter){
    this.emitter = emitter
  }
  
  break(){
    if(this.emitter){
      this.emitter.emit(`break`, `The ${this.name} has broken` )
    }
  }
  
  describe(){
    return `${this.weight}lb ${this.hard?'hard':'soft'} ${this.color} ${this.name}`
  }
}

class Stone extends Material {
  constructor(weight = 8, color = 'gray'){
    super("Stone", weight, color, true)
  }
}

class Wood extends Material {
  constructor(weight=4, color="brown"){
    super("Wood", weight, color, false)
  }
}

class House extends EventEmitter {
  constructor(material, name){
    super()
    this.material = this.setMaterial(material)
    this.name = name
    this.on('break', (what)=>{
      console.log(`${this.name} Event: `+what)
    })
  }
  
  setMaterial(mat){
    const matMap = {
      stone : Stone,
      wood : Wood
    }
    // Just gets a default material
    if(typeof mat == 'string'){
      mat = new matMap[mat]()
    }
    mat.setEmitter(this)
    return mat
  }
  // Logs information about the material
  describe(){
    console.log(`A house named ${this.name} made out of ${this.material.describe()}`)
  }
}


// Examples

// Method 1: Create a basic stone house and set material color later
const stoneHouse = new House("stone", "MyHouse")
stoneHouse.describe()
stoneHouse.material.color = "blue"
stoneHouse.describe()
stoneHouse.material.break()

// Method 2: Create a material and pass it to the house
const myMaterial = new Wood(6, "green")
const woodHouse = new House(myMaterial, "WoodHouse")
woodHouse.describe()
// Call a function that emits an event to the house
myMaterial.break()


推荐阅读