抽象模式(Abstract Factory Pattern) – 设计模式之创建模式:
目录
抽象工厂创建类 ShapeColorAbstractFactoryProducer
抽象工厂模式
定义:
定义: Provide an interface for creating families of related or dependent objects whithout specifying their concrete classes.
(为创建一组相关或相互依赖的对象提供一个接口,而且无须指定它们的具体类。)
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
理解抽象工厂模式,需先理解工厂模式,可看 工厂模式
A,类图
通用工厂模式类图:
例子:
过程:
画图,需要画形状和涂色,两种不同类型的内容
类图:
抽象工厂类:
public abstract class ShapeColorAbstractFactory {
public abstract Color getColors(String name);
public abstract Shape getShapes(String name) ;
}
形状工厂:
形状:Shape
public interface Shape {
void draw();
}
圆形: Circle
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Useing Circle::draw() method.");
}
}
三角形: Triangle
public class Triangle implements Shape {
@Override
public void draw() {
System.out.println("Useing Triangle::draw() method.");
}
}
矩形: Rectangle
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Useing Rectangle::draw() method.");
}
}
形状枚举类: ShapeTypeEnum
public enum ShapeTypeEnum {
CIRCLE("circle",Circle::new),
RECTANGEL("rectangle",Rectangle::new),
TRIANGLE("triangle",Triangle::new);
private String name;
public String getName() {
return name;
}
private Supplier<Shape> supplier;
public Supplier<Shape> getSupplier() {
return supplier;
}
ShapeTypeEnum(String name, Supplier<Shape> supplier) {
this.name = name;
this.supplier = supplier;
}
public static Shape getShape(String name){
Shape shape = Arrays.stream(ShapeTypeEnum.values())
.filter(shapeTypeEnum -> shapeTypeEnum.getName().equals(name))
.map(ShapeTypeEnum::getSupplier)
.map(Supplier::get)
.findFirst().orElse(null);
return shape;
}
}
形状工厂类: ShapeFactory
这边添加了静态类,获取单例的内容
public class ShapeFactory extends ShapeColorAbstractFactory {
public static class Holder {
private static ShapeFactory instance = new ShapeFactory();
}
public static ShapeFactory getInstance() {
return Holder.instance;
}
@Override
public Color getColors(String color) {
return null;
}
@Override
public Shape getShapes(String name) {
return ShapeTypeEnum.getShape(name);
}
}
测试类;
public class ShapeFactoryTest {
public static void main(String[] args) {
ShapeFactory shapeFactory = ShapeFactory.getInstance();
Shape shape3 = shapeFactory.getShapes(ShapeTypeEnum.CIRCLE.getName());
Shape shape4 = shapeFactory.getShapes("rectangle");
Shape shape5 = shapeFactory.getShapes("triangle");
shape3.draw();
shape4.draw();
shape5.draw();
}
}
结果:
Useing Circle::draw() method.
Useing Rectangle::draw() method.
Useing Triangle::draw() method.
颜色工厂:
形状:Color
public interface Color {
void fill();
}
蓝色:Blue
public class Blue implements Color {
@Override
public void fill() {
System.out.println("Using Blue::fill() method.");
}
}
橘色: Orange
public class Orange implements Color {
@Override
public void fill() {
System.out.println("Using Orange::fill() method.");
}
}
粉红色:Pink
public class Rectangle implements Shape {
static final String DESCRIPTION = "This is Rectangle.";
@Override
public String getDescription() {
return DESCRIPTION;
}
}
颜色枚举类: ShapeTypeEnum
public enum ColorEnum {
BLUE("blue",Blue::new),
PINK("pink",Pink::new),
ORANGLE("orangle",Orange::new);
private String name;
public String getName() {
return name;
}
private Supplier<Color> supplier;
public Supplier<Color> getSupplier() {
return supplier;
}
ColorEnum(String name, Supplier<Color> supplier) {
this.name = name;
this.supplier = supplier;
}
public static Color getColor(String name){
Color color = Arrays.stream(ColorEnum.values())
.filter(shapeTypeEnum -> shapeTypeEnum.getName().equals(name))
.map(ColorEnum::getSupplier)
.map(Supplier::get)
.findFirst().orElse(null);
return color;
}
}
颜色工厂类: ShapeFactory
public class ShapeFactory extends ShapeColorAbstractFactory {
public static class Holder {
private static ShapeFactory instance = new ShapeFactory();
}
public static ShapeFactory getInstance() {
return Holder.instance;
}
@Override
public Color getColors(String color) {
return null;
}
@Override
public Shape getShapes(String name) {
return ShapeTypeEnum.getShape(name);
}
}
测试类;
public class ColorFactoryTest {
public static void main(String[] args) {
ColorFactory colorFactory = ColorFactory.getInstance();
Color color3 = colorFactory.getColors(ColorEnum.BLUE.getName());
Color color4 = colorFactory.getColors("pink");
Color color5 = colorFactory.getColors("orangle");
color3.fill();
color4.fill();
color5.fill();
}
}
结果:
Using Blue::fill() method.
Using Pink ::fill() method.
Using Orange::fill() method.
抽象工厂:
抽象工厂创建类 ShapeColorAbstractFactoryProducer
public class ShapeColorAbstractFactoryProducer {
// 这边简单生成,也可以用Enum 如工厂的方式
public static ShapeColorAbstractFactory getFactory(String choice){
if(choice.equalsIgnoreCase("SHAPE")){
return ShapeFactory.getInstance();
} else if(choice.equalsIgnoreCase("COLOR")){
return ColorFactory.getInstance();
}
return null;
}
}
测试类;
public class ShapeColorAbstractFactoryTest {
public static void main(String[] args) {
//获取形状工厂
ShapeColorAbstractFactory shapeFactory =
ShapeColorAbstractFactoryProducer.getFactory("SHAPE");
Shape circle = shapeFactory.getShapes("circle");
circle.draw();
//获取颜色工厂
ShapeColorAbstractFactory colorFactory = ShapeColorAbstractFactoryProducer.getFactory("COLOR");
Color blue = colorFactory.getColors("blue");
blue.fill();
}
}
结果:
Useing Circle::draw() method.
Using Blue::fill() method.
总结:
优点: 当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
缺点: 产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。
理解了工厂方法,再理解抽象工厂方法就简单多了,对工厂方法进一步抽象化。