组合/继承/工厂 - 本案例的最佳模式 [英] Composition/Inheritance/Factory - Best Pattern For This Case

查看:125
本文介绍了组合/继承/工厂 - 本案例的最佳模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好[your_time_of_day],

Good [your_time_of_day],

今天我一直在学习javascript(es6)中的构图和工厂函数。我了解组成应该是优先于继承并且已经同意(至少在javascript中)。然后,我意识到我的情况是我应该使用合成......

Today I have been learning about composition and factory functions in javascript (es6). I understand that composition should be preferred over inheritance and have come to agree with that (at least in javascript). Then, I realised that I have a situation where I should be using composition...

有没有办法可以改变我复杂的,继承的结构,所以类是由函数组成而没有荒谬的装饰器数量?我是否错过了一般的构图(我觉得我有)?

Is there a way I can change my complex, inherited structure so classes are composed of functions without having ridiculous numbers of decorators? Have I missed something about composition in general (I feel like I have)?

我有一个基类AudioPlayer:

I have a base class AudioPlayer:

class BaseAudioPlayer {
    public track;
    protected seekBar;

    public togglePlay() {
        //
    }

    public seek(time) {
       //some seek methods using this.seekBar
    }
}

并且一些玩家类会从此类推出所以:

And a few player classes would extend from this like so:

class MainAudioPlayer extends BaseAudioPlayer {
    public loadTrack(track) {
        //This is horrible
        this.track = track;
    }

    public setSeekBar(seekBar) {
        //This is horrible
        this.seekBar = seekBar
    }
}

请记住我在父类和子类中实际上有很多方法,有些孩子有很多方法那些不在别人身上。当然,没有涉及多重继承,但我认为在某些情况下可能会成为多个相同的儿童玩家的可能性(糟糕!)。

Please bare in mind I actually have a lot more methods in parent and child classes and there are many methods in some children that are not in others. Of course, there is no multiple inheritance involved but I see at some point that might become a possibility with multiple alike child players (bad!).

我可以使用很多装饰器喜欢 @playable() @seekable()等但是我看到,最终,mixin的数量会变得巨大。我想我也可以以类似的方式使用工厂功能,但看到同样的问题。

I could use many decorators like @playable() @seekable() etc. but then I see that, eventually, the number of mixins would become huge. I guess I could also use factory functions in a similar manner but see the same problem.

完全披露:我正在使用Angular2并且已经削减了很多代码,以便继续讨论使用哪种设计模式而不是特定框架中的实现。

Full disclosure: I am using Angular2 and have cut back the code a lot to keep any discussion about which design pattern to use and not about an implementation in a specific framework.

如@JocelynLecomte评论,我的问题可能不清楚。

As @JocelynLecomte commented, my question may be unclear.


  • MainAudioPlayer(和其他玩家)继承自BaseAudioPlayer,因为所有音频播放器都必须 togglePlay 搜索,还有一些其他方法(特定于angular2,因此不包括在内)。

  • The MainAudioPlayer (and other players) inherit from BaseAudioPlayer since all audio players must have togglePlay, seek, and a few other methods (angular2 specific so not included here).

目前,有三个类继承自BaseAudioPlayer:MainAudioPlayer,DetailAudioPlayer和CardAudioPlayer。将来可能会有更多,每个都有自己的具体方法。

Currently, there are three classes that inherit from BaseAudioPlayer: MainAudioPlayer, DetailAudioPlayer and CardAudioPlayer. In the future there may be more and each has there own specific methods.

继承用于避免重复,所有玩家都是 BaseAudioPlayers。但是,所有玩家 togglePlay 搜索方法。

Inheritance was used to avoid duplication and all players are BaseAudioPlayers. However, all players also have togglePlay and seek methods.

我想使用合成,因为我可以看到玩家没有搜索方法或其他东西的可能性在未来的这些方面。

I'd like to use composition since I could see a possibility of a player that does not have a seek method or something along those lines in the future.

似乎使用合成将导致所有玩家类中的大量锅炉板代码,我想避免这种情况。

It seems to that using composition would lead to a lot of boiler plate code in all player classes and I'd like to avoid this.


  • 可能有很多装饰者( @playable @seekable

  • 可以使用基本播放器服务(如@amuse的答案)并且有多余的方法。

推荐答案

我想如果你想在基类中重用基本方法,你可能想要使用组合而不是继承(即:将BasePlayerComponent定义为MainAudioPlayer的属性):

I think if you want to reuse the base method in base class,you may want to use composition instead of inheritance (ie:define BasePlayerComponent as a property of MainAudioPlayer):

class MainAudioPlayer{
    constructor(){
    this.basePlayerComponent=new BasePlayerComponent();
    }
    public loadTrack(track) {
        //This is horrible
        this.track = track;
    }

    public setSeekBar(seekBar) {
        //This is horrible
        this.seekBar = seekBar
    }

    public togglePlay() {
        this.basePlayerComponent.togglePlay();
    }

    public seek(time) {
        this.basePlayerComponent.seek(time);
    }
}

这篇关于组合/继承/工厂 - 本案例的最佳模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆