抽象工厂模式
什么是抽象工厂模式
抽象工厂模式是围绕一个超级工厂创建其他工厂,超级工厂又称工厂的工厂,这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式
抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类
抽象工厂模式类图
1 package pattern; 2 3 public interface Shape { 4 void draw(); 5 }
1 package pattern; 2 3 public class Rectangle implements Shape { 4 @Override 5 public void draw() { 6 System.out.println("draw rectangle"); 7 } 8 }
1 package pattern; 2 3 public class Square implements Shape { 4 @Override 5 public void draw() { 6 System.out.println("draw square"); 7 } 8 }
1 package pattern; 2 3 public class Circle implements Shape { 4 @Override 5 public void draw() { 6 System.out.println("draw circle"); 7 } 8 }
1 package pattern; 2 3 public interface Color { 4 void fill(); 5 }
1 package pattern; 2 3 public class Red implements Color { 4 @Override 5 public void fill() { 6 System.out.println("fill red"); 7 } 8 }
1 package pattern; 2 3 public class Green implements Color { 4 @Override 5 public void fill() { 6 System.out.println("fill green"); 7 } 8 }
1 package pattern; 2 3 public class Blue implements Color { 4 @Override 5 public void fill() { 6 System.out.println("fill blue"); 7 } 8 }
1 package pattern; 2 3 public abstract class AbstractFactory { 4 public abstract Color getColor(String color); 5 6 public abstract Shape getShape(String shape); 7 }
1 package pattern; 2 3 public class ShapeFactory extends AbstractFactory { 4 @Override 5 public Color getColor(String color) { 6 return null; 7 } 8 9 @Override 10 public Shape getShape(String shape) { 11 if ("circle".equals(shape)) { 12 return new Circle(); 13 } else if ("rectangle".equals(shape)) { 14 return new Rectangle(); 15 } else if ("square".equals(shape)) { 16 return new Square(); 17 } 18 return null; 19 } 20 }
1 package pattern; 2 3 public class ColorFactory extends AbstractFactory { 4 @Override 5 public Color getColor(String color) { 6 if ("red".equals(color)) { 7 return new Red(); 8 } else if ("green".equals(color)) { 9 return new Green(); 10 } else if ("blue".equals(color)) { 11 return new Blue(); 12 } 13 return null; 14 } 15 16 @Override 17 public Shape getShape(String shape) { 18 return null; 19 } 20 }
1 package pattern; 2 3 public class FactoryProducer { 4 public static AbstractFactory getFactory(String choice) { 5 if ("shape".equals(choice)) { 6 return new ShapeFactory(); 7 } else if ("color".equals(choice)) { 8 return new ColorFactory(); 9 } 10 return null; 11 } 12 }
1 package pattern; 2 3 public class AbstractFactoryPatternDemo { 4 public static void main(String[] args) { 5 //获取形状工厂 6 AbstractFactory shapeFactory = FactoryProducer.getFactory("shape"); 7 8 Shape shape1 = shapeFactory.getShape("circle"); 9 shape1.draw(); 10 11 Shape shape2 = shapeFactory.getShape("rectangle"); 12 shape2.draw(); 13 14 Shape shape3 = shapeFactory.getShape("square"); 15 shape3.draw(); 16 17 //获取颜色工厂 18 AbstractFactory colorFactory = FactoryProducer.getFactory("color"); 19 20 Color color1 = colorFactory.getColor("red"); 21 color1.fill(); 22 23 Color color2 = colorFactory.getColor("green"); 24 color2.fill(); 25 26 Color color3 = colorFactory.getColor("blue"); 27 color3.fill(); 28 } 29 } 30 /* 31 draw circle 32 draw rectangle 33 draw square 34 fill red 35 fill green 36 fill blue 37 */