Strategy design pattern
Image credits:https://www.smartva.net/

Strategy design pattern

What is Strategy design pattern?

  • It is behavioural design pattern. This pattern is also referred as Policy pattern.
  • This pattern lets us define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.
  • This pattern is useful to when there are different algorithms to choose at runtime depending upon user selection/context.
  • Same behaviour(end result) however different ways to achieve it.

Real world analogy:

  • Consider example of project management.
  • To achieve this, there are different strategies/methodologies to choose from. However, main behaviour/end goal is better project management.
  • There could be various strategies like Scrum, Kanban, Lean,Waterfall and many more.

Example in software/apps development:

  • Consider scenario of Payment processing within mobile application.
  • We can infer that there is one behaviour payment that needs to be achieved. However, there are multiple ways/payment mechanisms to complete payment.
  • For example, payment can be done using CreditCard , DebitCard or any other mobile wallet APIs. Ultimate goal is to pay specific amount in a specified currency.
  • Sample code in swift: https://github.com/ParthContractor/StrategyDesignPattern

Benefits:

  • More isolated, understandable and readable code.
  • Adherence to Open/Closed principle.
  • Changing algorithms/strategies at runtime is easy.

Drawbacks:

  • There could be possibility of increased distinct strategies over the period of time and apparently cumbersome to handle/manage them all.
  • Clients must understand all strategies to properly utilised one over another at runtime.

Comparison with Template method pattern:

  • Template method depends upon single algorithm in abstract super class and allowing concrete subclasses to override part of steps.
  • However, Strategy design pattern enables selection of algorithm at runtime. Different strategy is applied to object at runtime to achieve its specific behaviour.

Other interesting articles/reference materials:

Summary:

Strategy pattern is useful when you want to decide during run time how you want to run a particular behaviour from set predefined ways.

要查看或添加评论,请登录

Parth C.的更多文章

  • One year in Australia ????

    One year in Australia ????

    Today marks one year since my arrival in this beautiful country. 10 key learnings since this new journey.

    2 条评论
  • SOLID Principles — Part3

    SOLID Principles — Part3

    Build SOLID foundation for your software; write code using SOLID principles. Liskov Substitution Principle: LSP…

  • Chain of Responsibility pattern

    Chain of Responsibility pattern

    What is COR pattern? It is behavioural design pattern. In this pattern there is a chain of event handlers.

  • Template Method Pattern

    Template Method Pattern

    What is Template Method? It is behavioural design pattern. It defines the skeleton of an algorithm in abstract…

  • Top 20 iOS Tech Lead Essentials

    Top 20 iOS Tech Lead Essentials

    For a successful iOS product/application development, there must be 360 degree thinking and a checklist to adhere. iOS…

    2 条评论
  • DTO - Data Transfer Objects

    DTO - Data Transfer Objects

    Give Them What They Ask For..

  • SOLID Principles - Part2

    SOLID Principles - Part2

    Before designing a new module, adding features to existing module and while writing the actual code, think through…

  • SOLID Principles - Part1

    SOLID Principles - Part1

    Solid knowledge of SOLID principles is the key to write a great software/program. Single Responsibility Principle: In…

    1 条评论
  • Builder Design Pattern

    Builder Design Pattern

    What is Builder Design Pattern? It is a creational design pattern. Actually, this pattern was introduced to solve some…

  • Abstract Factory Pattern

    Abstract Factory Pattern

    What is abstract factory design pattern? It is a creational design pattern. This pattern provides a way to encapsulate…

社区洞察

其他会员也浏览了