An overview of the
The factory pattern is a creative design pattern responsible for the creation of more complex objects
Three factory models
Static factory, simple factory, abstract factory
Simple factory
In this example, the factory will create two objects, TaxiCar and TruckCark, and you need to add two object creation methods to the project
public class SimpleFactory {
static TaxiCar makeTaxiCar(a){
return new TaxiCar();
}
static TruckCar makeTruckCar(a){
return new TruckCar();
}
public static void main(String[] args) { SimpleFactory.makeTaxiCar().drive(); SimpleFactory.makeTruckCar().drive(); }}class TaxiCar{
void drive(a){
System.out.println("Drive a cab."); }}class TruckCar{
void drive(a){
System.out.println("Drive a truck."); }}Copy the code
The abstract factory
An overview of the
The simple engineering pattern is too simple. If our business level is deep and requirements changes are complex, we should use abstract factories.
The abstract factory abstracts both the factory and the object being created using interfaces, and implements the concrete object creation logic using implementation classes.
Take the simple factory example above, if we have more cars to produce, such as electric cars, bicycles, trains!! There are even perverts asking us to make rockets and how do we deal with that.
Implementation analysis
For example, if a new demand requires us to build electric vehicles, bicycles, trains, planes and rockets, we can do as follows:
- Create the abstract factory class CarFactory
<T extent Car>
- Create abstract car
Car
- The concrete Car object implements the Car class, and the concrete Car factory implements the factory class CarFactory
code
In this example, only bicycles and trains are implemented
Several abstract classes
Abstract Factory class
public interface CarFactory<C extends Car> {
C makeTrafficTool(a);
}
Copy the code
Abstract vehicle classes
public interface Car {
void drive(a);
}
Copy the code
Abstract bike factory and abstract bike class
Abstract bicycle Factory
public class BikeCarFactory implements CarFactory<BikeCar>{
@Override
public BikeCar makeTrafficTool(a) {
return newBikeCar(); }}Copy the code
Abstract bicycle class
public class BikeCar implements Car{
@Override
public void drive(a) {
System.out.println("Ride a bike"); }}Copy the code
Abstract train factory and train class
Abstract train factory
public class TrainCarFactory implements CarFactory<TrainCar>{
@Override
public TrainCar makeTrafficTool(a) {
return newTrainCar(); }}Copy the code
Abstract train class
public class TrainCar implements Car{
@Override
public void drive(a) {
System.out.println("Drive a train."); }}Copy the code
Call code and run results
public class CarClient {
public static void main(String[] args) {
new BikeCarFactory().makeTrafficTool().drive();
newTrainCarFactory().makeTrafficTool().drive(); }}Copy the code
Factory mode in Android
BitMapFactory Static factory
A static factory is basically the same as a tool class, so I didn’t write it up front
BitmapFactory in Android is the static factory pattern used
For example decodeFile
public static Bitmap decodeFile(String pathName, Options opts) {* * *return bm;
}
Copy the code
Factories in Retrofit
The addCallAdapterFactory and addConverterFactory methods in the following code are factory method patterns. We will focus on the addCallAdapterFactory method.
Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory()
.build()
Copy the code
The addCallAdapterFactory method is used to create a CallAdapter object that can be used to build the request parameters.
The specific implementation is as follows:
The Retrofit class has a List< callAdapter.Factory > that holds Factory objects related to build request parameters.
When we call the addCallAdapterFactory method we add the factory to the list of factories,
When we call the Build method of the Retrofit.Builder class, we iterate through our factory list to get the CallAdapter object. Using the CallAdapter object, you can successfully retrieve the parameters associated with the request, as well as all the operations you intend to do before the request begins.