The strategy pattern (strategy pattern) embodies two fundamental principles of object-oriented design: The concept of package change, the use of interfaces in programming, and not the implementation of interfaces. The policy pattern is defined as follows:
Define a set of algorithms that encapsulate each algorithm and make them interchangeable. The policy pattern enables these algorithms to change independently when the client calls them.
The policy pattern enables developers to develop software that consists of many replaceable parts, and the relationships between the parts are weakly connected. The weak connection feature makes the software more extensible, easy to maintain, and more importantly, it greatly improves the reusability of the software.
The following uses the spring source code Description policy mode (Spring validation)
1. UML Diagram Description
Description: Uservalidator and Harmlesshandlevalidator are two behavioral strategies, respectively, to implement different algorithms.
2. Class and Interface code
Class:org.springframework.validation.ValidationUtils validation Tool class Java code public static void Invokevalidator (validator validator, object obj, errors errors) { assert.notnull (validator, "Validator must not be null "); assert.notnull (errors, " Errors object must not be null "); if (logger.isdebugenabled ()) { logger.debug ("invoking validator [" + validator + "]"); } if (Obj != null && !validator.supports (Obj.getclass ())) { throw new IllegalArgumentException ( "validator [" + validator.getclass () + "] does not support [" + obj.getclass () + "]"); } Validator.validate (obj, errors); if ( Logger.isdebugenabled ()) { if (Errors.haserrors ()) { logger.debug ("validator found " + Errors.geterrorcount () + " errors"); } else { logger.debug ("Validator found no errors "); } } }
interface:org.springframework.validation.validator Java code public interface validator { boolean supports (class clazz); void validate (object target, errors errors); }
class:uservalidator Java code public class uservalidator implements validator { @Override public boolean supports (class clazz) { return user.class.equals (clazz); } @Override public void validate (object target, errors errors) { User user = (User) target; if (! Stringutils.haslength (User.getusername ())) { errors.rejectvalue ("username", ", " Login code must be filled in. "); } if (! Stringutils.haslength (User.getpassword ())) { errors.rejectvalue ("Password", ", " Login password must be filled in. "); } if (! Stringutils.haslength (User.getname ())) { errors.rejectvalue ("name", ", " user name must be filled in.) "); } } }
Class:harmlesshandlevalidator Java code public class harmlesshandlevalidator implements Validator { @Override public boolean supports (class clazz) { return harmlesshandle.class.equals (clazz); } @Override public void Validate (object target, errors errors) { HarmlessHandle harmlessHandle = (harmlesshandle) target; if (! Stringutils.haslength (Harmlesshandle.gethandleno ())) { errors.rejectvalue ("Handleno", The ", " code must be filled in. "); } if (! Stringutils.haslength (Harmlesshandle.gethandlename ())) { errors.rejectv