The document outlines various Git commands and their usages, including initialization, adding files, committing changes, and managing branches. Key actions include committing with messages in Japanese, removing files, resetting changes, and checking out branches. It also describes the process of cloning repositories, fetching and pulling updates, and handling merges and rebases.
The document outlines various Git commands and their usages, including initialization, adding files, committing changes, and managing branches. Key actions include committing with messages in Japanese, removing files, resetting changes, and checking out branches. It also describes the process of cloning repositories, fetching and pulling updates, and handling merges and rebases.
12. 課題2:グローバルにオブジェクトを共有 システムの中では使うたびに new するのではなく、特定のオブジェクトをグローバルに共有したいことが多い 共有化したいオブジェクトの例 毎回生成するのが重たいリソースオブジェクト DB やメッセージキューに対する接続先 O/R マップ FW のコンテキストファクトリ リモートサービスに対する参照 ログ出力先 共通情報 システム設定プロパティ エラーメッセージ文字列リソース システムステータス
13. 伝統的な解決策: 独自 Singleton パターンの利用 グローバル情報のアクセスのために、独自に Singleton デザインパターンや Service Locator パターンを利用する public class AppConfig { private static final INSTANCE= new AppConfig(); public static AppConfig getInstance() { return INSTANCE; } // コンストラクタを隠蔽 private AppConfig () {} … } public class SomeServiceImpl { public void userAppConfig() { int val = AppConfig.getInstance() .getSomeProperty(); } } Static メソッド経由で、特定のインスタンスを共有
18. IF のみに依存するコードを書くことは実は結構難しい 単にインタフェース型を定義して、 implements すればよいわけでない! インタフェースに対して new を呼び出すことはできないため、結局 new をハードコードしたら特定の具象サブクラスに依存してしまう public class CustomerService { private CustomerDao customerDao = new JpaCustomerDao(); } 直接 new したら、結局インタフェース型のフィールドを宣言している意味がなくなる!!
19. 課題 1 ~ 3 すべてに対する解決策? Dependency Injection Dependency Injection( 依存性の注入) 従来はアプリケーションごとに独自のファクトリやシングルトンを駆使して設計していたため、正しく設計するためには相当敷居が高かった 一方、 DI では DI コンテナフレームワークが汎用のファクトリとして機能する DI によりオブジェクトの生成や依存関係の設定が自動化されるため、プログラム中では生成済みのオブジェクトを使えばよい 結果として、インタフェースのみに依存するプログラムを作成することが非常に簡単になる
20. 伝統的な setter インジェクション Spring FW では伝統的に setter メソッドを使って依存オブジェクトを設定する public class CustomerService { private CustomerDao customerDao; public void setCustomerDao( CustomerDao customerDao) { this.customerDao = customerDao; } } <bean id=“customerService" class=“…CustomerImpl”> <property name=“customerDao“ ref=“customerDao”/> </bean> XML ファイル中でセッターに対応するプロパティに依存関係をインジェクションする
21. Java コードが XML に移動しただけでは? オブジェクト生成、関連付けの Java コードは不要になった一方で、 XML の設定ファイルに生成に必要なメタ情報を記述する必要がある 確かに XML を含めた全体のコード量は変わらないが、以下の点で多大なメリットがある サービスや接続ファクトリなどのオブジェクトを簡単に共有できる インタフェースのみに依存するようにすることが簡単に実現できる 通常はサービスや DAO など粒度の大きなコンポーネントのみを DI 管理する(newの使用をすべて禁止するわけではない!)