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.
In what scenarios is the simple factory model used? The following is an example of my understanding:
Take the login function as an example. If the application system needs to support multiple login methods, such as password authentication and domain authentication (password authentication is usually used to verify the user in the database, domain authentication requires you to verify the user in the Microsoft domain ). The natural method is to create an interface applicable to various logon methods, as shown in:
Code:
[Java]
Package com. bankht. factory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:20:00
*
* @ Class description: creates an interface applicable to various logon methods.
*/
Public interface Login {
Public boolean verify (String name, String password );
}
Package com. bankht. factory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:20:00
*
* @ Class description: creates an interface applicable to various logon methods.
*/
Public interface Login {
Public boolean verify (String name, String password );
}
[Java]
Package com. bankht. factory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:25:06
*
* @ Class description: domain authentication
*/
Public class DomainLogin implements Login {
@ Override
Public boolean verify (String name, String password ){
// TODO Auto-generated method stub
/**
* Business logic
*/
Return true;
}
}
Package com. bankht. factory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:25:06
*
* @ Class description: domain authentication
*/
Public class DomainLogin implements Login {
@ Override
Public boolean verify (String name, String password ){
// TODO Auto-generated method stub
/**
* Business logic
*/
Return true;
}
}
[Java]
Package com. bankht. factory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:27:40
*
* @ Class description: Password Authentication
*/
Public class PasswordLogin implements Login {
@ Override
Public boolean verify (String name, String password ){
// TODO Auto-generated method stub
/**
* Business logic
*/
Return true;
}
}
Package com. bankht. factory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:27:40
*
* @ Class description: Password Authentication
*/
Public class PasswordLogin implements Login {
@ Override
Public boolean verify (String name, String password ){
// TODO Auto-generated method stub
/**
* Business logic
*/
Return true;
}
}
We also need a factory-class LoginManager to create different login objects and return them according to the caller's requirements. If an invalid requirement is met, a Runtime exception is returned.
[Java]
Package com. bankht. factory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:28:02
*
* @ Class description: factory class
*/
Public class LoginManager {
Public Login factory (String type ){
If (type. equals ("password ")){
System. out. println ("password authentication is taken from the factory ");
Return new PasswordLogin ();
} Else if (type. equals ("passcode ")){
System. out. println ("what you get from the factory is: domain authentication ");
Return new DomainLogin ();
} Else {
/**
* It is more appropriate to throw a custom exception here.
*/
Throw new RuntimeException ("No logon type found ");
}
}
}
Package com. bankht. factory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:28:02
*
* @ Class description: factory class
*/
Public class LoginManager {
Public Login factory (String type ){
If (type. equals ("password ")){
System. out. println ("password authentication is taken from the factory ");
Return new PasswordLogin ();
} Else if (type. equals ("passcode ")){
System. out. println ("what you get from the factory is: domain authentication ");
Return new DomainLogin ();
} Else {
/**
* It is more appropriate to throw a custom exception here.
*/
Throw new RuntimeException ("No logon type found ");
}
}
}
Perform a test:
[Java]
Package com. bankht. factory;
Import org. junit. Test;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:31:41
*
* @ Class description: Test factory class
*/
Public class TestFactory {
@ Test
Public void testFactory (){
// TODO Auto-generated method stub
String loginType = "password ";
String name = "name ";
String password = "password ";
Login login = new LoginManager (). factory (loginType );
Boolean bool = login. verify (name, password );
If (bool ){
/**
* Business logic
*/
} Else {
/**
* Business logic
*/
}
}
}
Package com. bankht. factory;
Import org. junit. Test;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:31:41
*
* @ Class description: Test factory class
*/
Public class TestFactory {
@ Test
Public void testFactory (){
// TODO Auto-generated method stub
String loginType = "password ";
String name = "name ";
String password = "password ";
Login login = new LoginManager (). factory (loginType );
Boolean bool = login. verify (name, password );
If (bool ){
/**
* Business logic
*/
} Else {
/**
* Business logic
*/
}
}
}
Test results:
[SQL]
What you get from the factory is: Password Authentication
What you get from the factory is: Password Authentication
The structure of the simple factory model is as follows:
We can imagine the real scenario. If we use the Test above as a servlet, when the client initiates a login request --> the Servlet sent to the server --> Servlet calls the factory () method of the factory LoginManager according to the loginType passed by the client --> factory () the method is to create a logon verification class (DomainLogin or PasswordLogin) based on the loginType parameter and return --> the method of calling the logon verification class verify () to verify that the user name and password are correct.
If you do not use the simple factory mode, verify that the logon Servlet code is as follows (assume that Test is a Servlet, and the variable loginType, name, and password indicate the parameters passed from the client ):
[Java]
Package com. bankht. factory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:26:45
*
* @ Class description:
*/
Public class Test {
Public static void main (String [] args ){
// TODO Auto-generated method stub
String 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
*/
}
}
// Process domain authentication
Else if (loginType. equals ("passcode ")){
DomainLogin domainLogin = new DomainLogin ();
Boolean bool = domainLogin. verify (name, password );
If (bool ){
/**
* Business logic
*/
} Else {
/**
* Business logic
*/
}
} Else {
/**
* Business logic
*/
}
}
}
Package com. bankht. factory;
/**
* @ Author: special soldier-AK47
* @ Creation Time: 02:26:45
*
* @ Class description:
*/
Public class Test {
Public static void main (String [] args ){
// TODO Auto-generated method stub
String 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
*/
}
}
// Process domain authentication
Else if (loginType. equals ("passcode ")){
DomainLogin domainLogin = new DomainLogin ();
Boolean bool = domainLogin. verify (name, password );
If (bool ){
/**
* Business logic
*/
} Else {
/**
* Business logic
*/
}
} Else {
/**
* Business logic
*/
}
}
}
Author: m13666425773