Route planning for two points on a map is a typical strategy mode application scenario. When we do a start to finish route planning, we expect the map to give us the best route for these modes: walking. Public transit, driving. Sometimes it may be subdivided into several strategies such as transit (rail priority), bus (transfer priority), etc.
Following our construction convention, here is a framework code for path planning
In addition to writing it like the test code above, we can also invoke the factory pattern to create all instances of the router and enumerate all routers to get all path plans at once. This iterative approach is also a real-life engineering solution, as it is always used in mapping software to manage all possible routers.
Based on the above example, we can reorganize a number of key points of the strategy pattern.
- the strategy pattern abstracts a public interface for accomplishing a specific task from a bunch of methods, provides a manager based on that public interface, and a number of strategies.
- each strategy represents a different algorithm for achieving a particular task.
- the manager does not care what is special about the specific policy used, as long as it supports the public policy computing interface.
- The manager is responsible for providing a contextual environment to invoke the policy calculator.
- The contextual environment can bring a different computational environment to the policy calculator.
- the policy calculator extracts the parameters of interest from the contextual environment according to the needs of its algorithm in order to complete the computation
- The result of the computation is abstracted into a common class form.
- The policy calculator can derive its own special implementation based on the public result class, but in order for the manager to extract the result, a public interface for extracting the result is agreed upon.
The example code provides an intermediate layer of the template class
router_t<T>, and it is at this location that we intend to introduce a factory creation and registration mechanism for routers, so that we can collect all unique instances of routers in a single manager and use them later in router_guide.
Policy-oriented programming, which has some similarities and differences with the policy pattern.
For example, to select different pen types of writers.
This is an example of a policy pattern implemented with Policy-oriented programming techniques. It has the following subtleties that are worth noting: 1.
- there is no public base class for strategy unlike the base class for router in the previous section to provide an interface for policy operations, the metaprogramming world can do interface coupling directly with the help of SFINAE techniques.
- Because of the compile-time unfolding feature of the template, it becomes less feasible to switch strategies dynamically at runtime. In order to do dynamic switching at runtime, you may need to provide several writers with additional code, which are expanded for different pen types, for use at runtime.
Possible appropriate sites
As commonly understood, you might think that the former approach is the standard strategy pattern. And, how can there be any scenario where I need to choose a strategy to be solidified at compile time?
Well, there is.
The pen type was chosen above as an example just to demonstrate the code writing approach in a more concise way (and it was the case in our previous article), but it really is not the best example of compile-time strategy selection.
But imagine a situation where you are a class library author and are providing a general-purpose socket communication library. Then you can provide two policy classes for blocking and non-blocking, and implement them separately. And the user can choose the most appropriate policy for his communication scenario when using your socket library.
In this configuration, the policy mode is also implemented by selecting the communication mode as blocking or non-blocking, but it is the compile-time choice that is appropriate.
Similarly, you can choose whether to use TCP or UDP communication, which algorithm to use for encoding and decoding datagrams, and so on when using this communication library.
Two typical implementation methods are provided in the paper, representing compile-time and run-time deployment schemes for the policy pattern.
Depending on the actual scenario you can refer to and pick one.
The engineering application of policy patterns can take many other forms, and does not even have to be limited to the coding, language-specific coding level. For example, we can also provide binary-level policy options by way of plug-in structures.