抽象工厂设计模式
程序员文章站
2022-03-09 20:35:56
...
抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
- 为形状创建一个接口
Shape.java
public interface Shape{
void draw();
}
- 创建实现接口的实体类
Rectangle.java
public class Rectangle implements Shape{
@Override
public void draw() {
System.out.println("Rectangle:draw()");
}
}
Square.java
public class Square implements Shape{
@Override
public void draw(){
System.out.println("Square:draw()");
}
}
Circle.java
public class Circle implements Shape{
@Override
public void draw(){
System.out.println("Circle:draw()");
}
}
- 为颜色创建一个接口
Color.java
public interface Color{
void fill();
}
- 创建实现接口的实体类
Red.java
public class Red implements Color{
@Override
public void fill(){
System.out.println("Red:fill()");
}
}
Green.java
public class Green implements Color{
@OVerride
public void fill(){
System.out.println("Green:fill()");
}
}
Blue.java
public class Blue implements Color{
@Override
public void fill(){
System.out.println("Blue:fill()");
}
}
- 为Color和Shape对象创建抽象类来获取工厂
AbstractFactory.java
public abstract class AbstractFactory{
public abstract Color getColor(String color);
public abstract Shape getShape(String shape);
}
- 创建扩展了AbstractFactory的工厂类。基于给定的信息生成实体类的对象
ShapeFactory.java
public class ShapeFactory extends AbstractFactory {
@Override
public Color getColor(String color) {
return null;
}
@Override
public Shape getShape(String shape) {
if(shape == null) {
return null;
}else 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;
}
}
ColorFactory.java
public class ColorFactory extends AbstractFactory {
@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;
}
}
- 创建一个工厂创建器生成器类。通过传递形状或颜色信息来获取工厂
FactoryProducer.java
public class FactoryProducer {
public static AbstractFactory getFactory(String choice) {
if(choice.equalsIgnoreCase("SHAPE")) {
return new ShapeFactory();
}else if(choice.equalsIgnoreCase("COLOR")) {
return new ColorFactory();
}
return null;
}
}
- 使用FactoryProducer来获取AbstractorFactory.通过传递类型信息来获取实体类的对象
AbstractFactoryPatternDemo.java
public class AbstractFactoryPatternDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
Shape shape1 = shapeFactory.getShape("CIRCLE");
shape1.draw();
Shape shape2 = shapeFactory.getShape("RECTANGLE");
shape2.draw();
Shape shape3 = shapeFactory.getShape("SQUARE");
shape3.draw();
AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
Color color1 = colorFactory.getColor("RED");
color1.fill();
Color color2 = colorFactory.getColor("GREEN");
color2.fill();
Color color3 = colorFactory.getColor("BLUE");
color3.fill();
}
}
执行结果:
Circle:draw()
Rectangle:draw()
Square:draw()
Curcle:draw()
Green:draw()
Blue:draw()
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。