抽象工厂模式,属于创造型模式。
描述
抽象工厂模式就像是一个大公司,其有很多小公司,然后各个小公司各司其职。
代码
接口
形状接口
package com.elaine.testpattern.abstractfactory;
/**
* 形状接口
*
* @author elaine
* @date 2020/7/27
*/
public interface Shape {
void draw();
}
颜色接口
package com.elaine.testpattern.abstractfactory;
/**
* 颜色接口
*
* @author elaine
* @date 2020/7/27
*/
public interface Color {
void fill();
}
形状对象
建立形状具体对象,继承形状接口,重写绘制方法。
圆形
package com.elaine.testpattern.abstractfactory;
/**
* 圆形
*
* @author elaine
* @date 2020/7/27
*/
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
长方形
package com.elaine.testpattern.abstractfactory;
/**
* 长方形
*
* @author elaine
* @date 2020/7/27
*/
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
正方形
package com.elaine.testpattern.abstractfactory;
/**
* 正方形
*
* @author elaine
* @date 2020/7/27
*/
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
颜色对象
建立具体颜色实体,继承颜色接口,重写填充方法。
红色
package com.elaine.testpattern.abstractfactory;
/**
* 红色
*
* @author elaine
* @date 2020/7/27
*/
public class Red implements Color {
@Override
public void fill() {
System.out.println("Inside Red::fill() method.");
}
}
绿色
package com.elaine.testpattern.abstractfactory;
/**
* 绿色
*
* @author elaine
* @date 2020/7/27
*/
public class Green implements Color {
@Override
public void fill() {
System.out.println("Inside Green::fill() method.");
}
}
蓝色
package com.elaine.testpattern.abstractfactory;
/**
* 蓝色
*
* @author elaine
* @date 2020/7/27
*/
public class Blue implements Color {
@Override
public void fill() {
System.out.println("Inside Blue::fill() method.");
}
}
抽象工厂
建立一个抽象工厂,内有两个抽象
方法。\
package com.elaine.testpattern.abstractfactory;
/**
* 抽象工厂
*
* @author elaine
* @date 2020/7/27
*/
public abstract class AbstractFactory {
/**
* 获取颜色
*
* @param color 颜色
* @return Color
*/
public abstract Color getColor(String color);
/**
* 获取形状
*
* @param shape 形状
* @return Shape
*/
public abstract Shape getShape(String shape);
}
工厂具体类
形状工厂
package com.elaine.testpattern.abstractfactory;
/**
* 形状工厂
* 继承抽象工厂
*
* @author elaine
* @date 2020/7/27
*/
public class ShapeFactory extends AbstractFactory {
@Override
public Color getColor(String color) {
return null;
}
/**
* 根据传入的形状,创建具体的形状对象
*
* @param shape 形状
* @return Shape
*/
@Override
public Shape getShape(String shape) {
if (shape == null) {
return null;
}
if (shape.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shape.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
} else if (shape.equalsIgnoreCase("SQUARE")) {
return new Square();
}
return null;
}
}
颜色工厂
package com.elaine.testpattern.abstractfactory;
/**
* 颜色工厂
* 继承抽象工厂
*
* @author elaine
* @date 2020/7/27
*/
public class ColorFactory extends AbstractFactory {
/**
* 根据传入的颜色,创建具体的颜色实体
*
* @param color 颜色
* @return Color
*/
@Override
public Color getColor(String color) {
if (color == null) {
return null;
} else if (color.equalsIgnoreCase("RED")) {
return new Red();
} else if (color.equalsIgnoreCase("GREEN")) {
return new Green();
} else if (color.equalsIgnoreCase("BLUE")) {
return new Blue();
}
return null;
}
@Override
public Shape getShape(String shape) {
return null;
}
}
工厂生产类
package com.elaine.testpattern.abstractfactory;
/**
* 工厂生产类
*
* @author elaine
* @date 2020/7/27
*/
public class FactoryProducer {
/**
* 根据传入的选择,实现具体的工厂
*
* @param choice 类型
* @return 具体的工厂
*/
public static AbstractFactory getFactory(String choice) {
if (choice.equalsIgnoreCase("SHAPE")) {
return new ShapeFactory();
} else if (choice.equalsIgnoreCase("COLOR")) {
return new ColorFactory();
}
return null;
}
}
实现类
private void use() {
//创建抽象工厂,传入约定的值,得到形状工厂
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
//传入形状的约定值,得到各类形状,就可以调用绘制方法
Shape circle = shapeFactory.getShape("CIRCLE");
circle.draw();
Shape rectangle = shapeFactory.getShape("RECTANGLE");
rectangle.draw();
Shape square = shapeFactory.getShape("SQUARE");
square.draw();
//创建抽象工厂,传入约定的值,得到颜色工厂
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
//传入颜色的约定值,得到各类颜色,就可以调用填充方法
Color red = colorFactory.getColor("RED");
red.fill();
Color green = colorFactory.getColor("GREEN");
green.fill();
Color blue = colorFactory.getColor("BLUE");
blue.fill();
}