Factory mode (Factory method)
The factory method pattern is the creation mode of the class, also called the Polymorphic factory pattern. The intent of this pattern is to define a factory interface that creates a product object and defer the actual creation to the factory subclass.
Factory mode supports inheritance, and it also overcomes the need to modify the factory role when new product roles are added. Let's look at the structure of the factory method pattern:
As you can see, the following roles are involved in this pattern:
A, Abstract Factory role: This role is at the heart of the factory method pattern, which is irrelevant to the application, and any factory class that creates objects in the schema must implement it.
B, specific factory role: This role is the specific class that implements the abstract factory role interface. The specific factory role contains a logical relationship that is closely related to the application and is called to create the Product object.
C, abstract product role: The parent class of the object created by the factory method pattern, which is also the co-owned interface of the product object.
D, specific product roles: This role implements the abstract product role, which is provided to the factory method to create objects.
Here is an example of how the factory method pattern is used, and my code is as follows:
Abstract Product Roles:
/* The role of abstract */
Public abstract class wifistate {
Private String wifiname;
private int Wifitype;
private int IP;
Public String Getwifiname () {
return Wifiname;
}
Public void setwifiname (String wifiname) {
this. wifiname = wifiname;
}
Public int Getwifitype () {
return Wifitype;
}
Public void setwifitype (int wifitype) {
this. Wifitype = Wifitype;
}
Public int getip () {
return IP;
}
Public void setIp (int IP) {
this. IP = IP;
}
}
Specific product roles (no password WiFi required):
/* The role of concret * *
Public class Wifistatenopwd extends wifistate {
Public Wifistatenopwd () {
Setwifiname (" no password WiFi, can be used directly Oh!") ");
Setwifitype (0);
SetIp (1921682204);
}
}
Specific product role (requires password WiFi):
/* The role of concret * *
Public class Wifistateneedpwd extends wifistate {
Public Wifistateneedpwd () {
Setwifiname (" WiFi is only available if you enter a password!") ");
Setwifitype (1);
SetIp (1921682205);
}
}
Specific product roles (WiFi for a fee):
Public class Wifistatecharges extends wifistate {
Public Wifistatecharges () {
Setwifiname (" WiFi is a must for the price!" Would you like to use it! ");
Setwifitype (2);
SetIp (1921682206);
}
}
Abstract Factory Role:
/* The role of the abstract method */
Public interface wififactory {
Public Wifistate Factory ();
}
Specific factory role (no password WiFi factory):
/* The role of concret method */
Public class Wifinopwdfactory implements wififactory {
@Override
Public Wifistate Factory () {
return new Wifistatenopwd ();
}
}
Specific factory role (requires password WiFi factory):
/* The role of concret method */
Public class Wifineedpwdfactory implements wififactory {
@Override
Public Wifistate Factory () {
return new Wifistateneedpwd ();
}
}
Specific factory role (paid WiFi factory):
/* The role of concret method */
Public class Wifichargesfactory implements wififactory {
@Override
Public Wifistate Factory () {
return new Wifistatecharges ();
}
}
Client role (clients):
Public class Factorymethodactivity extends Activity {
private static String TAG = "Verifyfactorymethodactivity";
Private Wififactory currfactory;
Private Wifistate Currwifi;
@Override
protected void onCreate (bundlesavedinstancestate) {
Super. OnCreate (savedinstancestate);
Setcontentview (r.layout. Activity_verify_factory_method);
// WiFi with no password required
currfactory = new wifinopwdfactory ();
Currwifi = currfactory. Factory ();
Log ("WIFI Info:"+Currwifi. Getwifiname () +"= = ="
+ Currwifi. Getwifitype () + "= = =" + Currwifi. GetIP ());
// WiFi with password required
currfactory = new wifineedpwdfactory ();
Currwifi = currfactory. Factory ();
Log ("WIFI Info:"+Currwifi. Getwifiname () +"= = ="
+ Currwifi. Getwifitype () + "= = =" + Currwifi. GetIP ());
// WiFi is available for a fee
currfactory = new wifichargesfactory ();
Currwifi = currfactory. Factory ();
Log ("WIFI Info:"+Currwifi. Getwifiname () +"= = ="
+ Currwifi. Getwifitype () + "= = =" + Currwifi. GetIP ());
}
private void log (String log) {
Log. D (TAG, log);
}
}
At the end, attach my program to run:
Here, we have analyzed the principle and use of the factory method mode.
Technology Exchange Group:179914858
Factory method mode used on Android