Java Simple Factory mode reprint

Source: Internet
Author: User

The Simple Factory mode is the creation mode of the class, also called the Static Factory method (Factory) mode. A simple factory model is a factory object that determines which product class instances are created.

So the simple Factory mode is used in what scenario, the following is an example of my understanding:

Take the login function, if the application needs to support a variety of login methods such as: Password Authentication, domain authentication (password authentication is usually to go to the database to authenticate users, and domain authentication is required to the Microsoft domain to authenticate users). The natural way to do this is to create an interface that is applicable to various login methods, as shown in:

 Public Interface Login {    // Login Verify public    boolean  Verify (String name, String password);}
 public  class  domainlogin implements   Login {@Override  public  boolean   verify (String name, String Password { //  TODO auto-generated m Ethod stub  /**   * Business logic */ return  true     ; }}
 Public class Implements Login {    @Override    publicboolean  Verify (string name, string password) {         //  TODO auto-generated method stub        /**         * Business logic           */        returntrue;    }}

We also need a factory class Loginmanager that creates different login objects and returns based on the caller's different requirements. If an illegal request is encountered, a runtime exception is returned.

 Public classLoginmanager { Public StaticLogin Factory (String type) {if(Type.equals ("Password")){                        return NewPasswordlogin (); }Else if(Type.equals ("passcode")){                        return NewDomainlogin (); }Else{            /*** It would be more appropriate to throw a custom exception here*/            Throw NewRuntimeException ("Login type not Found"); }    }}

Test class:

 Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString logintype = "Password"; String name= "Name"; String Password= "Password"; Login Login=loginmanager.factory (Logintype); BooleanBOOL =login.verify (name, password); if(bool) {/*** Business logic*/        } Else {            /*** Business logic*/        }    }}

The structure of the simple factory pattern is as follows:

We can imagine the real scenario, if the above test is a servlet, when the client initiates a login request--and the request is given to the server's servlet--> The servlet invokes the factory () method of the factory class Loginmanager based on the Logintype passed by the client-->factory () method creates the appropriate login validation class based on the parameter Logintype ( Domainlogin or Passwordlogin) and returns the login validation class call method Verify () verifies that the user name password is correct

If you do not use the Simple Factory mode, verify that the login servlet code is as follows (assuming that test is a servlet, the variables logintype, name, password represent parameters passed from the client):

 Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubString Logintype= "Password"; String name= "Name"; String Password= "Password"; //Process Password Authentication        if(Logintype.equals ("Password") {passwordlogin Passwordlogin=NewPasswordlogin (); BooleanBOOL =passwordlogin.verify (name, password); if(bool) {/*** Business logic*/            } Else {                /*** Business logic*/            }        }        //Process domain authentication        Else if(Logintype.equals ("passcode") {domainlogin Domainlogin=NewDomainlogin (); BooleanBOOL =domainlogin.verify (name, password); if(bool) {/*** Business logic*/            } Else {                /*** Business logic*/            }            }Else{            /*** Business logic*/        }    }}

The above code will not be very painful ah ... Oh

The Java and Patterns book uses the Java.text.DataFormat class as a typical example of a simple factory model.

Advantages of the Simple factory model

The core of the pattern is the factory class. This class contains the necessary logic to determine when to create an instance of the login validation class, while the caller is exempt from the responsibility to create the object directly. The simple factory model realizes the separation of responsibilities by this approach, without having to modify the caller when the system introduces a new login method.

Disadvantages of the Simple factory model

This factory class centralizes the creation logic, and when there is a complex hierarchical hierarchy, all business logic is implemented in this factory class. When it's not working, the whole system will be affected.

Java Simple Factory mode reprint

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.