ビルダーパターンは、主に「ステップで複雑なオブジェクトを構築する」ために使用されます。ここで、「ステップ」は安定したアルゴリズムですが、複雑なオブジェクトの一部はしばしば変更されます。したがって、ビルダーモデルは、主に「オブジェクトパーツ」のニーズの変更を解決するために使用されます。これにより、オブジェクト構築プロセスのより詳細な制御が可能になります。
パッケージcom.shejimoshi.create.builder;/***関数:複雑なオブジェクトの構築を表現から分離することを意図して、同じ構造プロセスが異なる表現を作成できるようにすることです*適用可能性:*複雑なオブジェクトを作成するためのアルゴリズムがオブジェクトのコンポーネントとは独立している必要がある場合、どの保護された文字列ツール2; public PersonBuilder(String T1、String T2){this.tool1 = t1; this.tool2 = t2; } //描画ヘッドpublic abstract void buildhead(); //体を描く公共の抽象void buildbody(); //左足と右足を描画しますパブリック抽象的なvoid buildarmleft();パブリックアブストラクトvoid buildarmright(); //左手と右手を描く公開抽象void buildlegleft();パブリックアブストラクトvoid buildlegright();}抽象的なジェネレーターは、対応するサブクラスを導き出して、対応する実際の使用タイプを構築します
パッケージcom.shejimoshi.create.builder;/***関数:複雑なオブジェクトの構築を表現から分離することを意図して、同じ構造プロセスが異なる表現を作成できるようにすることです*適用可能性:*複雑なオブジェクトを作成するためのアルゴリズムがオブジェクトのコンポーネントとは独立している必要があり、どのように編集されているか* T1、string t2){super(t1、t2); } @Override public void buildhead(){system.out.println( "draw a thin header"); } @Override public void buildbody(){system.out.println( "薄いボディを描く"); } @Override public void buildarmleft(){system.out.println( "薄い左腕を描く"); } @Override public void buildarmright(){system.out.println( "薄い左足を描く"); } @Override public void buildlegleft(){system.out.println( "薄い左脚を描く"); } @Override public void buildlegright(){system.out.println( "薄い男の右脚を描く"); }} package com.shejimoshi.create.Builder;/** * Function: The intention is to separate the construction of a complex object from its representation, so that the same construction process can create different representations* Applicability: * When the algorithm for creating complex objects should be independent of the components of the object and how they are assembled*/public class PersonFatBuilder extends PersonBuilder{ public PersonFatBuilder(String T1、string t2){super(t1、t2); } @Override public void buildhead(){system.out.println( "太った男の頭を描く"); } @Override public void buildbody(){system.out.println( "太った男の体を描く"); } @Override public void buildarmleft(){system.out.println( "太った男の左腕を描く"); } @Override public void buildarmright(){system.out.println( "太った男の右腕を描く"); } @Override public void buildlegleft(){system.out.println( "太った男の左足を描く"); } @Override public void buildlegright(){system.out.println( "太った男の右脚を描く"); }}私たちの発電機は、作成されたアクションを実装するために司令官に引き渡されます
パッケージcom.shejimoshi.create.builder;/*** function:キャラクターの司令官*/public class persondirector {private personbuilder pb; //対応する文字作成モデルpublic personDirector(personbuilder pber){this.pb = pber; } //人を作成しますpublic void createperson(){pb.buildhead(); pb.buildbody(); pb.buildarmleft(); pb.buildarmright(); pb.buildlegleft(); pb.buildlegright(); }}テストケース:
パッケージcom.shejimoshi.create.builder;/*** function:client program*/public class test {//対応する文字Public static void create(personbuilder pb){//パラメーターPersonDirector PD = New PersonDirector(PB)を通過したモデルで対応するオブジェクトを作成します。 pd.createperson(); } public static void main(string [] args){personthinbuilder ptb = new personthinbuilder( "Image Tool"、 "Brush"); create(ptb); //薄いものを作成しますSystem.out.println( "========================================================================================================================== =========================================================================================================================== =========================================================================================================================== =================================================================================================================================薄いシステムを作成します。out.out.println( "=============================================================================================================================== System.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out.out。実行結果:
薄い男の頭を描き、薄い体を描き、薄い男の体を描き、薄い男の左腕を描き、薄い男の右腕を描き、細い男の左足を描き、細い男の右脚を描き、 ============================================================================================================================================== ==============================================================================================================================================太った男の頭を描き、太った男の体を描き、太った男の左腕を描き、太った男の右脚を描く
上記はJava Builderモデルです。誰もがJavaプログラミングを学ぶことが役立つことを願っています。