A brief look at the concept of a simple factory pattern:
Simple factory pattern to create a schema belong to the class, also known as static factory method pattern; specifically defined by a class responsible for creating instances of other classes, instances are created usually have a common parent class
From the description we can extract some of the features:
- Each instance of a class having a common parent
- A particular class is responsible for creating instances of other classes
Now we come to realize through a factory pattern example: we now need a 水果工厂
?? To obtain, such as different fruits ...
First, start by creating one of them (?,?) Common parent classFruit
- Fruit of the parent class
Fruit.java
public abstract class Fruit {
abstract void get();
}
Second, now we create?,? Concrete implementation class
- ? Class implementation
Apple.java
public class Apple extends Fruit {
/**
* 获得苹果
*/
@Override
public void get() {
System.out.println("采集?");
}
}
- ? Class implementation
Banana.java
public class Banana extends Fruit {
/**
* 获得香蕉
*/
@Override
public void get() {
System.out.println("采集?");
}
}
Third, in general we are by ordinary new
to create an object, as shown below:
//创建苹果和香蕉的实例对象
Fruit apple = new Apple();
apple.get();
Fruit banana = new Banana();
banana.get();
Fourth, through the factory and now we need to create the object model to transform it to a
- We create a
FruitFactory.java
factory class (generally use the factory model class names are *** Factory.java)
public class FruitFactory {
/**
* 根据不同的类型创建不同的对象
*
* @param type
* @return
*/
public static Fruit getFruit(String type) {
switch (type) {
case "apple":
return new Apple();
case "banana":
return new Banana();
default:
System.out.println("找不到所匹配的对象!");
return null;
}
}
}
- Local calls can be simplified a little longer
Fruit apple = FruitFactory.getFruit("apple");
Fruit banana = FruitFactory.getFruit("banana");
apple.get();
banana.get();
The results of operation:
collection?
Collection?
Fifth, so that we achieve a simple plant, and can produce, fruit; but a closer look will find a problem:?? When we want to produce a new fruit such as: when, and so on ... we need to??? the plant's getFruit()
function transformation, as follows:
/**
* 根据不同的类型创建不同的对象
*
* @param type
* @return
*/
public static Fruit getFruit(String type) {
switch (type) {
case "apple":
return new Apple();
case "banana":
return new Banana();
//西瓜
case "watermelon":
return new Watermelon();
//菠萝
case "pineapple":
return new Pineapple();
case "peach":
//桃
return new Peach();
default:
System.out.println("找不到所匹配的对象!");
return null;
}
}
If we need to continue adding other fruit, so you have to constantly transform this function; Is there any way that we can further simplify here, the answer is yes as follows:
Sixth, to further simplify use reflection to create an object factory
FruitFactory.java
public class FruitFactory {
/**
* 使用反射
* 进一步简化工厂的创建
*
* @param type 这个参数就是需要创建的实例路径,eg:包名.类名
*/
public static Fruit getFruit2(String type) throws ClassNotFoundException,
IllegalAccessException, InstantiationException {
//加载类的实例
Class<?> name = Class.forName(type);
return (Fruit) name.newInstance();
}
}
Then place the call at the same need to follow to change together
//使用反射创建
try {
Fruit apple = FruitFactory.getFruit2(Apple.class.getName());
apple.get();
Fruit banana = FruitFactory.getFruit2(Banana.class.getName());
banana.get();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
e.printStackTrace();
}
It should be noted ⚠️ it, type the full path argument here must be the object to be created such as: com.xx.xx.Apple
, otherwise it will throwClassNotFoundException
Seven classes of objects created in this way by the need to constantly re-encounter when additional different objects, not in need of rehabilitation factory method to use again the only place for the incoming target line on the line, as follows:
Fruit watermelon = FruitFactory.getFruit2(Watermelon.class.getName());
Fruit pineapple = FruitFactory.getFruit2(Pineapple.class.getName());
Fruit peach = FruitFactory.getFruit2(Peach.class.getName());
//.....等等