Design pattern--spring Use policy mode in source code (strategy pattern)

Source: Internet
Author: User
Tags assert

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.