2012-01-05 4 views
2

私は数時間は検索して読みましたが、まだ別のメソッドを選択できる新しいオブジェクトを作成するための基本的なデザインパターンを理解できません引数の1つに依存して設定された(同じ名前の)名前です。ここで私がしようとしていることを説明するコードがあります。 アドバイスと代替アプローチはすべて歓迎します。私は誰かが私をこの無知の雲を形作ってしまうことを許してくれることを願っています。使用してそれをあなたの方法を行うための おかげオブジェクト作成時の引数に基づいて、オブジェクトが持つメソッドのセットを選択する - JavaScript

function BaseConstructor(whichMethods) { 
    if (whichMethods==='a') { 
     // do something to incorporate methodSetA 
    } 
    else if (whichMethods==='b') { 
     // do something to incorporate methodSetB 
    } 

    this.init(); 
}; 

var methodSetA = { 
    init: function() { 
     // do initialisation A way 
    }, 
    speak: function() { 
     alert('i speak AAA way') 
    } 
}; 

var methodSetB = { 
    init: function() { 
     // do initialisation B way 
    }, 
    speak: function(){ 
     alert('i got BBB all the way') 
    } 
}; 

thing = new BaseConstructor('b'); 
// b is an instance of BaseConstructor and has done the bWay init() function 

thing.speak() // return alert 'i got BBB all the way' 

答えて

0

あなたはファクトリ関数(あなたのための適切なオブジェクトを作成し、定期的な機能)を使用して、このようにそれを行うことができます。

function BaseConstructor(whichMethods) { 
    var elem; 
    if (whichMethods==='a') { 
     elem = new MethodSetA(); 
    } else if (whichMethods==='b') { 
     elem = new MethodSetB(); 
    } else { 
     // figure out what to do here if whichMethods is neither of the previous options 
    } 

    elem.init(); 
    return(elem); 
}; 

を、通常の関数呼び出しとしてそれを呼び出す:

var thing = BaseConstructor('b'); 
thing.speak(); 

注:通常の関数呼び出しであるため、とBaseConstructor()の使用はありません。

+0

これは、すぐに返信いただきありがとうございます!工場のデザインパターン - 今日はその5回について読んでいるはずですが、それは私が必要とするものだと思っていましたが、いつも余計な内容でそんなに混乱しました。おかげで再び@ jfriend00 – johowie

0

まあ、「メソッドのセットを、」あなたはthisに(here's a demo)を反復してコピーすることができます。

個人的に
function copy(source, destination) { 
    for(var x in source) { 
     if(source.hasOwnProperty(x)) { 
      destination[x] = source[x]; 
     } 
    } 
} 

function BaseConstructor(whichMethods) { 
    if(whichMethods === 'a') { 
     copy(methodSetA, this); 
    } else if(whichMethods === 'b') { 
     copy(methodSetB, this); 
    } 

    this.init(); 
} 

、しかし、私は、割り当てることを好むだろう直接thisに直接送信してください。

+0

おかげで、私はあなたのコードから学びます。もう一度おねがいします@ minitech – johowie

0

あなたは工場パターンを探しています。 例:あなたのアドバイスとそれを行うための方法を見つける「私の方法」の

function objectFactory(whichMethods) { 
     if (whichMethods==='a') { 
      return new objectSetA(); 
     } 
     else if (whichMethods==='b') { 
      return new objectSetB() 
     } 
    }; 
    function objectSetA() { 
     this.init = function() { 
     // do initialisation A way 
     }, 
     this.speak = function() { 
      alert('i speak AAA way') 
     } 
    }; 

    function objectSetB() { 
     this.init = function() { 
     // do initialisation B way 
     }, 
     this.speak = function(){ 
      alert('i got BBB all the way') 
     } 
    }; 
    var thing = objectFactory('b'); 
    thing.speak(); 
+0

あなたの迅速かつ明確な返信をありがとう、本当に私は工場が必要です! @petar – johowie

+0

speakメソッドは、それぞれのコンストラクターのプロトタイプにある必要があります。 – RobG

+0

@RobGプロトタイプにspeak()メソッドを入れるのは意味があります。プロトタイプでもinit()メソッドを提案しない理由はありますか? – johowie

関連する問題