Simple Factory)
Simple Factory)
The simple Factory mode is the class creation mode, also known as the Static Factory Method ). The simple factory mode is used by a factory to determine which type of instances to create. The following figure shows its structure:
From the above structure, we can see that the simple factory model involves three roles: factory role, abstract Product role, and specific product role.
A. Factory role: this role is the core role in the simple factory model and contains the logic closely related to the application. The factory role determines the class to be created under the client call.
B. Abstract Product role: this role is the parent class of the object created by the factory role. It is a common interface for creating objects. Generally, this interface is implemented by interfaces or abstract classes in Java.
C. Specific product role: Any object created by the factory method is an instance of this class. It is the concrete implementation of the abstract Product role.
Note:
In Java, we generally implement some objects with the same features but no logical relationship using Java interfaces; on the contrary, Java Abstract classes are used to implement some objects with similar features and certain logical relationships.
The following example demonstrates the use of the simple factory model. In this example, we search for Wi-Fi signals and select different Internet access methods based on the public type of WIFI. There are three types: Password required, no password required, and fee charged. If wifiType in the Wi-Fi information returned by the search is 0, the password is free. If it is 1, the password is required. If it is 2, the fee is charged. Here I implement it in the Android project.
Abstract Product role (WifiState ):
/* The role of abstract */
Public abstract classWifiState {
StringwifiName;
IntWifiType;
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 roles (WifiStateNoPwd ):
/* The roleConcret*/
Public classWifiStateNoPwdExtendsWifiState {
PublicWifiStateNoPwd (){
SetWifiName (I don't need a password for WIFI !);
SetWifiType (0 );
SetIp (1921680202 );
}
}
Specific product roles (WifiStateNeedPwd ):
/* The roleConcret*/
Public classWifiStateNeedPwdExtendsWifiState {
PublicWifiStateNeedPwd (){
SetWifiName (I need a password for WIFI !);
SetWifiType (1 );
SetIp (1921680203 );
}
}
Specific product roles (WifiStateCharges ):
/* The roleConcret*/
Public classWifiStateChargesExtendsWifiState {
PublicWifiStateCharges (){
SetWifiName (I am charged for WIFI !);
SetWifiType (2 );
SetIp (1921680204 );
}
}
Wififacloud ):
/* Therole of factory */
Public classWififacloud {
Public staticWifiState factory (IntWifiType ){
WifiStatecurrWifi =Null;
Switch(WifiType ){
Case0:
CurrWifi =NewWifiStateNoPwd ();
Break;
Case1:
CurrWifi =NewWifiStateNeedPwd ();
Break;
Case2:
CurrWifi =NewWifiStateCharges ();
Break;
Default:
Break;
}
Return CurrWifi;
}
}
Paste the following at the end:
Obviously, we use the factory to create multiple instance Objects Based on different conditions, which is a simulation implementation of a simple factory. Next, we will discuss its advantages and disadvantages:
Advantages:
The core of the mode is the factory class. This class contains important Judgment logic to determine when to create the instance. At the same time, it also avoids the task of directly creating products on the client, instead, it is only responsible for consuming products, which effectively achieves the division of responsibility between the client and the product layer.
Disadvantages:
The disadvantages are also obvious. First, the factory class in this mode concentrates all the judgment logic and becomes an omnipotent creation class. Once this class has some problems, the whole system has a direct problem. Second, when the product class has different interfaces, the factory class needs to know when to create a product, this mixed the timing judgment and the creation of the product, which will be very troublesome for future expansion (the factory method solves this problem). Third, obviously, the simple factory uses the static factory method to create objects, so the factory role cannot use the inherited hierarchy (the factory method mode solves this problem ).
Note:
To some extent, the simple factory model has reached the "open-close" principle to a limited extent.
Well, the simple factory model will be summarized here. Next we will continue to analyze and overcome the new model of the simple factory model-the factory method model to a great extent.