设计模式-抽象工厂模式

文章目录

一、详解

  • 概念: 定义一个抽象工厂接口 ,根据传入的类型 创建对应的工厂,进而创建一组相关的对象
  • 主要用途:有多于一个的产品族,根据类型选择某一组的产品
  • 代码:多组产品接口、子类和工厂、抽象工厂、产品组选择器

二、代码

  • 源码:https://gitee.com/deschen/designPattern-study

  • 多组产品接口、子类和工厂

    • 交通工具类

      java 复制代码
      public interface Transport {
      
          void drive();
      }
      
      // 子类
      public class Bicycle implements Transport {
      
          @Override
          public void drive() {
              System.out.println("Driving a bicycle.");
          }
      }
      public class Car implements Transport {
      
          @Override
          public void drive() {
              System.out.println("Driving a car.");
          }
      }
      public class Plane implements Transport {
      
          @Override
          public void drive() {
              System.out.println("Driving a plane.");
          }
      }
      
      // 工厂
      public class TransportFactory extends AbstractFactory{
      
          @Override
          public Transport createTransport(String type) {
              switch (type) {
                  case "car":
                      return new Car();
                  case "bicycle":
                      return new Bicycle();
                  case "plane":
                      return new Plane();
                  default:
                      throw new IllegalArgumentException("Invalid transport type: " + type);
              }
          }
      
          @Override
          public Animal createAnimal(String type) {
              return null;
          }
      
      }
    • 动物类

      java 复制代码
      public interface Animal {
      
          void run();
      }
      
      //子类
      public class Bird implements Animal {
          @Override
          public void run() {
              System.out.println("Bird fly.");
          }
      }
      public class Cat implements Animal {
      
          @Override
          public void run() {
              System.out.println("Cat run.");
          }
      }
      public class Fish implements Animal {
          @Override
          public void run() {
              System.out.println("Fish swim.");
          }
      }
      
      // 工厂
      public class AnimalFactory extends AbstractFactory{
      
          @Override
          public Transport createTransport(String type) {
              return null;
          }
      
          @Override
          public Animal createAnimal(String type) {
              switch (type) {
                  case "cat":
                      return new Cat();
                  case "bird":
                      return new Bird();
                  case "fish":
                      return new Fish();
                  default:
                      throw new IllegalArgumentException("Invalid animal type: " + type);
              }
          }
      
      }
  • 抽象工厂

    java 复制代码
    public abstract class AbstractFactory {
    
        public abstract Transport createTransport(String type);
    
        public abstract Animal createAnimal(String type);
    }
  • 产品组选择器

    java 复制代码
    public class FactorySelector {
    
        public static AbstractFactory getFactory(String type) {
            switch (type) {
                case "transport":
                    return new TransportFactory();
                case "animal":
                    return new AnimalFactory();
                default:
                    throw new IllegalArgumentException("Invalid factory type: " + type);
            }
        }
    }
  • 用例

    java 复制代码
    public class Demo {
    
        public static void main(String[] args) {
            // 选择交通工具工厂
            AbstractFactory transportFactory = FactorySelector.getFactory("transport");
    
            // 创建汽车
            Transport car = transportFactory.createTransport("car");
            car.drive();
    
            // 创建自行车
            Transport bicycle = transportFactory.createTransport("bicycle");
            bicycle.drive();
    
            // 创建飞机
            Transport plane = transportFactory.createTransport("plane");
            plane.drive();
    
            System.out.println("========================");
            // 选择动物工厂
            AbstractFactory animalFactory = FactorySelector.getFactory("animal");
    
            // 创建猫
            Animal cat = animalFactory.createAnimal("cat");
            cat.run();
    
            // 创建鸟
            Animal bird = animalFactory.createAnimal("bird");
            bird.run();
    
            // 创建鱼
            Animal fish = animalFactory.createAnimal("fish");
            fish.run();
        }
    }
    
    // 输出
    Driving a car.
    Driving a bicycle.
    Driving a plane.
    ========================
    Cat run
    Bird fly.
    Fish swim.
相关推荐
孟陬1 天前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌1 天前
一站式了解四种限流算法
java·后端·go
华仔啊1 天前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java
也些宝1 天前
Java单例模式:饿汉、懒汉、DCL三种实现及最佳实践
java
阿星AI工作室1 天前
给openclaw龙虾造了间像素办公室!实时看它写代码、摸鱼、修bug、写日报,太可爱了吧!
前端·人工智能·设计模式
Nyarlathotep01131 天前
SpringBoot Starter的用法以及原理
java·spring boot
wuwen51 天前
WebFlux + Lettuce Reactive 中 SkyWalking 链路上下文丢失的修复实践
java
SimonKing1 天前
GitHub 10万星的OpenCode,正在悄悄改变我们的工作流
java·后端·程序员
Seven971 天前
虚拟线程深度解析:轻量并发编程的未来趋势
java