橘子味的心
标题:Java抽象工厂模式

抽象工厂模式是一个超级工厂,用来创建其他工厂。 这个工厂也被称为工厂的工厂。 这种类型的设计模式属于创建模式,因为此模式提供了创建对象的最佳方法之一。

在抽象工厂模式中,接口负责创建相关对象的工厂,而不明确指定它们的类。 每个生成的工厂可以按照工厂模式提供对象。

实现实例

我们将创建一个ShapeColor接口并实现这些接口的具体类。在下一步中,将创建一个抽象工厂类AbstractFactory。在每个工厂类ShapeFactoryColorFactory定义都是扩展自AbstractFactory。创建工厂创建者/生成器类 - FactoryProducer

AbstractFactoryPatternDemo这是一个演示类,使用FactoryProducer来获取AbstractFactory对象。 它会将信息(CIRCLE / RECTANGLE / SQUARE)传递给AbstractFactory以获取所需的对象类型。 它还将信息(用于ColorRED / GREEN / BLUE)传递给AbstractFactory以获取所需的对象类型。

第1步

创建Shape的接口。

Shape.java

  1. public interface Shape {
  2. void draw();
  3. }
  4. Java

第2步

创建实现相同接口的具体类。

Rectangle.java

  1. public class Rectangle implements Shape {
  2.  
  3. @Override
  4. public void draw() {
  5. System.out.println("Inside Rectangle::draw() method.");
  6. }
  7. }
  8. Java

Square.java

  1. public class Square implements Shape {
  2.  
  3. @Override
  4. public void draw() {
  5. System.out.println("Inside Square::draw() method.");
  6. }
  7. }
  8. Java

Circle.java

  1. public class Circle implements Shape {
  2.  
  3. @Override
  4. public void draw() {
  5. System.out.println("Inside Circle::draw() method.");
  6. }
  7. }
  8. Java

第3步

创建一个Colors接口,如下所示

Color.java

  1. public interface Color {
  2. void fill();
  3. }
  4. Java

第4步

创建实现相同接口的具体类。

  1. public class Red implements Color {
  2.  
  3. @Override
  4. public void fill() {
  5. System.out.println("Inside Red::fill() method.");
  6. }
  7. }
  8. Java

Green.java

  1. public class Green implements Color {
  2.  
  3. @Override
  4. public void fill() {
  5. System.out.println("Inside Green::fill() method.");
  6. }
  7. }
  8. Java

Blue.java

  1. public class Blue implements Color {
  2.  
  3. @Override
  4. public void fill() {
  5. System.out.println("Inside Blue::fill() method.");
  6. }
  7. }
  8. Java

第5步

创建实现相同接口的具体类。

AbstractFactory.java

  1. public abstract class AbstractFactory {
  2. abstract Color getColor(String color);
  3. abstract Shape getShape(String shape) ;
  4. }
  5. Java

第6步

创建实现相同接口的具体类。

创建工厂类,根据给定信息扩展AbstractFactory以生成具体类的对象。

ShapeFactory.java

  1. public class ShapeFactory extends AbstractFactory {
  2.  
  3. @Override
  4. public Shape getShape(String shapeType){
  5.  
  6. if(shapeType == null){
  7. return null;
  8. }
  9.  
  10. if(shapeType.equalsIgnoreCase("CIRCLE")){
  11. return new Circle();
  12.  
  13. }else if(shapeType.equalsIgnoreCase("RECTANGLE")){
  14. return new Rectangle();
  15.  
  16. }else if(shapeType.equalsIgnoreCase("SQUARE")){
  17. return new Square();
  18. }
  19.  
  20. return null;
  21. }
  22.  
  23. @Override
  24. Color getColor(String color) {
  25. return null;
  26. }
  27. }
  28. Java

ColorFactory.java

  1. public class ColorFactory extends AbstractFactory {
  2.  
  3. @Override
  4. public Shape getShape(String shapeType){
  5. return null;
  6. }
  7.  
  8. @Override
  9. Color getColor(String color) {
  10.  
  11. if(color == null){
  12. return null;
  13. }
  14.  
  15. if(color.equalsIgnoreCase("RED")){
  16. return new Red();
  17.  
  18. }else if(color.equalsIgnoreCase("GREEN")){
  19. return new Green();
  20.  
  21. }else if(color.equalsIgnoreCase("BLUE")){
  22. return new Blue();
  23. }
  24.  
  25. return null;
  26. }
  27. }
  28. Java

第7步

创建工厂生成器/生产器类,通过传递如ShapeColor等信息来获取工厂

FactoryProducer.java

  1. public class FactoryProducer {
  2. public static AbstractFactory getFactory(String choice){
  3.  
  4. if(choice.equalsIgnoreCase("SHAPE")){
  5. return new ShapeFactory();
  6.  
  7. }else if(choice.equalsIgnoreCase("COLOR")){
  8. return new ColorFactory();
  9. }
  10.  
  11. return null;
  12. }
  13. }
  14. Java

第8步

使用FactoryProducer来获取AbstractFactory,以便通过传递类型等信息来获取具体类的工厂。

AbstractFactoryPatternDemo.java

  1. public class AbstractFactoryPatternDemo {
  2. public static void main(String[] args) {
  3.  
  4. //get shape factory
  5. AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
  6.  
  7. //get an object of Shape Circle
  8. Shape shape1 = shapeFactory.getShape("CIRCLE");
  9.  
  10. //call draw method of Shape Circle
  11. shape1.draw();
  12.  
  13. //get an object of Shape Rectangle
  14. Shape shape2 = shapeFactory.getShape("RECTANGLE");
  15.  
  16. //call draw method of Shape Rectangle
  17. shape2.draw();
  18.  
  19. //get an object of Shape Square
  20. Shape shape3 = shapeFactory.getShape("SQUARE");
  21.  
  22. //call draw method of Shape Square
  23. shape3.draw();
  24.  
  25. //get color factory
  26. AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
  27.  
  28. //get an object of Color Red
  29. Color color1 = colorFactory.getColor("RED");
  30.  
  31. //call fill method of Red
  32. color1.fill();
  33.  
  34. //get an object of Color Green
  35. Color color2 = colorFactory.getColor("Green");
  36.  
  37. //call fill method of Green
  38. color2.fill();
  39.  
  40. //get an object of Color Blue
  41. Color color3 = colorFactory.getColor("BLUE");
  42.  
  43. //call fill method of Color Blue
  44. color3.fill();
  45. }
  46. }
  47. Java

第9步

验证输出,结果如下 -

  1. Inside Circle::draw() method.
  2. Inside Rectangle::draw() method.
  3. Inside Square::draw() method.
  4. Inside Red::fill() method.
  5. Inside Green::fill() method.
  6. Inside Blue::fill() method.
  7. Java