Design Patterns (15) - Template Method Pattern

Template method pattern

1. Definition

        Defining the skeleton of an algorithm in an operation, while deferring some steps to subclasses, template methods allow subclasses to redefine certain steps of an algorithm without changing its structure.

2. Sample code

        Sample code used to implement common login and staff login

  

/* Encapsulate the data required for login control */
public class LoginModel{
   private String loginId;
   private String pwd;
   public String getLoginId(){
       return loginId;
   }
   public void setLoginId(String loginId){
       this.loginId = loginId;
   }
   public String getPwd(){
       return pwd;
   }
   public void setPwd(String pwd){
       this.pwd = pwd;
   }
}

/*Define the public login control algorithm skeleton*/
public abstract class LoginTemplate{
   // Determine whether to log in
   public final boolean login(LoginModel lm){
      //1. Obtain the corresponding data according to the personnel number
      LoginModel dbLm = this.findLoginUser(lm.getLoginId());
      if(dbLm != null){
          //2. Encrypt the password
          String encryptPwd = this.encryptPwd(lm.getPwd);
          //3. Set the encrypted password back to the login model
          lm.setPwd(encryptPwd);
          //4. Determine whether it matches
          return this.match(lm,dbLm);
      }
   }
   //Get the corresponding information according to the login number
   public abstract LoginModel findLoginUser(String loginId);
   //encrypt the password
   public String encryptPwd(String pwd){
      return pwd;
   }
   // Determine if it matches
   public boolean match(LoginModel lm,LoginModel dbLm){
      if(lm.getLoginId().equals(dbLm.getLoginId()) && lm.getPwd().equals(dbLm.getPwd)){
         return true;
      }
      return false;
   }
}

 

/* Realize the login of ordinary users */
public class NormalLogin extends LoginTemplate{
    public LoginModel findLoginUser(String loginId){
       LoginModel lm = new LoginModel();
       lm.setLoginId(loginId);
       lm.setPwd("normalpwd");
    }
}

/*To realize the login of the staff, the password needs to be encrypted and verified*/
public class WorkerLogin extends LoginTemplate{
    public LoginModel findLoginUser(String loginId){
       LoginModel lm = new LoginModel();
       lm.setLoginId(loginId);
       lm.setPwd("workerpwd");
    }
    //Override the password encryption method of the parent class to provide the real implementation
    public String encryptPwd(){
       System.out.println("Use MD5 for password encryption");
       return pwd;
    }
}

   

/*Client login*/
public class Client{
    public static void main(String args[]){
        LoginModel lm = new LoginModel();
        lm.setLoginId("admin");
        lm.setPwd("workerpwd");
        // Prepare the object to judge
        LoginTemplate lt = new WorkerLogin();
        LoginTemplate lt2 = new NormalLogin();
        //do login test
        boolean flag = lt.login(lm);
        System.out.println("Can log in to work platform: " + flag);
        boolean flag2 = lt2.login(lm);
        System.out.println("Ordinary personnel can log in: " + flag2);     
    }
}

 

3. Practical application

      The function of the template method pattern is to fix the skeleton of the algorithm, while allowing the specific algorithm to be extended. Templates can use abstract classes or interfaces. Usually, abstract classes are used as templates to constrain the behavior of subclasses and provide public functions for subclasses.

 

The essence of the template method pattern: fixed algorithm skeleton

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326413384&siteId=291194637