Appearance
Strategy Design Pattern
The Strategy Pattern also known as the Policy Pattern is a behavioural software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.
Category
PlatformHub Integration
Scope - Problem
As per decoupled architecture we are making independent components/journeys and their relation is going to build at run time with the system. The system doesn't know about that plugin, how it communicates, what are the dependencies with core, so is PlatformHub's requirement is to provide instructions/algorithm set to core so that core could execute the required instruction set and establish the communication.
Scope - Pattern Context
Strategy Pattern defines a family of functionality/strategies, encapsulate each one, and make them interchangeable. Code receives run-time instructions as to which in a family of algorithms to use.
When to Use This Pattern
The demand of decoupled architecture is to make independent module, it should be plug and play. Things should work automatically after plugging the module to the core. The plugin should follow the docking protocols defied by the hub for various functionalities, here the hub is expecting the instruction set of communication, dependencies and authentication from each plugin to establish the communication at run time.
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. The original class, called context, must have a field for storing a reference to one of the strategies. The context delegates the work to a linked strategy object instead of executing it on its own. The context isn’t responsible for selecting an appropriate algorithm for the job. Instead, the client passes the desired strategy to the context. In fact, the context doesn’t know much about strategies. It works with all strategies through the same generic interface, which only exposes a single method for triggering the algorithm encapsulated within the selected strategy.
This way the context becomes independent of concrete strategies, so you can add new algorithms or modify existing ones without changing the code of the context or other strategies. Hence we have to use this pattern to pass the docking instructions to the hub at run time.
Design and Implementation
High Level View

Reference Class Structure
Policy interface facilities the required instruction set to the Microkernel to start the communication at run time.

Code
Policy Interface
java
interface Policy {
val name: String
val version: String
val event: PolicyEvent
val dependencies: List<String>
}Policy Data Structure
java
{
"name": "<Plugin Name>",
"version": "<Plugin Version>",
"event": {},
"dependencies": ["<Plugin A>", "<Plugin B>"]
}Issues and Considerations
- Need to know, when to use this pattern, when an application is aware of all the strategies to select the right one for the right situation.
- Extra objects creation, in most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.
- Strategy awareness among clients. Difference between the strategies should be clear between the clients to able to select a best one for them.
- When we have only a few algorithms to implement, then its waste of resources to implement the Strategy method.
- Synchronisation, any changes in any strategy may impact all clients, simultaneous updates would be needed for all clients.
- It requires an implementation of additional classes in the project, but that’s worth paying for it.
Forces and Constraints
- Can't get strategy/function object directly, need to use reflection pattern for casting from string to object.
- It is a bit difficult to find out function references or classes in different namespaces/packages. Finding string references is bit more costly than finding object references, so use some good tools that will help to find all references.
Read More Patterns