创建型模式-抽象工厂模式
介绍
抽象工厂模式:提供一个创建一系列相关或者相互依赖对象的接口,而无需指定它们具体的类。
实例
- 创建一个形状接口 - 1 
 2
 3- public interface Shape { 
 void draw();
 }
- 创建实现形状接口的实体类 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23- public class Rectangle implements Shape { 
 
 
 public void draw() {
 System.out.println("Inside Rectangle::draw() method.");
 }
 }
 public class Square implements Shape {
 
 
 public void draw() {
 System.out.println("Inside Square::draw() method.");
 }
 }
 public class Circle implements Shape {
 
 
 public void draw() {
 System.out.println("Inside Circle::draw() method.");
 }
 }
- 创建一个颜色接口 - 1 
 2
 3- public interface Color { 
 void fill();
 }
- 创建实现颜色接口的实体类 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23- public class Red implements Color { 
 
 
 public void fill() {
 System.out.println("Inside Red::fill() method.");
 }
 }
 public class Green implements Color {
 
 
 public void fill() {
 System.out.println("Inside Green::fill() method.");
 }
 }
 public class Blue implements Color {
 
 
 public void fill() {
 System.out.println("Inside Blue::fill() method.");
 }
 }
- 为颜色和形状对象创建抽象类来获取工厂* - 1 
 2
 3
 4- public abstract class AbstractFactory { 
 public abstract Color getColor(String color);
 public abstract Shape getShape(String shape) ;
 }
- 创建扩展了抽象工厂类的工厂类,基于给定信息生成实体类的对象 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45- public class ShapeFactory extends AbstractFactory { 
 
 
 public Shape getShape(String shapeType){
 if(shapeType == null){
 return null;
 }
 if(shapeType.equalsIgnoreCase("CIRCLE")){
 return new Circle();
 } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
 return new Rectangle();
 } else if(shapeType.equalsIgnoreCase("SQUARE")){
 return new Square();
 }
 return null;
 }
 
 
 public Color getColor(String color) {
 return null;
 }
 }
 public class ColorFactory extends AbstractFactory {
 
 
 public Shape getShape(String shapeType){
 return null;
 }
 
 
 Color getColor(String color) {
 if(color == null){
 return null;
 }
 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;
 }
 }
- 创建一个工厂生成器,通过传递形状或者信息来获取工厂 - 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22- public class ColorFactory extends AbstractFactory { 
 
 
 public Shape getShape(String shapeType){
 return null;
 }
 
 
 Color getColor(String color) {
 if(color == null){
 return null;
 }
 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;
 }
 }
- 使用 - FactoryProducer来获取- AbstractFactory,通过传递类型信息来获取实体类的对象- 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46- public class AbstractFactoryPatternDemo { 
 public static void main(String[] args) {
 
 //获取形状工厂
 AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
 
 //获取形状为 Circle 的对象
 Shape shape1 = shapeFactory.getShape("CIRCLE");
 
 //调用 Circle 的 draw 方法
 shape1.draw();
 
 //获取形状为 Rectangle 的对象
 Shape shape2 = shapeFactory.getShape("RECTANGLE");
 
 //调用 Rectangle 的 draw 方法
 shape2.draw();
 
 //获取形状为 Square 的对象
 Shape shape3 = shapeFactory.getShape("SQUARE");
 
 //调用 Square 的 draw 方法
 shape3.draw();
 
 //获取颜色工厂
 AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
 
 //获取颜色为 Red 的对象
 Color color1 = colorFactory.getColor("RED");
 
 //调用 Red 的 fill 方法
 color1.fill();
 
 //获取颜色为 Green 的对象
 Color color2 = colorFactory.getColor("Green");
 
 //调用 Green 的 fill 方法
 color2.fill();
 
 //获取颜色为 Blue 的对象
 Color color3 = colorFactory.getColor("BLUE");
 
 //调用 Blue 的 fill 方法
 color3.fill();
 }
 }
- 输出结果 - 1 
 2
 3
 4
 5
 6- Inside Circle::draw() method. 
 Inside Rectangle::draw() method.
 Inside Square::draw() method.
 Inside Red::fill() method.
 Inside Green::fill() method.
 Inside Blue::fill() method.
总结
抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类。
