一、Factory Method设计模式介绍
Factory Method设计模式是一种创建型设计模式,它的主要目的是为了将对象的创建过程从主要业务逻辑中抽离出来,从而提高代码的可维护性和可扩展性。在使用Factory Method模式时,我们将对象的创建交给了Factory Method,而业务逻辑的代码只需要调用Factory Method提供的接口即可获得对象。 Factory Method模式的核心是一个接口(或抽象类),用于规范所有具体工厂类所需要实现的方法。具体工厂类实现该接口,生产出具体的产品对象。
二、Factory Method设计模式的实现
1、简单工厂模式
在简单工厂模式中,我们需要一个根据传入参数动态创建对象的工厂类。对于每一个需要创建的对象,我们都需要手动添加对应的创建方法。当需要创建新的对象时,我们不得不修改工厂类的内部逻辑。这种方式不仅不符合开闭原则,而且增加了工厂类的负担。
interface Fruit {
void produce();
}
class Apple implements Fruit {
public void produce() {
System.out.println("产生了一个苹果!");
}
}
class Strawberry implements Fruit {
public void produce() {
System.out.println("产生了一个草莓!");
}
}
class FruitFactory {
public Fruit createFruit(String type) {
if("apple".equals(type)) {
return new Apple();
} else if("strawberry".equals(type)) {
return new Strawberry();
} else {
return null;
}
}
}
public class SimpleFactoryTest {
public static void main(String[] args) {
FruitFactory factory = new FruitFactory();
Fruit apple = factory.createFruit("apple");
Fruit strawberry = factory.createFruit("strawberry");
apple.produce();
strawberry.produce();
}
}
2、工厂方法模式
工厂方法模式是对简单工厂模式的改进,在工厂方法模式中,我们将创建一个对象的方法抽象出来,在工厂抽象类中定义该方法的接口,具体工厂类实现这个方法来创建具体的产品对象。使用工厂方法模式时,我们可以通过扩展工厂抽象类和具体工厂类来创建新的对象,而无需修改已有的代码。
interface Fruit {
void produce();
}
class Apple implements Fruit {
public void produce() {
System.out.println("产生了一个苹果!");
}
}
class Strawberry implements Fruit {
public void produce() {
System.out.println("产生了一个草莓!");
}
}
interface FruitFactory {
Fruit createFruit();
}
class AppleFactory implements FruitFactory {
public Fruit createFruit() {
return new Apple();
}
}
class StrawberryFactory implements FruitFactory {
public Fruit createFruit() {
return new Strawberry();
}
}
public class FactoryMethodTest {
public static void main(String[] args) {
FruitFactory appleFactory = new AppleFactory();
Fruit apple = appleFactory.createFruit();
apple.produce();
FruitFactory strawberryFactory = new StrawberryFactory();
Fruit strawberry = strawberryFactory.createFruit();
strawberry.produce();
}
}
3、抽象工厂模式
抽象工厂模式是进一步的改进,它解决了工厂方法模式内部只能有一种产品类型的问题。在抽象工厂模式中,我们将多个工厂类抽象出来,每个工厂类负责创建一系列相关联的产品。这样,我们就可以生产不同的产品族并且可以保证一组产品一起生产,从而保证产品的兼容性。
interface Fruit {
void produce();
}
interface Cake {
void make();
}
class Apple implements Fruit {
public void produce() {
System.out.println("产生了一个苹果!");
}
}
class Strawberry implements Fruit {
public void produce() {
System.out.println("产生了一个草莓!");
}
}
class Mousse implements Cake {
public void make() {
System.out.println("制作了一份慕斯蛋糕!");
}
}
class Cheesecake implements Cake {
public void make() {
System.out.println("制作了一份芝士蛋糕!");
}
}
interface FruitFactory {
Fruit createFruit();
}
interface CakeFactory {
Cake createCake();
}
class AppleFactory implements FruitFactory {
public Fruit createFruit() {
return new Apple();
}
}
class StrawberryFactory implements FruitFactory {
public Fruit createFruit() {
return new Strawberry();
}
}
class MousseFactory implements CakeFactory {
public Cake createCake() {
return new Mousse();
}
}
class CheesecakeFactory implements CakeFactory {
public Cake createCake() {
return new Cheesecake();
}
}
interface AbstractFactory {
FruitFactory createFruitFactory();
CakeFactory createCakeFactory();
}
class SpringFactory implements AbstractFactory {
public FruitFactory createFruitFactory() {
return new AppleFactory();
}
public CakeFactory createCakeFactory() {
return new MousseFactory();
}
}
class SummerFactory implements AbstractFactory {
public FruitFactory createFruitFactory() {
return new StrawberryFactory();
}
public CakeFactory createCakeFactory() {
return new CheesecakeFactory();
}
}
public class AbstractFactoryTest {
public static void main(String[] args) {
AbstractFactory factory;
Fruit fruit;
Cake cake;
factory = new SpringFactory();
fruit = factory.createFruitFactory().createFruit();
cake = factory.createCakeFactory().createCake();
fruit.produce();
cake.make();
factory = new SummerFactory();
fruit = factory.createFruitFactory().createFruit();
cake = factory.createCakeFactory().createCake();
fruit.produce();
cake.make();
}
}
三、结语
Factory Method模式是一种非常重要的设计模式,它可以帮助我们将对象的创建过程抽离出来,并实现业务逻辑和对象创建的松耦合。Factory Method模式有多种实现方式,每一种实现方式都有自己的特点和局限性,我们可以根据需要选择合适的实现方式。