[Design Patterns] 4. Creation Pattern
2635 ワード
設計モードは繰り返し使用され、多くの人が知っている、分類編目された、コード設計経験の総括であり、設計モードを使用する目的はコードの再利用性を高め、コードを他人に理解されやすくし、コードの信頼性を保証することである.これはコード作成が本当にエンジニアリング化されています.
4つのキー要素:(1)Pattern Name,(2)Problem,(3)Solution,(4)Consequences.
01. Factory Method Pattern
/* The product should be created by his own factory. */
タスクの割り当て:
アーキテクチャ1:factory、productインタフェースを定義します.
プログラマー1:FileLogFactoryクラス、FileProductクラスを完了します.
質問:製品のみを定義するのはどうですか.
答え:factoryクラスには、システムバージョン、環境などProductで処理するのに適していない情報を追加できます.
02. Abstract Factory Pattern
1つの工場は一般的に関連性のある製品、すなわち製品ファミリーを生産します.
異なる工場(大環境下)で同じ一連の製品ファミリーを生産することができ、類似の製品等級構造を有する.
タスクの割り当て:
アーキテクチャ1:factory、productインタフェースを定義します.
プログラマ1:OracleFactoryクラス、connectionクラス、statementクラスを完了します.
03. Builder Pattern
抽象工場モデルが自動車部品生産工場である場合、構築者モデルは自動車組立工場であり、部品の組立を通じて完全な自動車に戻る.
コンストラクタモードは、複雑なオブジェクトのコンストラクションとオブジェクトの表現を分離し、同じコンストラクションプロセスで異なる表現オブジェクトを作成できます.
4つのキー要素:(1)Pattern Name,(2)Problem,(3)Solution,(4)Consequences.
01. Factory Method Pattern
/* The product should be created by his own factory. */
LogFactory factory = new FileLogFactory();
Log log = factory.createLog();
log.writeLog();
タスクの割り当て:
アーキテクチャ1:factory、productインタフェースを定義します.
プログラマー1:FileLogFactoryクラス、FileProductクラスを完了します.
質問:製品のみを定義するのはどうですか.
答え:factoryクラスには、システムバージョン、環境などProductで処理するのに適していない情報を追加できます.
02. Abstract Factory Pattern
1つの工場は一般的に関連性のある製品、すなわち製品ファミリーを生産します.
異なる工場(大環境下)で同じ一連の製品ファミリーを生産することができ、類似の製品等級構造を有する.
DBFactory factory= new OracleFactory(); // ; , 。
Connection connection = factory.createConnection():
Statement statement = factory.createStatement();
connection.connect();
statement.executeStatement();
タスクの割り当て:
アーキテクチャ1:factory、productインタフェースを定義します.
プログラマ1:OracleFactoryクラス、connectionクラス、statementクラスを完了します.
03. Builder Pattern
抽象工場モデルが自動車部品生産工場である場合、構築者モデルは自動車組立工場であり、部品の組立を通じて完全な自動車に戻る.
コンストラクタモードは、複雑なオブジェクトのコンストラクションとオブジェクトの表現を分離し、同じコンストラクションプロセスで異なる表現オブジェクトを作成できます.
ActorController ac = new ActorController();
ActorBuilder ab = new AngelBuilder();
Actor angel = ac.construct(ab); // builder/ /
String type = angel.getType();
System.out.println(type + " :");
System.out.println(" :" + angel.getSex());
System.out.println(" :" + angel.getFace());
System.out.println(" :" + angel.getCostume());
System.out.println(" :" + angel.getHairstyle());