あーあ、俺には速度が足りねぇかなぁ!!
はいどうも、お久しぶりです。のろくです。
本日はリハビリーついでにFactoryパターンについて書きます。うろ解釈なので間違っている可能性が含まれます。知らない方は鵜呑みにしないように、知ってる方はつっこみお待ちしております。R.N.恋するうさぎちゃん。夏の太陽が俺を笑っているように見えるのは何故ですか。俺を。黒い太陽が俺を笑っているんですよ!!
はーい発狂しなーい。ちなみにFactoryパターンについて書くのは今回は嘘です。Simple Factoryしか書きません。
- 「もう誰もnewしたくない」
newしたくないですー。new書くとクラス名書かなくちゃいけなくてすげーうざい。
class HajimeSaito{ public battle(String range){ Gatotyu gatotyu; if(range.equals("密着")){ gatotyu = new GatotyuZeroStyle(); } else if(range.equals("対地")){ gatotyu = new GatotyuSecondStyle(); } else if(range.equals("対空")){ gatotyu = new GatotyuThirdStyle(); } else { // 通常 gatotyu = new GatotyuFirstStyle(); } gatotyu.attack(); } }
弐式と参式と零式は後から出てきたので、連載が進むにつれてHajimeSaitoクラスのコードは書き換えられているはず。HajimeSaitoクラスが各Gatoryu系クラスに依存してんのが良くない。具体的なクラス名を書くと技が増えたら増えただけelse文が増えたりする。HajimeSaito
class HajimeSaito{ public battle(String range){ Gatotyu gatotyu = new SimpleGatotyuFactory().createGatotyu(range); gatotyu.attack(); } } class SimpleGatotyuFactory{ public Gatotyu SimpleGatyotyuFactory(String range){ Gatotyu gatotyu; if(range.equals("密着")){ gatotyu = new GatotyuZeroStyle(); } else if(range.equals("対地")){ gatotyu = new GatotyuSecondStyle(); } else if(range.equals("対空")){ gatotyu = new GatotyuThirdStyle(); } else { // 通常 gatotyu = new GatotyuFirstStyle(); } return gatotyu; } }
続きはまた跡で書く。