Design Patterns Strategy Pattern - Design Patterns

What is Design Patterns Strategy Pattern?

In Strategy pattern, category behaviour or its algorithm can be transformed at run time. This form of design pattern comes under behaviour model.

In Strategy pattern, we create objects which represent various strategies and a context object whose behaviour varies as per its strategy object. The strategy object changes the executing algorithm of the context object.

Implementation

We are available to create a Strategy interface important an action and real strategy classes implementing the Strategy boundary. Context is a class which uses a Strategy.

StrategyPatternDemo, our demo class, will use Context and strategy objects to display change in Context performance based on strategy it deploys or uses.
strategy_pattern_uml_diagram

Step 1

Create an interface.

Strategy.java

Step 2

Create concrete classes implementing the same interface.

OperationAdd.java

OperationSubstract.java

OperationMultiply.java

Step 3

Create Context Class.

Context.java

Step 4

Use the Context to observe transform in behaviour when it changes its Strategy.

StrategyPatternDemo.java

Step 5

Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Design Patterns Topics