Java design mode-Simple Factory mode (Static Factory method)

Source: Internet
Author: User
Tags tutorialspoint

Simple Factory mode (Static Factory method)

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 model is used in those scenarios:

With 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 applies to all kinds of logins: as shown:

 Package Com.tutorialspoint;  Public Interface Login {    // login Authentication public    boolean  Verify (String username, String password);}

Domain Login class:

 Package Com.tutorialspoint;  Public class Implements Login {    @Override    publicboolean  Verify (string Username, string password) {         // TODO auto-generated Method stub         // Business logic        System.out.println ("Login through passcode!" );         return true ;    }}

Password Login class:

 package   Com.tutorialspoint;  public  class  Passwordlogin implements   Login {@Override public  boolean   Verify (string username, string password) { //
      TODO auto-generated method stub  // Span style= "color: #008000;" > business logic  System.out.println ("Login through password!"        );  return  true  ; }}

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

 PackageCom.tutorialspoint; Public classLoginmanager { Public StaticLogin Factory (String type) {if(Type.equals ("Password")) {            return NewPasswordlogin (); } Else if(Type.equals ("passcode") ) {            return NewDomainlogin (); } Else {            Throw NewRuntimeException ("Illegal login type"); }    }}

Test class:

 Public classMainapp { Public Static voidMain (string[] args) {//Abstractapplicationcontext context = new Classpathxmlapplicationcontext ("Beans.xml");////Xmlbeanfactory factory = new Xmlbeanfactory (New Classpathresource ("Beans.xml"));////HelloWorld h = (HelloWorld) factory.getbean ("HelloWorld");//HelloWorld h = (HelloWorld) context.getbean ("HelloWorld1");//System.out.println (H.getmessage ());//Context.registershutdownhook ();//context.close ();String Logintype = "passcode"; String username= "Admin"; String Password= "Admin"; Login Login=loginmanager.factory (Logintype);    Login.verify (username, password); }}

The result of the operation is:

The structure of the simple factory pattern is as follows:

Imagine the real scene, if the above test as a servlet, when the client initiated the login request, the request to the server servlet-> The servlet invokes the factory () method of the factory class Loginmanager based on the client-passed Logintype->factory () method to create a response based on the parameter logintype the login validation class ( Domainlogin or Passwordlogin) and return the login validation class called Method Verfy () to verify that the user name password is correct.

Summarize

Advantages of the Simple factory model:

The core of the pattern is the factory class, which 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 method, and does not need to modify the caller when the system introduces a new login method.

Disadvantages of the Simple factory model:

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

Java design mode-Simple Factory mode (Static Factory method)

Related Article

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.