关于Java 设计模式 抽象工厂模式 Abstract Factory

关于Java 设计模式 抽象工厂模式 Abstract Factory

1、Shape.java

package com.ssm.cts.pattern.abstractfactory;

public interface Shape {
  void draw();
}

2、Circle.java

package com.ssm.cts.pattern.abstractfactory;

public class Circle implements Shape {

  @Override
  public void draw() {
    System.out.println("圆形");
  }

}

3、Square.java

package com.ssm.cts.pattern.abstractfactory;

public class Square implements Shape {

  @Override
  public void draw() {
    System.out.println("方形");
  }

}

4、Color.java

package com.ssm.cts.pattern.abstractfactory;

public interface Color {
  void fill();
}

5、Green.java

package com.ssm.cts.pattern.abstractfactory;

public class Green implements Color {

  @Override
  public void fill() {
    System.out.println("绿色");
  }

}

6、Red.java

package com.ssm.cts.pattern.abstractfactory;

public class Red implements Color {
  @Override
  public void fill() {
    System.out.println("红色");
  }
}

7、AbstractFactory.java

package com.ssm.cts.pattern.abstractfactory;

public abstract class AbstractFactory {
  public abstract Color getColor(String color);

  public abstract Shape getShape(String shape);
}

8、ColorFactory.java

package com.ssm.cts.pattern.abstractfactory;

public class ColorFactory extends AbstractFactory {

  @Override
  public Color getColor(String color) {
    if (color == null) {
      return null;
    }
    if (color.equalsIgnoreCase("RED")) {
      return new Red();
    } else if (color.equalsIgnoreCase("GREEN")) {
      return new Green();
    }
    return null;
  }

  @Override
  public Shape getShape(String shape) {
    // TODO Auto-generated method stub
    return null;
  }

}

9、ShapeFactory.java

package com.ssm.cts.pattern.abstractfactory;

public class ShapeFactory extends AbstractFactory {

  @Override
  public Color getColor(String color) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Shape getShape(String shape) {
    if (shape == null) {
      return null;
    }
    if (shape.equalsIgnoreCase("CIRCLE")) {
      return new Circle();
    } else if (shape.equalsIgnoreCase("SQUARE")) {
      return new Square();
    }
    return null;
  }

}

10、FactoryProducer.java

package com.ssm.cts.pattern.abstractfactory;

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;
  }
}

11、Demo.java

package com.ssm.cts.pattern.abstractfactory;

public class Demo {
  // 测试
  public static void main(String[] args) {

    // 获取形状工厂
    AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

    // 获取形状为 Circle的对象
    Shape shape1 = shapeFactory.getShape("CIRCLE");

    // 调用 Circle的 draw方法
    shape1.draw();

    // 获取颜色工厂
    AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

    // 获取颜色为 Red的对象
    Color color1 = colorFactory.getColor("RED");

    // 调用Red的 fill方法
    color1.fill();
  }
}

 

发表回复

您的电子邮箱地址不会被公开。