Use of factory method mode in android
Factory Method)
The factory method mode is the class creation mode, also known as the polymorphism factory mode. The purpose of this mode is to define a factory interface for creating product objects and postpone the actual creation work to the factory subclass.
The factory model supports the inheritance mechanism, and it also overcomes the need to modify the factory role when adding new product roles.
We can see that this mode involves the following roles:
A. Abstract Factory role: this role is the core of the factory method mode and is unrelated to the application. Any factory class that creates an object in the mode must implement it.
B. Specific factory role: this role is a specific class that implements the abstract factory role interface. A specific factory role contains a logical relationship closely related to the application and is called to create a product object.
C. Abstract Product role: the parent class of the object created in the factory method mode. It is also a common interface of the product object.
D. Specific product role: this role implements the abstract Product role and provides the factory method to create objects.
The following is an example of how to use the factory method. My code is as follows:
Abstract Product role:
/* The role of abstract */
Public abstract classWifiState {
PrivateString wifiName;
Private intWifiType;
Private intIp address;
PublicString getWifiName (){
ReturnWifiName;
}
Public voidSetWifiName (String wifiName ){
This. WifiName = wifiName;
}
Public intGetWifiType (){
ReturnWifiType;
}
Public voidSetWifiType (IntWifiType ){
This. WifiType = wifiType;
}
Public intGetIp (){
ReturnIp address;
}
Public voidSetIp (IntIp ){
This. Ip = ip;
}
}
Specific product role (no password WIFI required ):
/* The roleConcret*/
Public classWifiStateNoPwdExtendsWifiState {
PublicWifiStateNoPwd (){
SetWifiName (no password WIFI, can be used directly !);
SetWifiType (0 );
SetIp (1921682204 );
}
}
Specific product role (password WIFI required ):
/* The roleConcret*/
Public classWifiStateNeedPwdExtendsWifiState {
PublicWifiStateNeedPwd (){
SetWifiName (WIFI only available after you enter the password !);
SetWifiType (1 );
SetIp (1921682205 );
}
}
Specific product roles (charged WIFI ):
Public classWifiStateChargesExtendsWifiState {
PublicWifiStateCharges (){
SetWifiName (WIFI to be paid! Are you willing to use it !);
SetWifiType (2 );
SetIp (1921682206 );
}
}
Abstract Factory role:
/* The role of abstract method */
Public interfaceWififacloud {
PublicWifiState factory ();
}
Specific factory role (no password required for WIFI factory ):
/* The roleConcretMethod */
Public classWifiNoPwdFactoryImplementsWififacloud {
@ Override
PublicWifiState factory (){
Return newWifiStateNoPwd ();
}
}
Specific factory role (password required for WIFI factory ):
/* The roleConcretMethod */
Public classWifiNeedPwdFactoryImplementsWififacloud {
@ Override
PublicWifiState factory (){
Return newWifiStateNeedPwd ();
}
}
Specific factory role (paid WIFI factory ):
/* The roleConcretMethod */
Public classWifiChargesFactoryImplementsWififacloud {
@ Override
PublicWifiState factory (){
Return newWifiStateCharges ();
}
}
Client role ):
Public classFactoryMethodActivityExtendsActivity {
Private staticStringTAG= VerifyFactoryMethodActivity;
PrivateWifiFactory currFactory;
PrivateWifiState currWifi;
@ Override
Protected voidOnCreate (BundlesavedInstanceState ){
Super. OnCreate (savedInstanceState );
SetContentView (R. layout.Activity_verify_factory_method);
// WIFI without a password
CurrFactory =NewWifiNoPwdFactory ();
CurrWifi = currFactory. factory ();
Log (WIFI information: + currWifi. getWifiName () + ===
+ CurrWifi. getWifiType () + ==+ currWifi. getIp ());
// WIFI with password required
CurrFactory =NewWifiNeedPwdFactory ();
CurrWifi = currFactory. factory ();
Log (WIFI information: + currWifi. getWifiName () + ===
+ CurrWifi. getWifiType () + ==+ currWifi. getIp ());
// Charged WIFI
CurrFactory =NewWifiChargesFactory ();
CurrWifi = currFactory. factory ();
Log (WIFI information: + currWifi. getWifiName () + ===
+ CurrWifi. getWifiType () + ==+ currWifi. getIp ());
}
Private voidLog (String log ){
Log.D(TAG, Log );
}
}
At this point, we have analyzed the principles and usage of the factory method model.