1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DIについて知りたくなった

Last updated at Posted at 2023-10-25

はじめに

Dependency Injection(依存性の注入)、通称DIは、オブジェクト指向プログラミングにおける設計原則の1つであり、特に大規模なアプリケーションやモジュール間の疎結合を実現するための手法。

DIとは?

DIは、オブジェクトの依存関係を外部から注入する手法です。これにより、以下のような利点が得られます。

  • 疎結合: クラス間の依存関係が少なく、コンポーネントの再利用や交換が容易になります。
  • テスト容易性: テスト時に実際の依存をモックオブジェクトに置き換えることが容易になります。
  • コードの可読性向上: 依存関係が明確になるため、コードの理解が容易になります。

DIの動作原理

通常、オブジェクトは自身の依存関係を内部でインスタンス化します。しかし、DIを使用すると、依存関係は外部から注入され、オブジェクトのライフサイクルとは独立して管理されるようになります。

# 通常の方法
class UserService:
    def __init__(self):
        self.repository = UserRepository()

# DIを使用した方法
class UserService:
    def __init__(self, repository):
        self.repository = repository
  • コンストラクタ注入
    依存関係をコンポーネントのコンストラクタを通じて注入します。
class ServiceA:
    pass

class Client:
    def __init__(self, service):
        self.service = service

client = Client(ServiceA())
  • プロパティ/セッター注入
    依存関係をコンポーネントのプロパティやセッターメソッドを通じて注入します。
class Client:
    @property
    def service(self):
        return self._service

    @service.setter
    def service(self, service):
        self._service = service

client = Client()
client.service = ServiceA()
  • メソッド注入
    依存関係をメソッドの引数として注入します。
class Client:
    def execute(self, service):
        service.do_something()

client = Client()
client.execute(ServiceA())

PythonでのDI

PythonでDIを実践する方法としては、コンストラクタインジェクションセッターインジェクション、またはDIフレームワークを使用する方法などが考えられます。

以下は、コンストラクタインジェクションを用いた例です。

class UserRepository:
    def get_user(self, user_id):
        # データベースからユーザーを取得
        pass

class UserService:
    def __init__(self, repository: UserRepository):
        self.repository = repository

    def get_user_details(self, user_id):
        return self.repository.get_user(user_id)

具体的な使用例

  1. Webアプリケーション
    例: ユーザー認証サービス、商品管理システム、CMS等
    DIを使用することで、データベース接続、外部APIとの通信、認証ロジックなどの異なる部分を柔軟に切り替えることが容易になります。

  2. モバイルアプリケーション
    例: 通知サービス、位置情報サービス等
    異なるデバイスやOSバージョンで動作する機能の実装をスムーズに行えます。

  3. マイクロサービスアーキテクチャ
    例: 顧客サービス、在庫管理サービス、注文サービス等
    サービス間の依存を疎結合に保ち、サービスの単体テストや統合テストを容易に行うことができます。

  4. ゲーム開発
    例: AIの動作、物理エンジン、グラフィックレンダリング等
    異なるゲームエンジンやレンダリング技術の切り替え、AIの振る舞いの変更などが簡単に行えます。

  5. IoT
    例: センサーデータの収集、デバイスのリモート制御等
    異なるハードウェアやプラットフォームに対して、共通のロジックを適用する際に便利です。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?