多类扩展与内置类

huangapple go评论76阅读模式
英文:

multiple class extensions with built-in classes

问题

我一直在研究TypeScript中的混合(mixins),并想知道如何实现我的问题。
我有四个类:

AudioNode(原生)
CustomAudioNode(自定义)
GainNode(原生)
CustomGainNode(自定义)

我想实现以下继承关系:

  • 1 CustomAudioNode 继承自 AudioNode
  • 2 GainNode 继承自 CustomAudioNode
  • 3 CustomGainNode 继承自 GainNode[GainNode, CustomAudioNode]

我该如何实现这个继承关系?

英文:

I've been looking at mixins in typescript and wonder how I could implement my problem.
I have four classes:

AudioNode (native)
CustomAudioNode ( custom )
GainNode ( native )
CustomGainNode ( custom )

I would like to achieve this inheritance:

  • 1 CustomAudioNode extends AudioNode.
  • 2 GainNode extends CustomAudioNode.
  • 3 CustomGainNode extends GainNode or [GainNode,CustomAudioNode].

How could I achieve this?

答案1

得分: 1

不可能直接构建像问题中所寻找的那样的关系,其中涉及到本地AudioNode和本地GainNode,每个都有另一个自定义实现(CustomAudioNodeCustomGainNode),其中...

  1. CustomAudioNode扩展AudioNode
  2. GainNode扩展CustomAudioNode
  3. CustomGainNode扩展GainNode[GainNode,CustomAudioNode]

这是因为...

  • GainNode 已经扩展了AudioNode
  • AudioNode 既不能被扩展,也不能以有意义的方式包装(因此,它将保持不变)。

但可以做的是...

  • 提供与AudioNode相关的任何附加行为作为混入,最好是作为基于函数的混入实现。

这意味着,现在只提供一个非常干净的代码库,其中只涉及一个CustomGainNode,它扩展了本地的GainNode并提供自定义增益节点行为,而自定义音频节点行为被单独实现,但在任何自定义增益节点的构建/实例化时应用。

// 纯自定义音频节点特定功能。

function customAudioNodeBahavior01() {
  // `this` 指的是当前的 `CustomGainNode` 实例,
  // 也是 `GainNode` 和 `AudioNode` 的实例。
}
function customAudioNodeBahavior02() {
  // `this` 指的是当前的 `CustomGainNode` 实例,
  // 也是 `GainNode` 和 `AudioNode` 的实例。
}

// 基于函数的混入。
function withCustomAudioNodeBahavior() {
  Object.assign(this, {
    customAudioNodeBahavior01,
    customAudioNodeBahavior02,
  });
}

// 自定义增益节点子类/子类型。
class CustomGainNode extends GainNode {
  constructor(...args) {

    // - `args` 总是 ...
    //   `context`和`options`
    //   ... 就像 ...
    //   ```
    //   constructor(context, options) {
    //     super(context, options);
    //     // ...
    //   }
    //   ```

    // 继承部分。
    super(...args);

    // 基于混入的组合部分。
    withCustomAudioNodeBahavior.call(this);

    // - 实施进一步的自定义增益节点
    //   特定行为。
  }
}

注意

为了符合Web音频API,问题中的人可能考虑实现BaseAudioContext.createGain工厂方法的相对应版本...类似于BaseAudioContext.createCustomGain

英文:

It is not possible to straightforwardly build a relationship like the one the OP is looking for, which involves both native AudioNode and native GainNode, each with another custom implementation (CustomAudioNode and CustomGainNode) where ...

> 1. CustomAudioNode extends AudioNode.
> 2. GainNode extends CustomAudioNode.
> 3. CustomGainNode extends GainNode or [GainNode,CustomAudioNode].

This is due to ...

  • GainNode already extends AudioNode.
  • AudioNode neither can be extended nor can it be wrapped in any meaningful way (thus, it is going to remain untouched).

But what can be done instead is ...

  • providing any additional behavior which is related to AudioNode as mixin, preferably implemented as function based mixin.

This is, one provides a very clean code base which now involves just a CustomGainNode which extends the native counterpart and provides custom gain node behavior, whereas custom audio node behavior gets implemented separately, but applied at any custom gain node's construction/instantiation time.

// the pure custom audio node specific functionality.

function customAudioNodeBahavior01() {
  // `this` refers to the current `CustomGainNode` instance
  //  which too is an instance of `GainNode` and `AudioNode`.
}
function customAudioNodeBahavior02() {
  // `this` refers to the current `CustomGainNode` instance
  //  which too is an instance of `GainNode` and `AudioNode`.
}

// the function based mixin.
function withCustomAudioNodeBahavior() {
  Object.assign(this, {
    customAudioNodeBahavior01,
    customAudioNodeBahavior02,
  });
}

// the custom gain node subclass/subtype.
class CustomGainNode extends GainNode {
  constructor(...args) {

    // - `args` are always ...
    //   `context`and `options`
    //   ... like with ...
    //   ```
    //   constructor(context, options) {
    //     super(context, options);
    //     // ...
    //   }
    //   ```

    // the inheritance part.
    super(...args);

    // the mixin based composition part.
    withCustomAudioNodeBahavior.call(this);

    // - implementing further custom gain node
    //   specific behavior.
  }
}

Note

In order to comply with the Web Audio Api, the OP might think about implementing the pendant of the BaseAudioContext.createGain factory method ... something like BaseAudioContext.createCustomGain.

huangapple
  • 本文由 发表于 2023年3月31日 23:04:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900036.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定