Java Policy mode
@author Ixenos
Defined
1. Encapsulation algorithm: Define a set of algorithms that encapsulate each algorithm and enable them to be interchangeable
2. Segmentation Behavior and Environment: the internal implementation of the user screen, so that the client in the call algorithm can not affect the interchange
Implementation of the policy pattern (interface-oriented programming) Method:
1. interface polymorphism : The intent of the policy model is to encapsulate each algorithm in a separate class with a common interface for a set of algorithms, so that they can be replaced by each other
2. The specific strategy provides different algorithms, the environment is responsible for maintaining and querying the policy , the specific policy and environment to separate, so that the algorithm can not affect the client and the environment, the modification of the
Role Division:
1. Abstract policy roles : typically implemented by an interface or abstract class
2. Specific policy roles : implementing or inheriting abstract roles, encapsulating algorithms and behaviors for corresponding policies
3. Environmental role : holding a reference to an abstract policy role
4. Client: Execute policy through environment role
Example
Abstract policy roles:
1 Public Interface Strategy { 2 3public void dosomething (); 4 }
Specific policy roles:
1 public class Strategya implements strategy { 2 3 @Override 4 public void DoSomething () { 5 System.out.println ("strategy A do something.") ); 6 } 8 }
1 Public class Implements Strategy { 2 3 @Override 4 Public void dosomething () { 5 System.out.println ("Strategy B do Something ." ); 6 } 7 8 }
Environmental role:
1 Public classContext {2 3 Privatestrategy strategy; 4 5 PublicContext (Strategy strategy) {//using interface polymorphism6 This. Strategy =strategy; 7 } 8 9 Public voidExecute () {Ten This. strategy.dosomething (); One } A}
Client:
1 Public classStrategycaller {2 3 Public Static voidMain (string[] args) {4Context C =NewContext (NewStrategya ()); 5 C.execute (); 6 7Context C2 =NewContext (Newstrategyb ()); 8 C2.execute (); 9 } Ten}
Generic Policy mode
Generic Environment role:
1@SuppressWarnings ("All") 2 Public classContext<textendsStrategy>{//equivalent to pass specific policy type instances! Instead of an interface type3 4 Privatestrategy S; 5The constructor transforms the passed-in wildcard type into a corresponding generic using reflection6 PublicContext (class<?extendsStrategy>c) {//Use wildcard type to enlarge specific policy type, actually a bit of interface polymorphism flavor7 Try { 8s =(T) Class.forName (C.getname ()). newinstance ();//Because of the type erase, the original type of the runtime is strategy, taking advantage of a new instance of the original type reflected, (GetName returns what type, with no generics) 9}Catch(Exception e) {Ten E.printstacktrace (); One } A } - - Public voidExecute () { the s.dosomething (); - } - -}
Client:
1 Public classStrategycaller {2 3 Public Static voidMain (string[] args) {4context<strategya> C =NewContext<strategya> (Strategya.class); 5 C.execute (); 6 7context<strategyb> C2 =NewContext<strategyb> (Strategyb.class); 8 C2.execute (); 9 } Ten}
Java Policy mode