Strategy Pattern
Strategy Pattern
I. Overview
1. Definition:
- Defines a family of algorithms, encapsulates each one,
and makes them interchangeable. Strategy lets the
algorithm vary independently from clients that use it.
- Capture the abstraction in an interface, bury
implementation details in derived classes.
2. Principles in the pattern:
- Principle 1: Encapsulate what varies (identify the aspects
of the application that vary and separate them from
what stays the same).
- Principle 2: Program to interfaces, not implementations
(by “interface”, this principle isn’t limiting itself to
Java interfaces, it also covers supertypes, preferably
abstract classes or interfaces, both of which refer to
classes with undefined behaviors).
- Principle 3: Favor composition over inheritance.
3. Pros and Cons:
- Advantages:
Using encapsulation the algorithm separately, new
algorithms complying with the same interface can be
easily introduced.
The strategy pattern application can switch
strategies at run-time.
Strategy enables the clients to choose the required
algorithm, without using a "switch" statement or a
series of "if-else" statements.
Data structures used for implementing the algorithm
is completely encapsulated in Strategy classes.
Therefore, the implementation of an algorithm can
be changed without affecting the Context class.
The same Strategy object can be strategically shared
between different Context objects. However, the
shared Strategy object should not maintain states
across invocations.
- Disadvantages:
The application must be aware of all the strategies
to select the right one for the right situation.
Strategy and Context classes may be tightly coupled.
The Context must supply the relevant data to the
Strategy for implementing the algorithm and
sometimes, all the data passed by the Context may
not be relevant to all the Concrete Strategies.
The application configures the Context with the
required Strategy object. Therefore, the application
needs to create and maintain two objects in place of
one.
Since, the Strategy object is created by the
application in most cases; the Context has no control
on lifetime of the Strategy object. However, the
Context can make a local copy of the Strategy object.
But, this increases the memory requirement and has
a sure performance impact.
And:
And:
- Step 7: Finally, the Application class is created to test all
the code above :
And there are the results for each keyboarInput:
credit:
Payed 350 cents using credit card[name = John Doe, number = 4111111111111111, CVV = 123,
expiration = 01/22]
debit:
Payed 350 cents using debit card[name = John Doe, number = 4111111111111111, CVV = 123, e
xpiration = 01/22]
cash:
Payed 350 cents using cash
IV. Conclusion
- To wrap up, The Strategy Pattern is considered as one of
the most important design pattern due to its huge
benefits l in solving different problems in coding such as:
sorting, validation, outputing etc. Therefore, it is
necessary for every programmer to comprehend and use
the pattern expertly and thereby can improve their
coding capability.