Want to create interactive content? It’s easy in Genially!
Strategy - Design Pattern 3547
Ramyaa Prasath
Created on November 24, 2021
Start designing with a free template
Discover more than 1500 professional designs like these:
View
Audio tutorial
View
Pechakucha Presentation
View
Desktop Workspace
View
Decades Presentation
View
Psychology Presentation
View
Medical Dna Presentation
View
Geometric Project Presentation
Transcript
Strategy Design Pattern
MODULE 8 - RAMYAA.P 2019503547
Index
1. Design Pattern
- Types of Design Pattern
- About Design Pattern
2. Structural Design Pattern
3. When and how to use
4. Examples
5. Advantages and Disadvantages
Design Pattern
Definition
Design Pattern is divided into three main Categories
Creational Design Pattern
Design pattern is general reusable solution to commonly occuring problem within a given context in software design.Design pattern typically has 4 elements
- Pattern Name
- Problem
- Solution
- Consequence
Structural Design Pattern
Behavioral Design Pattern
Further Classification
Structural design patterns are about organizing different classes and objects to form larger structures and provide new functionality. They are classified into Adapter, Bridge, Composite, Decorator, Facade etc.
Behavioral patterns are about identifying common communication patterns between objects and realizing these patterns. These are further divided into Null Object, Observer, State, Strategy, Template method etc.
Creational design patterns are all about class instantiation or object creation. They are classified into Factory Method, Abstract Factory, Builder, Singleton, Object Pool, and Prototype.
StrategyDesign Pattern
There are common situations when classes differ only by behaviour.
A context object whose behavior varies as per its strategy object.
Strategy Pattern
This is a type of Behavioural Design Pattern. The role of this pattern is to define the family of algorithms, encapsulate these algorithms within class and make them interchangable
If algorithms are isolated in seperate classes, we can select which ones to run.
Create objects based on various strategies i.e algorithms.
Intended to define a family of algorithms as well as interchangable within.
Add algorithms without changing anything in other strategies.
When to use?
- Use the Strategy pattern when you want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime.
- Use the Strategy when you have a lot of similar classes that only differ in the way they execute some behavior.
- Use the pattern to isolate the business logic of a class from the implementation details of algorithms that may not be as important in the context of that logic.
- Use the pattern when your class has a massive conditional operator that switches between different variants of the same algorithm.
How to implement?
- In the context class, add a field for storing a reference to a strategy object. Provide a setter for replacing values of that field. The context should work with the strategy object only via the strategy interface. The context may define an interface which lets the strategy access its data.
- Clients of the context must associate it with a suitable strategy that matches the way they expect the context to perform its primary job.
- In the context class, identify an algorithm that’s prone to frequent changes. It may also be a massive conditional that selects and executes a variant of the same algorithm at runtime.
- Declare the strategy interface common to all variants of the algorithm.
- One by one, extract all algorithms into their own classes. They should all implement the strategy interface.
For example, Let's say you have an application that automatically finds routes roadways. What happens when you want to add many more ways like railways, airways or waterways? The technical part would cause you many headaches. Each time you added a new routing algorithm, the main class of the navigator doubled in size. At some point, the software becomes too hard to maintain. Solution?The Strategy pattern suggests that you take a class that does something specific in a lot of different ways and extract all of these algorithms into separate classes called strategies. In our navigation app, each routing algorithm can be extracted to its own class with a single buildRoute method. The method accepts an origin and destination and returns a collection of the route’s checkpoints.
Interface Realization / Realization is represented using a dotted line with a closed unshaded arrow mark.
Advantages of Strategy Design Pattern
Enable clients to choose required algorithms even inside objects
New algorithms with same interface are easily introduced
Encapsulation allows unaffected implementation
Alter behaviour without changing architecture
Effortlessly switch strategies during run-time
Disadvantages ofStrategy Pattern
- System or Client must be aware of all strategies.
- Need for two objects instead of one because the application must configure the context with strategy object.
- If you only have one more strategy to add, there is no need to overcomplicate the code with classes and interfaces.
Thank you!