依存性注入
Dec 29 2022
DI は、ソフトウェア プログラミングで広く使用されている手法です。Google も Android 開発者に DI を推奨していますが、Android にはこの目的のために Dagger や Hilt などのライブラリがあります。
DI は、ソフトウェア プログラミングで広く使用されている手法です。Google も Android 開発者に DI を推奨していますが、Android にはこの目的のために Dagger や Hilt などのライブラリがあります。DI には次の利点があります。
- コードの再利用性
- リファクタリングの容易さ
- テストの容易さ
依存性注入は、オブジェクトが依存するオブジェクトから分離できるようにする手法です。iOS で DI を実装する方法の例を見てみましょう。
class HTTPClient {
func downloadFrom(url: URL) {
//download data from remote
}
}
class DataDownload {
let client = HTTPClient()
func getData(url: URL) {
client.downloadFrom(url: url)
}
}
class client {
let dataDownload: DataDownload
init() {
self.dataDownload = DataDownload()
self.dataDownload.getData(url: URL(string: "https://any_http_url.com")!)
}
}
class FTPClient {
func downloadFrom(url: URL) {
//download data from remote
}
}
class DataDownload {
let client = FTPClient()
func getData(url: URL) {
client.downloadFrom(url: url)
}
}
class client {
let dataDownload: DataDownload
init() {
self.dataDownload = DataDownload()
self.dataDownload.getData(url: URL(string: "https://any_ftp_url.com")!)
}
}
protocol RemoteClient {
func downloadFrom(url: URL)
}
class FTPClient: RemoteClient {
func downloadFrom(url: URL) {
//download data from remote
}
}
class DataDownload {
let client: RemoteClient
init(client: RemoteClient) {
self.client = client
}
func getData(url: URL) {
client.downloadFrom(url: url)
}
}
class Client1 {
let dataDownload: DataDownload
init() {
self.dataDownload = DataDownload(client: FTPClient())
self.dataDownload.getData(url: URL(string: "https://any_ftp_url.com")!)
}
}
class Client2 {
let dataDownload: DataDownload
init() {
self.dataDownload = DataDownload(client: HTTPClient())
self.dataDownload.getData(url: URL(string: "https://any_htttp_url.com")!)
}
}
RemoteClientDataDownloadがコンポーザ/アセンブラから注入されClient1、Client2. また、コードを分離します。これで、不要な依存関係を満たすことなく、それらを個別にテストできます。上記のコード リファクタリングのメリット
- テストの容易さ。
- 新機能の迅速な開発。
- Xcode のコンパイル時間の改善 (これについては別のブログを書きます)。
- 時間とお金を節約します。
- コンストラクター注入 (上記で説明)
- 関数注入
- プロパティ注入
class ChildViewController: UIViewController {
func viewDidLoad() { }
func viewWillAppear() { }
func viewDidAppear() { }
func getDataFrom(_ client: RemoteClient, url: URL) {
client.downloadFrom(url: url)
}
}
class ParentViewController: UIViewController {
func viewDidLoad() { }
func viewWillAppear() { }
func viewDidAppear() { }
func present() {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let vc = storyboard.instantiateViewController(withIdentifier: "ChildViewController") as! ChildViewController
vc.getDataFrom(client: HTTPClient(), url: URL(string:"https://any-url.com")!)
}
}
プロパティ注入: HTTPClient は、プロパティ注入として に渡されvar clientます。
class ChildViewController: UIViewController {
var client: RemoteClient?
func viewDidLoad() { }
func viewWillAppear() { }
func viewDidAppear() { }
func getDataFrom(url: URL) {
client.downloadFrom(url: url)
}
}
class ParentViewController: UIViewController {
func viewDidLoad() { }
func viewWillAppear() { }
func viewDidAppear() { }
func present() {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let vc = storyboard.instantiateViewController(withIdentifier: "ChildViewController") as! ChildViewController
vc.client = HTTPClient()
}
}
それが役に立てば幸い!この記事が気に入って、さらにすばらしい記事を読みたい場合は、ここで私をフォローしてサポートを示してください。
コンテンツを改善するため、および次に探求したいトピックについて、フィードバックを共有してください。
読んでくれてありがとう、幸せなコーディング!!
参考文献
- https://essentialdeveloper.com
- https://developer.android.com/training/dependency-injection

![とにかく、リンクリストとは何ですか?[パート1]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































