抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
介绍
- 意图:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
- 优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
- 缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。
实现
1. 创建一个动物接口
/**
* 动物接口
* @author remainsu
* @version 1.0 2019-06-12
*/
public interface Animal {
//一个声音的方法
void voice();
}
2. 创建多个不同的动物的实现类
/**
* 动物接口的实现类:猫
* @author remainsu
* @version 1.0 2019-06-12
*/
public class Cat implements Animal {
/**
* 重写Animal的voice()
* @see designPatterns.factory.Animal#voice()
*/
@Override
public void voice() {
System.out.println("This is the Cat Voice() !");
}
}
public class Dog implements Animal {
@Override
public void voice() {
System.out.println("This is the Dog Voice() !");
}
}
public class Bird implements Animal {
@Override
public void voice() {
System.out.println("This is the Bird Voice() !");
}
}
3. 创建一个颜色接口
/**
* 颜色接口
* @author remainsu
* @version 1.0 2019-06-12
*/
public interface Color {
//一个填充的方法
void fill();
}
4. 创建多个不同的颜色的实现类
/**
* 颜色接口的实现类:白色
* @author remainsu
* @version 1.0 2019-06-12
*/
public class White implements Color {
/**
* 重写Color的fill()
*/
@Override
public void fill() {
System.out.println("This is the White Fill() !");
}
}
public class Black implements Color {
@Override
public void fill() {
System.out.println("This is the Black Fill() !");
}
}
public class Brown implements Color {
@Override
public void fill() {
System.out.println("This is the Brown Fill() !");
}
}
5. 创建一个抽象工厂,并提供获取动物类、颜色类的方法
/**
* 抽象工厂
* @author remainsu
* @version 1.0 2019-06-12
*/
public abstract class AbstractFactory {
/**
* 创建动物
* @param animal
* @return
*/
public abstract Animal getAnimal(String animal);
/**
* 创建颜色
* @param color
* @return
*/
public abstract Color getColor(String color);
}
6. 创建动物工厂,并继承上面的抽象工厂
/**
* 动物工厂
* @author remainsu
* @version 1.0 2019-06-12
*/
public class AnimalFactory extends AbstractFactory {
/**
* 创建动物类
* @param type 动物类型
* @return 动物类
*/
public Animal getAnimal(String type) {
// 根据传入的参数,创建不同的动物类
if("cat".equals(type)) {
return new Cat();
} else if("dog".equals(type)) {
return new Dog();
} else if("bird".equals(type)) {
return new Bird();
} else {
return null;
}
}
@Override
public Color getColor(String color) {
return null;
}
}
7. 创建颜色工厂,并继承上面的抽象工厂
/**
* 颜色工厂
* @author remainsu
* @version 1.0 2019-06-12
*/
public class ColorFactory extends AbstractFactory {
/**
* 创建颜色类
* @param color 颜色
* @return 颜色类
*/
@Override
public Color getColor(String color) {
// 根据传入的参数,创建不同的颜色类
if("white".equals(color)) {
return new White();
} else if("black".equals(color)) {
return new Black();
} else if("brown".equals(color)) {
return new Brown();
} else {
return null;
}
}
@Override
public Animal getAnimal(String animal) {
return null;
}
}
8. 创建工厂生成器,用于根据不同的参数创建对应的工厂
/**
* 工厂生成器,通过传递动物或颜色信息来获取工厂
* @author remainsu
* @version 1.0 2019-06-12
*/
public class FactoryProducer {
/**
* 根据参数创建工厂
* @param type
* @return
*/
public static AbstractFactory getFactory(String type) {
if("animal".equals(type)) {
return new AnimalFactory();
} else if("color".equals(type)) {
return new ColorFactory();
} else {
return null;
}
}
}
9. 抽象工厂测试类
/**
* 抽象工厂测试类
* @author remainsu
* @version 1.0 2019-06-12
*/
public class Test {
public static void main(String[] args) {
/**
* 获取动物工厂
*/
AbstractFactory afa = FactoryProducer.getFactory("animal");
Animal a1 = afa.getAnimal("cat");
Animal a2 = afa.getAnimal("dog");
Animal a3 = afa.getAnimal("bird");
System.out.println("-------animal-------");
a1.voice();
a2.voice();
a3.voice();
/**
* 获取颜色工厂
*/
AbstractFactory afc = FactoryProducer.getFactory("color");
Color c1 = afc.getColor("white");
Color c2 = afc.getColor("black");
Color c3 = afc.getColor("brown");
System.out.println("-------color-------");
c1.fill();
c2.fill();
c3.fill();
}
}
输出结果:
-------animal-------
This is the Cat Voice() !
This is the Dog Voice() !
This is the Bird Voice() !
-------color-------
This is the White Fill() !
This is the Black Fill() !
This is the Brown Fill() !