Java design patterns-simple factory

Source: Internet
Author: User

The simple Factory mode is the class creation mode, also called the Static Factory Method mode. The simple factory mode is determined by a factory object to create a product instance. The following is a simple factory UML Model:

Vcq9yOejusrWu/rIz9akoaLTys/kyM/keys + keys/2jo8L3A + CjxwPgo8L3A + CjxwcmUgY2xhc3M9 "brush: java;"> package com. simple. factory;/*** logon interface **/public interface Login {public boolean login (String name, String password );}

Mobile phone number Logon:

Package com. simple. factory; public class PhoneNumberLogin implements Login {@ Overridepublic boolean login (String name, String password) {// Login processing logic if (password. equals ("123123") {return true;} else {return false ;}}}

Email Logon:

Package com. simple. factory;/*** email logon **/public class EmailLogin implements Login {@ Overridepublic boolean login (String name, String password) {// Login processing logic if (password. equals ("123123") {return true;} else {return false ;}}}

User name Logon:

Package com. simple. factory; public class UserNameLogin implements Login {@ Overridepublic boolean login (String name, String password) {// Login processing logic if (password. equals ("123123") {return true;} else {return false ;}}}

Static factory core class:

Package com. simple. factory;/*** core static factory class **/public class LoginManager {public static Login factory (String type) {if (type. equals ("Email") {return new EmailLogin ();} if (type. equals ("PhoneNumber") {return new PhoneNumberLogin ();} if (type. equals (type. equals ("UserName") {return new UserNameLogin () ;}return null ;}}

Test client:

Package com. simple. factory;/*** test Client **/public class Client {public static void main (String [] args) {String mLoginType = "PhoneNumber"; String name = "dashu "; string password = "123123"; Login login = LoginManager. factory (mLoginType); boolean bool = login. login (name, password); if (bool) {System. out. println ("Logon successful");} else {System. out. println ("Logon Failed ");}}}

When the system introduces a new Login method, you do not need to modify the caller.

Disadvantages of the simple factory Model

This factory class focuses on the Creation logic. When there is a complex multi-level hierarchy, all the business logic is implemented in this factory class. When it cannot work, the entire system will be affected.


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.