Simple factory pattern for java design pattern

Keywords: Java Database

In Dr. Yan Hong's book JAVA and Patterns, we begin by describing the simple factory pattern: the simple factory pattern is a class creation pattern, also known as the Static Factory Method pattern. Simple factory pattern is a factory object that decides which product class instance to create.

In terms of login function, if the application system needs to support a variety of login modes, such as password authentication and domain authentication (password authentication is usually to verify users in the database, while domain authentication is to verify users in Microsoft's domain). The natural way to do this is to create an interface that is applicable to all kinds of login modes, as shown in the following figure:

public interface Login {
    //validate logon
    public boolean verify(String name , String password);
}
public class DomainLogin implements Login {

    @Override
    public boolean verify(String name, String password) {
        // TODO Auto-generated method stub
        /**
         * Business logic
         */
        return true;
    }

}
//Copy code
public class PasswordLogin implements Login {

    @Override
    public boolean verify(String name, String password) {
        // TODO Auto-generated method stub
        /**
         * Business logic
         */
        return true;
    }

}

We also need a factory class, LoginManager, to create different login objects and return them according to the caller's different requirements. If an illegal request is encountered, a Runtime exception is returned.

public class LoginManager {
    public static Login factory(String type){
        if(type.equals("password")){
            
            return new PasswordLogin();
            
        }else if(type.equals("passcode")){
            
            return new DomainLogin();
            
        }else{
            /**
             * It would be more appropriate to throw a custom exception here
             */
            throw new RuntimeException("No login type found");
        }
    }
}

Test class:

public class Test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String loginType = "password";
        String name = "name";
        String password = "password";
        Login login = LoginManager.factory(loginType);
        boolean bool = login.verify(name, password);
        if (bool) {
            /**
             * Business logic
             */
        } else {
            /**
             * Business logic
             */
        }
    }
}

If you don't use simple factory mode, verify that the login Servlet code is as follows (assuming that Test is a Servlet, the variables loginType, name, password represent the parameters passed from the client):

public class Test {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        String loginType = "password";
        String name = "name";
        String password = "password";
        //Handling password authentication
        if(loginType.equals("password")){
            PasswordLogin passwordLogin = new PasswordLogin();
            boolean bool = passwordLogin.verify(name, password);
            if (bool) {
                /**
                 * Business logic
                 */
            } else {
                /**
                 * Business logic
                 */
            }
        }
        //Processing Domain Authentication
        else if(loginType.equals("passcode")){
            DomainLogin domainLogin = new DomainLogin();
            boolean bool = domainLogin.verify(name, password);
            if (bool) {
                /**
                 * Business logic
                 */
            } else {
                /**
                 * Business logic
                 */
            }    
        }else{
            /**
             * Business logic
             */
        }
    }
}

Will the above code hurt a lot? Ha-ha

JAVA and Models uses the java.text.DataFormat class as a typical example of a simple factory pattern.

Advantages of Simple Factory Model

The core of the model is the factory class. This class contains the necessary logical judgment to decide when to create an instance of the login validation class, while the caller is exempt from the responsibility of creating the object directly. Simple factory mode achieves the division of responsibility by this way, and when the system introduces a new login mode, it does not need to modify the caller.

Disadvantages of Simple Factory Model

This factory class centralizes all the creation logic. When there is a complex hierarchical structure, all business logic is implemented in this factory class. When it can't work, the whole system will be affected.

Posted by bruckerrlb on Mon, 27 May 2019 12:43:18 -0700