1. Definitions
For creating a definition of the object 接口
, so 子类
decide which instance of a class. The method of making a factory to instantiate the class to delay its subclasses.
2, for example
There is a Animal
port, there are two classes Dog
and Cat
are inherited interface. We called a AnimalDactory
factory class interface, and then define DogFactory
and CatFactory
class to create, respectively, Dog
and Cat
instances, by the calling terminal to decide which plants to create objects.
3, Code
// 创建 动物 接口
public interface Animal {
void sayName();
}
// 创建 猫和狗 类
public class Cat implements Animal {
@Override
public void sayName() {
System.out.println("我是猫!");
}
}
public class Dog implements Animal {
@Override
public void sayName() {
System.out.println("我是狗!");
}
}
// 创建工厂接口类
public interface IAnimalFactory {
Animal createAnimal();
}
// 分别创建生成猫和狗 的工厂类
public class CatFactory implements IAnimalFactory{
@Override
public Animal createAnimal() {
return new Cat();
}
}
public class DogFactory implements IAnimalFactory{
@Override
public Animal createAnimal() {
return new Dog();
}
}
// 客户端使用工厂类来创建动物对象
public class Main {
public static void main(String[] args) {
// 分别获得生产猫和狗的工厂类
IAnimalFactory catFacroty = new CatFactory();
IAnimalFactory dogFacroty = new DogFactory();
Animal cat = catFacroty.createAnimal();
Animal dog = dogFacroty.createAnimal();
cat.sayName();
dog.sayName();
}
}
Here is the result of the operation:
我是猫!
我是狗!
4, the advantages
Factory pattern into 简单工厂模式
and 工厂方法模式
. 简单方法模式
The factory methods is generally static, it will not have an interface, and can create multiple instances of a plant , as follows:
// 简单工厂模式的工厂
public class AnimalFactory implements IAnimalFactory{
@Override
public Animal createAnimal(String name) {
// 通过 if 判断,生成多中不同的实例
if("cat".equals(name)){
return new Cat();
}
if("dog".equals(name)){
return new Dog();
}
throw new RuntimeException("无此类动物:" + name);
}
}
In 工厂方法模式
the, there is the factory interface, and for different instances, have a plurality of factory class.
We see here will find, 简单工厂模式
than 工厂方法模式
it is, more concise (because there is no more factory class). However, in 简单工厂模
the formula, the factory class contains a decision logic, if we want to add a new Animal class, you need to modify the method of the original factory class, so that we not only to expand open for modification also open , contrary to the open - closed principle . And if you use 工厂方法
mode, we only need to add a new plant will be able to achieve functional expansion, without going to modify the original code.
Basically, these two models are judged problem exists. 简单工厂模式
Is to determine the logic into a factory method, 工厂方法模式
put the call on decision logic end, by the calling terminal to determine the use to which factory class, so when you want to add new features, just need to modify the call ends on the line, rather than the factory class .
5, application scenarios
- Factory mode shield internal details of the class is created to reduce the coupling between the calling class.
- If the process to create a more complex class using the class factory can effectively reduce the complexity of the system.
- Rely on abstract factory pattern framework, it has good scalability.