Se202 Lecture 6
Se202 Lecture 6
AND ARCHITECTURE
LECTURE 6
Strategy Design Pattern
Concept
Suppose there is an application where you have multiple algorithms and each of
these algorithms can perform a specific task. A client can dynamically pick any of
these algorithms to serve its current need.
The strategy pattern suggests that you implement these algorithms in separate
classes. When you encapsulate an algorithm in a separate class, you call it a
strategy. An object that uses the strategy object is often referred to as a context
object. These “algorithms” are also called behaviors in some applications.
http://sce2.umkc.edu/csee/leeyu/class/CS590L-03/Presentation/Pattern/Strategy.ppt
Strategy Design Pattern
• Strategy
declares an interface common to all supported algorithms.
Context uses its interface to call the algorithm defined by a ConcreteStrategy.
• ConcreteStrategy
implements a specific algorithm using the Strategy interface.
• Context
is configured with a ConcreteStrategy object.
maintains a reference to a Strategy object.
may define an interface for Strategy to use to access its
Now I want that client should be able to tell the name of friend and desired
platform – then my application should connect to him transparently.
More importantly, if I want to add more social platforms into application then
application code should accommodate it without breaking the design.
https://howtodoinjava.com/design-patterns/behavioral/strategy-design-pattern/
Solution: Strategy Design Pattern
Test Program:
https://howtodoinjava.com/design-patterns/behavioral/strategy-design-pattern/
https://howtodoinjava.com/design-patterns/behavioral/strategy-design-pattern/
https://howtodoinjava.com/design-patterns/behavioral/strategy-design-pattern/
Example
The class diagram is an example of strategy design pattern but in the
diagram data fields and methods of «Context» class is missing. Complete
the missing parts and implement the following class hierarchy.
Demo Program:
Do following operations
// 10+5
// 10-5
// 10*5
https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm
Q: The class diagram is an example of strategy
design pattern but in the diagram data fields
and methods of «Context» class is missing.
Complete the missing parts and implement the
Context class.
https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm
https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm
Example
The example will be based on
football.
Let’s imagine that any football team
can play in two manners: attacking
and defending.
These two tactics are particular
realisations of a football strategy.
https://www.javacodegeeks.com/2013/06/design-patterns-strategy.html
public class TacticContext { public class Test3 {
private FootballStrategy strategy = null; public static void main(String[] args) {
public void selectTactic(String team) {
strategy.adhereTactic(team); String team1 = "Barcelona";
} String team2 = "Real Madrid";
Though these options look straightforward, applying the Strategy pattern results in a more elegant and efficient design.
Applying the Strategy pattern, each of the encryption algorithms can be encapsulated in a separate (strategy) class. Table 36.4 shows the
list of these strategy classes and the algorithms they implement.
Let us define a common interface to be implemented by each of the strategy classes, in the form of a Java interface EncryptionStrategy, as
follows:
Only the last
String(char[] value)
Allocates a new String so that it represents
the sequence of characters currently
contained in the character array argument.
The HashMap class has many useful methods. For example,
To add items to it, use the put() method.
To access a value in the HashMap, use the get() method and refer to its
key
Ex: capitalCities.get("England");
When the EncryptLogger is first instantiated, its current
encryption strategy is set to SimpleEncryption inside its
constructor.
Note: The EncryptLogger contains an object reference of FileLogger type. This relationship is not included in the class diagram as it is
not part of the pattern implementation.
References
Java Design Patterns, A Hands-On Experience with Real-World Examples, Vaskaran Sarcar
Software Architecture Design Patterns in Java, Partha Kuchana
Additional Material
Class StringTokenizer
The string tokenizer class allows an application to break a string into tokens.
The following is one example of the use of the tokenizer. The code: