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