工厂模式的第一种已经学习过了,但是我感觉(不用感觉,也确实是)简单工厂模式违反了开闭 原则。即如果工厂内的逻辑改动了,工厂类就得改,这样很不好(历史代码是不能动滴)。如果需要在方法里写很多与对象创建有关的业务代码,而且需要的创建的对象还不少的话,我们要在这个简单工厂类里编写很多个方法,每个方法里都得写很多相应的业务代码,而每次增加子类或者删除子类对象的创建都需要打开这简单工厂类来进行修改。这会导致这个简单工厂类很庞大臃肿、耦合性高,而且增加、删除某个子类对象的创建都需要打开简单工厂类来进行修改代码也违反了开-闭原则。这时候,工厂(方法)模式就出现了。
在简单工厂模式中,其实是一个工厂生产多个产品。但是在工厂方法模式中,每一个子类都对应着一个工厂类。所有的工厂类都实现了一个工厂接口或者抽象工厂类。这样,这相当于是把原本会因为业务代码而庞大的简单工厂类,拆分成了一个个的工厂类,这样代码就不会都耦合在同一个类里了。
我们先看产品接口的定义
package com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface;
public interface Animal {
void say();
}
然后看具体的产品
Dog
package com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface.impl;
import com.study.designPattern.factoryPattern.simapleFactoryPattern.factoryInterface.Animal;
public class Dog implements Animal {
@Override
public void say() {
System.out.println("我是一只小狗子,我说汪汪汪");
}
}
Cat
package com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface.impl;
import com.study.designPattern.factoryPattern.simapleFactoryPattern.factoryInterface.Animal;
public class Cat implements Animal {
@Override
public void say() {
System.out.println("我是一只小猫咪,我说喵喵喵");
}
}
Pig
package com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface.impl;
import com.study.designPattern.factoryPattern.simapleFactoryPattern.factoryInterface.Animal;
public class Pig implements Animal {
@Override
public void say() {
System.out.println("我是一个小猪猪,我说哼哼哼");
}
}
然后是工厂接口
package com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface;
import com.study.designPattern.factoryPattern.simapleFactoryPattern.factoryInterface.Animal;
public interface AnimalFactory {
Animal produceAnimal();
}
每一个产品都有自己的工厂,
Dog工厂:
package com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface.impl;
import com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface.AnimalFactory;
import com.study.designPattern.factoryPattern.simapleFactoryPattern.factoryInterface.Animal;
public class DogFactory implements AnimalFactory {
@Override
public Animal produceAnimal() {
return new Dog();
}
}
Cat工厂:
package com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface.impl;
import com.study.designPattern.factoryPattern.simapleFactoryPattern.factoryInterface.Animal;
public class Cat implements Animal {
@Override
public void say() {
System.out.println("我是一只小猫咪,我说喵喵喵");
}
}
Pig工厂:
package com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface.impl;
import com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface.AnimalFactory;
import com.study.designPattern.factoryPattern.simapleFactoryPattern.factoryInterface.Animal;
public class PigFactory implements AnimalFactory {
@Override
public Animal produceAnimal() {
return new Pig();
}
}
测试类
package com.study.designPattern.factoryPattern.factoryMethodPattern.demo;
import com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface.AnimalFactory;
import com.study.designPattern.factoryPattern.factoryMethodPattern.factoryInterface.impl.PigFactory;
import com.study.designPattern.factoryPattern.simapleFactoryPattern.factoryInterface.Animal;
public class Demo01 {
public static void main(String[] args) {
AnimalFactory pigFactory = new PigFactory();
Animal animal = pigFactory.produceAnimal();
animal.say();
}
}