A lot of factory classes are used in Android, such as threadfactory to create abstract runnable. The following is a simple example to learn about factory patterns. First, the role
Factory mode (Factory method): Defines an interface for creating objects, allowing subclasses to decide which class to instantiate. The instantiation of a class is deferred to its subclasses.
Abstract Factory: Provides an interface to create a series of related or interdependent objects without specifying their specific classes.
Second, the use of the scene
1. Manage and instantiate the implementation class for the same interface
Third, the use of common methods
Before telling the example, let's assume that you want to do such a design. Design a USB function interface with store (storage) and Takealong (easy to carry) two behaviors. Then design two products, one is phone (mobile), the other is camera (camera), it is obvious that they can be USB implementation class. In order to facilitate unified management and creation, it is easy to design a simple factory model.
(1) Common factory methods
First of all, we can draw the relevant design diagram:
The code implementation is as follows:
Define USB Interface
Public interface USB {
void Store ();
void Takealong ();
}
Phone class
public class Phone implements USB {
@Override the public
Void Store () {
System.out.println (' This is Phone usb! ');
}
@Override public
void Takealong () {
//TODO auto-generated method stub
} public
void Call () {
//todo
}
public void SMS () {
//todo
}
}
Camera class
The public class Camera implements USB {
@Override
the public void Store () {
System.out.println ("This is Camera us B! ");
}
@Override public
void Takealong () {
//TODO auto-generated a stub
} public
void Takephotos () C24/>//todo
}
}
Create a simple factory class UsbFactory1, responsible for the production
/**
* Ordinary Factory method
* Parameter passed string error
*: Create object correctly * *
* @author Xuzhaohu */Public
class UsbFactory1 {public
USB produce (String type) {
if (' Phone '. Equals (type)) {return
new phone ();
} else if ("Camera". Equals (Type)) {return
new camera ();
} else {
System.out.println ("Please enter the correct type!");
return null;}}}
Subclass instantiation is created by the factory class, if you want to create a camera,
/* Common Factory method **/
UsbFactory1 factory1 = new UsbFactory1 ();
USB usb1 = factory1.produce ("Camera");//+phone
Usb1.store ();
Output: This is Camera usb!
Summary: The implementation of the basic functions of the factory model, but need to pass the reference to control, there will be a lot of uncertainties, can be defined in the factory class production of different products, is described below the factory production of multiple methods.
(2) Factory multiple methods
Just customize it in the Usbfactory, and the business is more trenchant.
Modify the factory class according to the design
* *
Multiple Factory class method
*
* @author xuzhaohu
* *
/Public
class UsbFactory2 {public
Usb Producephone ( ) {return
new Phone ();
}
Public USB Producecamera () {return
new Camera ();
}
}
Similarly, to instantiate a subclass object, you can invoke the following:
/* Multiple factory method mode **/
UsbFactory2 factory2 = new UsbFactory2 ();
USB USB2 = Factory2.producephone ();//+camera
Usb2.store ();
Output: This is Phone usb!
Does this make the business logic a little clearer?
But if you want to call production in many places, you can't go through the example of a chemical plant every time.
Yes, you can use class access to add a static method to the factory class.
/**
* Static Factory method
*: no instance
*
* @author Xuzhaohu * */Public
class UsbFactory3 {
public static Phone Producephone () {return
new phone ();
}
public static Camera Producecamera () {return
new Camera ();
}
}
So this time can be directly without the example of chemical plants to visit the
/* Static Factory method mode **/
Usb USB3 = Usbfactory3.producephone ();//+camera
Usb3.store ();
Output: This is Phone usb!
This makes it a little more convenient. In general, this will basically meet the requirements, but if the need to increase the production of another implementation of the product Phone1, this time will certainly need to modify the factory class, in the factory class to add a new type of production Phone1 method. From a design standpoint, it may violate the design principles of closures, which are open to the modification to be closed, and can be designed with abstract factory methods in order not to violate this principle, as described below.
(3) Abstract Factory method
In order not to modify the methods in the factory, we can create a corresponding factory class for each product to achieve production. At this time, we can define the behavior of production (produce) through an interface Provider, then let the factory class of each product implement this interface. This way, if a new product is to be produced, it is only necessary to implement a factory class first. Design as follows
Production interface Provider
/**
* The factory production of the act as an interface independent * * * @author Xuzhaohu * * * * * */public
interface Provider {
USB produce ();
}
Every product has its own factory.
Phonefactory Factory
public class Phonefactory implements Provider {
@Override public
USB Produce () {
//TODO auto-generated Metho D stub return
new Phone ();
}
Camerafactory Factory
public class Camerafactory implements Provider {
@Override public
USB Produce () {
//TODO auto-generated Meth OD stub return
new Camera ();
}
Each product can be produced according to its own business, such as the production of camera
/* Abstract Factory method pattern **/
/*1. Extensibility is good, do not need to modify source code
*2. Add a class to implement the USB interface + a corresponding implementation Provider factory class **/Provider
= new Camerafactory ();
USB usb4 = Provider.produce ();
Usb4.store ();
Output: This is Camera usb!
Summary: Abstract factory model is still worth recommending from the principle of software design pattern.
Reference: http://zz563143188.iteye.com/blog/1847029