The policy model (strategy pattern) embodies two very basic principles of object-oriented design
– Concept of package change
– Interfaces are used in programming, not implementations of interfaces
Definition of the policy pattern
– Define a set of algorithms that encapsulate each algorithm and make them interchangeable.
– The policy mode enables these algorithms to change independently when the client calls them.
The significance of the strategy model
– The policy pattern enables developers to develop software that consists of a number of replaceable parts, and the relationships between the parts are weakly connected.
– The weak connection feature makes the software more scalable, easy to maintain, and more importantly, it greatly improves the reusability of the software.
Composition of the policy model
– Abstract policy role: A policy class, usually implemented by an interface or abstract class
– Specific policy roles: wrapping up related algorithms and behaviors
– Environment role: holds a reference to a policy class that is eventually called to the client.
Implementation of the policy pattern
– The policy pattern is intended to be a set of algorithms that encapsulate each algorithm in a separate class with a common interface so that they can be replaced with each other.
– The policy mode allows the algorithm to change without affecting the client. Using the policy model, you can separate behavior from the environment.
– The Environment class is responsible for maintaining and querying the behavior classes, and the various algorithms are provided in the specific strategy. Because the algorithm and the environment are independent, the modification of the algorithm will not affect the environment and the client.
How to write the policy mode
–1 Defines a public interface for a policy object. (strategy)
–2 Write a policy class that implements the public interface above. (Addstrategy,subtractstrategy,multiplystrategy,dividestrategy)
–3 A reference to a policy object is saved in the class that uses the policy object. (Environment)
–4. In the class that uses the policy object, implement the Set and Get methods (injections) for the policy object or use the construction method to complete the assignment.
Policy Mode Implementation subtraction:
Package Stratery; Public InterfaceStrategy { Public intCalulate (intAintb);} Package stratery; Public classAddstrategy implements strategy{ Public intCalulate (intAintb) { returnA +b; }} package stratery; Public classSubtractstrategy implements strategy { Public intCalulate (intAintb) { returnA-b; }}package Stratery; Public classMultiplystrategy implements strategy { Public intCalulate (intAintb) { returnA *b; }}package Stratery; Public classDividestrategy implements strategy { Public intCalulate (intAintb) { returnA/b; }}package Stratery; Public classEnvironment {Privatestrategy strategy; PublicEnvironment (Strategy strategy) {//TODO Auto-generated constructor stub This. strategy=strategy; } PublicStrategy Getstrategy () {returnstrategy; } Public voidSetstrategy (Strategy strategy) { This. Strategy =strategy; } Public intCalulate (intAintb) { returnStrategy.calulate (A, b); }}package Stratery; Public classClient { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsAddstrategy Addstrategy =NewAddstrategy (); Environment Environment=NewEnvironment (ADDSTRATEGY); System. out. println (Environment.calulate (3,4)); Subtractstrategy Subtractstrategy=NewSubtractstrategy (); Environment.setstrategy (Subtractstrategy); System. out. println (Environment.calulate (3,4)); Multiplystrategy Multiplystrategy=NewMultiplystrategy (); Environment.setstrategy (Multiplystrategy); System. out. println (Environment.calulate (3,4)); Dividestrategy Dividestrategy=NewDividestrategy (); Environment.setstrategy (Dividestrategy); System. out. println (Environment.calulate (3,4)); }}
Policy mode (strategy)