(1) Simple Factory mode of Java design mode

Source: Internet
Author: User

One: Advantages of the Simple factory model

---> In Dr. Shanhong's book Java and Patterns, this describes the simple factory pattern: The Simple factory pattern is the class creation mode, also called the Static Factory method (Factory) mode. A simple factory model is a factory object that determines which product class instances are created.

The core of---> Mode 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.

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



Three: code example for simple Factory mode


Case: Landing page has a variety of landing methods, a factory class, to generate different ways of landing objects.

Login interface

1  Packagecom.yeepay.sxf.interfaces;2 3 /**4 * Login Interface5  * @authorSXF6  *7  */8  Public InterfaceLogin {9 Ten     /** One * Login Method A      * @paramusername -      * @parampasswd -      * @return the      */ -      Public BooleanGologin (String username,string passwd); -}
View Code

QQ Login

1  PackageCom.yeepay.sxf.interfaces.impl;2 3 ImportCom.yeepay.sxf.interfaces.Login;4 /**5 * QQ Number Login6  * @authorSXF7  *8  */9  Public classQloginImplementslogin{Ten  One @Override A      Public BooleanGologin (string username, string passwd) { -         //Login with QQ account password -         return false; the     } -  -      -}
View CodeWeibo login
1  PackageCom.yeepay.sxf.interfaces.impl;2 3 ImportCom.yeepay.sxf.interfaces.Login;4 /**5 * Weibo login6  * @authorSXF7  *8  */9  Public classWeibologinImplementsLogin {Ten  One @Override A      Public BooleanGologin (string username, string passwd) { -         //Login with Weibo account -         return false; the     } -  -      -}
View Code

Landing Mode Factory

1  PackageCom.yeepay.sxf.interfaces.impl;2 3 ImportCom.yeepay.sxf.interfaces.Login;4 /**5 * Landing mode of the factory class6  * @authorSXF7  *8  */9  Public classLoginfactory {Ten      Public StaticLogin Createlogin (String fn) { One         if("QQ". Equals (FN)) { A             return NewQlogin (); -}Else if("Weibo". Equals (FN)) { -             return NewWeibologin (); the}Else{ -             return NULL; -         } -     } +}
View Code

Simulation Scenario Testing

1  Packagecom.yeepay.sxf.test;2 3 ImportCom.yeepay.sxf.interfaces.Login;4 Importcom.yeepay.sxf.interfaces.impl.LoginFactory;5 6  Public classTest {7 8      Public Static voidMain (string[] args) {9         //Front Desk Submit user name and password, and login methodTenString username= "290325337"; OneString password= "Shangxiaofei"; AString fn= "QQ"; -          -         //backstage access to the appropriate login bean theLogin login=Loginfactory.createlogin (FN); -          -         //Log In -         Booleanflag=login.gologin (username, password); +          -         //determine landing success or failure +         if(flag) { A             //Landing Success at}Else{ -             //Login Failed -         } -          -          -     } in      -      to}
View Code

(1) Simple Factory mode of Java design mode

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.