设计模式实现:工厂方法与抽象工厂的应用实例

原创 你的名字 2024-12-12 13:33 96阅读 0赞

工厂方法(Factory Method)和抽象工厂(Abstract Factory)是面向对象编程中两种常用的工厂模式。

工厂方法(Factory Method)实例:

  1. 简单工厂模式:

    1. // 工厂接口
    2. public interface ShapeFactory {
    3. Shape createShape(String shapeType);
    4. }
    5. // 简单工厂实现
    6. class SimpleShapeFactory implements ShapeFactory {
    7. @Override
    8. public Shape createShape(String shapeType) {
    9. if (shapeType.equalsIgnoreCase("Circle")) {
    10. return new Circle();
    11. } else if (shapeType.equalsIgnoreCase("Rectangle")) {
    12. return new Rectangle();
    13. }
    14. return null; //非法形状类型
    15. }
    16. }
    17. // 使用工厂创建形状实例
    18. public class ShapeDemo {
    19. public static void main(String[] args) {
    20. ShapeFactory shapeFactory = new SimpleShapeFactory();
    21. Shape circle = shapeFactory.createShape("Circle");
    22. if (circle != null) {
    23. System.out.println("Created Circle: " + circle);
    24. }
    25. Shape rectangle = shapeFactory.createShape("Rectangle");
    26. if (rectangle != null) {
    27. System.out.println("Created Rectangle: " + rectangle);
    28. }
    29. }
  2. 工厂方法的实现,可能在一个具体的类中:
    ```java
    // 具体工厂类
    class CircleFactory extends ShapeFactory {
    @Override
    public Shape createShape(String shapeType) {
    1. if (shapeType.equalsIgnoreCase("Circle")) {
    2. return new Circle();
    3. }
    4. return null; //非法形状类型
    }
    }

// 使用工厂创建具体形状实例
public class ShapeDemo {
public static void main(String[] args) {
ShapeFactory shapeFactory = new CircleFactory();

  1. Shape circle = shapeFactory.createShape("Circle");
  2. if (circle != null) {
  3. System.out.println("Created Circle: " + circle);
  4. }
  5. // 如果需要创建矩形,可以使用相同工厂
  6. Shape rectangle = shapeFactory.createShape("Rectangle");
  7. if (rectangle != null) {
  8. System.out.println("Created Rectangle: " + rectangle);
  9. }
  10. }

}

  1. **抽象工厂(Abstract Factory)实例:**
  2. 1. **多个具体工厂类的集合:**
  3. ```java
  4. // 抽象工厂接口
  5. public interface ShapeFactoryInterface {
  6. Shape createShape(String shapeType);
  7. }
  8. // 抽象工厂实现
  9. abstract class AbstractShapeFactory implements ShapeFactoryInterface {
  10. @Override
  11. public Shape createShape(String shapeType) {
  12. // 实现不同形状的具体创建逻辑
  13. if (shapeType.equalsIgnoreCase("Circle")) {
  14. return new Circle();
  15. } else if (shapeType.equalsIgnoreCase("Rectangle")) {
  16. return new Rectangle();
  17. }
  18. return null; //非法形状类型
  19. }
  20. }
  1. 使用抽象工厂创建具体形状实例:

    1. // 使用抽象工厂创建具体形状实例
    2. public class ShapeDemo {
    3. public static void main(String[] args) {
    4. ShapeFactoryInterface shapeFactory = new AbstractShapeFactory();
    5. Shape circle = shapeFactory.createShape("Circle");
    6. if (circle != null) {
    7. System.out.println("Created Circle: " + circle);
    8. }
    9. // 创建矩形形状,逻辑同上
    10. Shape rectangle = shapeFactory.createShape("Rectangle");
    11. if (rectangle != null) {
    12. System.out.println("Created Rectangle: " + rectangle);
    13. }
    14. }
    15. }

    这样就实现了工厂方法和抽象工厂的设计模式。

文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。

发表评论

表情:
评论列表 (有 0 条评论,96人围观)

还没有评论,来说两句吧...

相关阅读