Github:FactoryPattern.java

创建接口,定义公共方法

  1. /**
  2. * 公共接口
  3. */
  4. public interface Shape{
  5. void draw();
  6. }

创建接口实现类

矩形实现类

  1. /**
  2. * 矩形
  3. */
  4. public class Rectangle implements Shape{
  5. @Override
  6. public void draw() {
  7. System.out.println("this is Rectangle::draw method!");
  8. }
  9. }

圆形实现类

  1. /**
  2. * 圆形
  3. */
  4. public class Circle implements Shape{
  5. @Override
  6. public void draw() {
  7. System.out.println("this is Circle::draw method!");
  8. }
  9. }

正方形实现类

  1. /**
  2. * 正方形
  3. */
  4. public class Square implements Shape{
  5. @Override
  6. public void draw() {
  7. System.out.println("this is Square::draw method!");
  8. }
  9. }

定义形状枚举类

  1. /**
  2. * 定义可使用的类型,所有新的在此增加即可
  3. */
  4. public enum ShapeEnums{
  5. RECTANGLE, CIRCLE, SQUARE
  6. ;
  7. }

创建工厂类

  1. /**
  2. * 定义 shape工厂
  3. */
  4. public class ShapeFactory{
  5. private Shape shape;
  6. public ShapeFactory(ShapeEnums shape){
  7. switch (shape){
  8. case RECTANGLE: this.shape = new Rectangle(); break;
  9. case CIRCLE: this.shape = new Circle(); break;
  10. case SQUARE: this.shape = new Square(); break;
  11. default: throw new IllegalArgumentException("not found match construct method with : "+shape);
  12. }
  13. }
  14. public Shape getShape() {
  15. return shape;
  16. }
  17. }

测试,打印矩形

  1. public static void main(String[] args) {
  2. FactoryPattern factoryPattern = new FactoryPattern();
  3. factoryPattern.factory();
  4. }
  5. public void factory(){
  6. ShapeFactory factory = new ShapeFactory(ShapeEnums.CIRCLE);
  7. factory.getShape().draw();
  8. }

输出结果:

  1. this is Rectangle::draw method!

打印其他类型:
只需要创建工厂实例即可。

  1. public void factory(){
  2. // 矩形
  3. ShapeFactory factory = new ShapeFactory(ShapeEnums.RECTANGLE);
  4. factory.getShape().draw();
  5. // 圆形
  6. factory = new ShapeFactory(ShapeEnums.CIRCLE);
  7. factory.getShape().draw();
  8. // 正方形
  9. factory = new ShapeFactory(ShapeEnums.SQUARE);
  10. factory.getShape().draw();
  11. }

输出结果:

  1. this is Rectangle::draw method!
  2. this is Circle::draw method!
  3. this is Square::draw method!

如何新增其他类型

1. 在枚举类新增指定类型

比如新增菱形

  1. /**
  2. * 定义可使用的类型,所有新的在此增加即可
  3. */
  4. public enum ShapeEnums{
  5. RECTANGLE, CIRCLE, SQUARE, RHOMBUS
  6. ;
  7. }

2. 新增菱形实现类

  1. /**
  2. * 菱形
  3. */
  4. public class Rhombus implements Shape{
  5. @Override
  6. public void draw() {
  7. System.out.println("this is Rhombus::draw method!");
  8. }
  9. }

3. 修改工厂类,新增菱形实例化

  1. /**
  2. * 定义 shape工厂
  3. */
  4. public class ShapeFactory{
  5. private Shape shape;
  6. public ShapeFactory(ShapeEnums shape){
  7. switch (shape){
  8. case RECTANGLE: this.shape = new Rectangle(); break;
  9. case CIRCLE: this.shape = new Circle(); break;
  10. case SQUARE: this.shape = new Square(); break;
  11. case RHOMBUS: this.shape = new Rhombus(); break;
  12. default: throw new IllegalArgumentException("not found match construct method with : "+shape);
  13. }
  14. }
  15. public Shape getShape() {
  16. return shape;
  17. }
  18. }

4. 测试

  1. public void factory(){
  2. // 矩形
  3. ShapeFactory factory = new ShapeFactory(ShapeEnums.RECTANGLE);
  4. factory.getShape().draw();
  5. // 圆形
  6. factory = new ShapeFactory(ShapeEnums.CIRCLE);
  7. factory.getShape().draw();
  8. // 正方形
  9. factory = new ShapeFactory(ShapeEnums.SQUARE);
  10. factory.getShape().draw();
  11. // 菱形
  12. factory = new ShapeFactory(ShapeEnums.RHOMBUS);
  13. factory.getShape().draw();
  14. }

输出结果:此时会发现多了打印菱形结果

  1. this is Rectangle::draw method!
  2. this is Circle::draw method!
  3. this is Square::draw method!
  4. this is Rhombus::draw method!

参考

菜鸟教程 - 工厂模式