首页 \ 问答 \ 组合/继承/工厂 - 本案例的最佳模式(Composition/Inheritance/Factory - Best Pattern For This Case)

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

好[your_time_of_day],

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

问题第一:

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

情况

我有一个基类AudioPlayer:

class BaseAudioPlayer {
    public track;
    protected seekBar;

    public togglePlay() {
        //
    }

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

一些玩家类会从这样延伸:

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

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

请记住,我实际上在父类和子类中有更多的方法,并且在一些孩子中有许多方法不在其他孩子中。 当然,没有涉及多重继承但我在某些时候看到可能成为多个相似的儿童玩家的可能性(糟糕!)。

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

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

更新1:

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

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

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

  • 继承用于避免重复,所有玩家都是 BaseAudioPlayers。 但是,所有玩家也都有 togglePlayseek方法。

  • 我想使用合成,因为我可以看到一个玩家没有seek方法的可能性,或者将来会出现这种情况。

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

    • 可以有很多装饰器( @playable@seekable
    • 可以使用基本播放器服务(如@amuse的答案)并具有冗余方法。

Good [your_time_of_day],

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...

Question first:

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)?

Situation

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!).

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.

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.

Update 1:

As @JocelynLecomte commented, my question may be unclear.

  • 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).

  • 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.

  • 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.

    • Could have many decorators (@playable, @seekable)
    • Could use a base player service (as in @amuse 's answer) and have redundant methods.

原文:https://stackoverflow.com/questions/40895466
更新时间:2024-04-27 15:04

最满意答案

尝试这个。

   function CheckMaxLength(Object, MaxLen) 
    {
        if (Object.value.length >= MaxLen) { //find textbox and use substring } 

        return Object.value.substring(0, 255);
    }

如果你想在按键,粘贴,焦点等处理这个,然后试试这个。

   var MaxLen = 255;
   $('textboxSelector').bind('keypress focus blur paste', function () {
        if (this.value.length > MaxLen) {
            this.value = this.value.substring(0, MaxLen);
            return false;
        }
    });

Try this.

   function CheckMaxLength(Object, MaxLen) 
    {
        if (Object.value.length >= MaxLen) { //find textbox and use substring } 

        return Object.value.substring(0, 255);
    }

If you want to handle this on keypress, paste, focus etc then try this.

   var MaxLen = 255;
   $('textboxSelector').bind('keypress focus blur paste', function () {
        if (this.value.length > MaxLen) {
            this.value = this.value.substring(0, MaxLen);
            return false;
        }
    });

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。