2016-11-25 9 views
1

私はtypescriptで多重継承が必要です。 論理的には、多くの機能を階層に組み込むことは良くありません。 私は1つの基本クラスと階層分岐の数を持っています。 しかし、何とかミックスを使っていくつかのメインロジックを別々のクラスに入れる必要があります。それはどのブランチでも使われていません。コードexmpleのTypescript多重継承

UPDATE:現実に

function Mixin = function(mixins:any[]){ // mixin decorator 
    return function(target){ 
     mixins.forEach((mixin) => { 
      add every function from mixing to target prototype, 
      if functions with same name does not exists there, 
      so we are able to do calls to as example different render() functions 

      Will show it in OpenableItem   
     }); 
    } 
} 

function element = function(){ 
    return function(target){ 
     target.prototype.element = function(){ 
      return this.$el; 
     } 
    } 
} 
-------------------------------------------------------------------------------------- 
@element // every item will have this function as Root as Openable 
class BaseItem{ 
    y() => number; // we need to get y position of every item 
} 
OpenableMixin{ 
    render() => render arrow only 
    open(){} => change arrow position and cause menu to fire change event 
    close(){} => change arrow position and cause menu to fire change event 
} 
class ItemsMixin extends OpenableMixing{// for now if item have childs it must be openable, 
        // but Responsive has only tasks and must be openable too 
    addItem(item: BaseItem) // need to add generics 
    removeItem(item: BaseItem) 
} 
-------------------------------------------------------------------------------------- 
@Mixin([ItemsMixin, ActivitiesMixin]) // it can have items and activities 
class OpenableItem extends BaseItem implement ItemsMixin, ActivitiesMixin { // as in typescript docs 
    render(){ 
     // call rendering from BaseItem class 
     super.render(); 
     // call rendering from OpenableMixing 
     OpenableMixin.prototype.render.call(this); 
     // do some separate rendering for OpenableItem only 
     this.$el.append('line'); 
    } 
} 

@Mixin([ItemsMixin]) // it can have items 
class RootItem extends BaseItem implement ItemsMixin{ // and subitems functionality is only for Root class 
    subitems: Array<BaseItem> // need to add generics to be able to put here different item types 
} 
-------------------------------------------------------------------------------------- 
@element 
class Menu{ 
    items: Array<Item> 
} 

@element 
class Timeline{ 
    menu: Menu 
    listAllelement() => { 
     console.log(this.element()); 
     console.log(this.menu.element()); 
     this.menu.items.forEach((item) => { 
      console.log(item.element()); 
      if(item.hasChilds()){ // really it must be (item instanceof RootItem || item instanceof OpenableItem) 
       item.items.forEach((subitem) => { // really we need some recursion here 
        console.log(subitem.element()); 
       }) 
      } 
     }) 
    } 
} 

あなたは多重継承を実装する必要がある場合、それはまれな状況で、あなたはJavaScriptで、このような問題を持つことができたときに、それは非常にまれにしかありません。しかし、すべてのアイテムは、必要に応じて異なる機能を持つことができます。

mixinsの数を持つことができる異なるアイテムが存在する可能性があることを想像してください。すべてを基礎にすることは賢明ですか?そして、この問題に対するあなたのアプローチは何ですか?

+0

私はこのためにDIを使うべきです。 – Dieterg

+0

@LenilsondeCastro複数の継承のアイデアは多くの場合OOPで解消されますが、実際にはいかなる原則も違反していません。実装するのは簡単ではなく、それはとにかく愚かな考えです。実際には、すべてのエンティティはさまざまな無関係な抽象化で分類することができます。そのような構造化組織を言語で一流のサポートを提供するのはうまくいくと思います。 – Alex

+0

@Dieterg申し訳ありませんが、このような状況でDIがどのように役立つか分かりませんが、コード例が更新されています。 – Vayrex

答えて

2

現在、TypeScriptには複数の継承またはミックスインを表す特別な構文はありませんが、公式の回避策はhereです。

基本的には、メインクラスにはのインターフェイスを実装し、簡単なダミー実装をインターフェイスに作成し、クラスを読み込んでメインクラスのプロパティを上書きする特殊な関数を用意することが戦略です。

mixins/traitsでTypeScriptを改善するための提案がいくつかあります。 #2919および#311

+0

ありがとうございます。しかし、私はその記事を読んだ後にここに来ました。私はコード例の更新を行いましたが、現在は主な質問が変更されています。 – Vayrex

3

Typescript 2.2 added support for mix-ins

+5

このユーザーはこの新しいmixin機能をどのように使用しますか? – Seanny123