The simple Factory mode of Java and Mode (RPM)

Source: Internet
Author: User

In Dr. Shanhong's book, Java and Patterns, the simple factory pattern is described as follows: The simple factory pattern is the creation mode of the class, also known as 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:

Interface Login {     // Login Validation      Boolean Verify (string name, string password);}   
Implements Login {
@Override boolean Verify (string name, string password) { // TODO auto-generated Method stub /**true;}
Implements Login {
@Override boolean Verify (string name, string password) { // TODO auto-generated Method stub /**true;}

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.

Class Loginmanager {     static Login factory (String type) {         if (type.equals ("password")) {                           New Passwordlogin (); }new Domainlogin ();} /**New RuntimeException ("Login type not 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 Span style= "color: #008000;" >*/} else {/** * Business logic Span style= "color: #008000;" >*/ } } } 

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):

PublicClass Test {PublicStaticvoid Main (string[] args) {//TODO auto-generated Method StubString logintype = "password"; String name = "Name"; String password = "password";//Process 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 Span style= "color: #008000;" >*/} else {/** * Business logic Span style= "color: #008000;" >*/}}else{/** * Business logic Span style= "color: #008000;" >*/ } } } 

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.

The simple Factory mode of Java and Mode (RPM)

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: 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.