1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【デザインパターン】ストラテジーパターン解説(Flutter / Android 実例付き)

Last updated at Posted at 2025-09-03

1. パターンの意図

ストラテジー(Strategy)パターン は、
アルゴリズムをカプセル化し、実行時に差し替え可能にする デザインパターンです。

解決する問題

  • 複数のアルゴリズム(処理手順)を切り替えたい
  • if/else や switch 文が増えすぎて管理が大変
  • アルゴリズムをクラスとして独立させ、疎結合で差し替えたい

ポイント

  • Strategy:アルゴリズムのインターフェース
  • ConcreteStrategy:具体的なアルゴリズム実装
  • Context:Strategy を保持し、必要に応じて切り替える

2. UML 図


3. Flutter / Dart 実装例

Strategy

abstract class PaymentStrategy {
  void pay(int amount);
}

ConcreteStrategy

class CreditCardPayment implements PaymentStrategy {
  @override
  void pay(int amount) {
    print("Paid $amount with Credit Card");
  }
}

class PaypalPayment implements PaymentStrategy {
  @override
  void pay(int amount) {
    print("Paid $amount with PayPal");
  }
}

Context

class ShoppingCart {
  PaymentStrategy? _strategy;

  void setPaymentStrategy(PaymentStrategy strategy) {
    _strategy = strategy;
  }

  void checkout(int amount) {
    _strategy?.pay(amount);
  }
}

利用例

void main() {
  var cart = ShoppingCart();

  cart.setPaymentStrategy(CreditCardPayment());
  cart.checkout(100);

  cart.setPaymentStrategy(PaypalPayment());
  cart.checkout(200);
}

出力:

Paid 100 with Credit Card
Paid 200 with PayPal

4. Android / Kotlin 実装例

Strategy

interface PaymentStrategy {
    fun pay(amount: Int)
}

ConcreteStrategy

class CreditCardPayment : PaymentStrategy {
    override fun pay(amount: Int) {
        println("Paid $amount with Credit Card")
    }
}

class PaypalPayment : PaymentStrategy {
    override fun pay(amount: Int) {
        println("Paid $amount with PayPal")
    }
}

Context

class ShoppingCart {
    private var strategy: PaymentStrategy? = null

    fun setPaymentStrategy(strategy: PaymentStrategy) {
        this.strategy = strategy
    }

    fun checkout(amount: Int) {
        strategy?.pay(amount)
    }
}

利用例

fun main() {
    val cart = ShoppingCart()

    cart.setPaymentStrategy(CreditCardPayment())
    cart.checkout(100)

    cart.setPaymentStrategy(PaypalPayment())
    cart.checkout(200)
}

5. 実務ユースケース

Flutter

  • バリデーションルールを切り替える(入力チェック)
  • 画面のテーマ戦略(ダーク/ライト、地域ごとのフォーマット処理)
  • 決済方式(クレカ/PayPal/ポイント)

Android / Kotlin

  • データ保存方式(DB / ネットワーク / メモリキャッシュの切替)
  • 画像ロード戦略(Glide / Picasso / Cache)
  • アルゴリズム切替(ソート、検索)

6. メリット / デメリット

メリット

  • if/else や switch 文を排除し、スッキリした設計
  • アルゴリズムを差し替え可能、拡張性が高い
  • OCP(開放閉鎖原則)に沿った設計ができる

デメリット

  • Strategy クラスが増える
  • 小規模処理にはオーバーエンジニアリングになる場合もある

まとめ

  • Strategy パターンは「アルゴリズムをクラスとして独立・差し替え可能にする」仕組み
  • Flutter/Android ではバリデーション、決済、データ処理などに使いやすい
  • Decorator = 足し算で拡張、Strategy = 引き換えで置換 という違いを押さえると混同しにくい

1
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?