设计模式-抽象工厂模式

文章目录

一、详解

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

二、代码

  • 源码: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.
相关推荐
xcl09252 小时前
北京24小时自助健身房系统软件开发实战:从架构到部署全流程指南
java·spring boot·架构
一 乐3 小时前
动漫书销售商城|基于springboot + vue动漫书销售商城(源码+数据库+文档)
java·数据库·vue.js·spring boot·毕业设计
卓怡学长4 小时前
w214基于jsp知道特产网
java·intellij-idea
步行cgn4 小时前
Spring 注解使用详解
java·spring
一条小小yu4 小时前
Spring IoC的理解
java·后端·spring
落魄实习生4 小时前
Agent Scope Java 2.x 系列【7】工具使用
java·开发语言·ai
旺仔学长 哈哈4 小时前
springboot钓鱼爱好者交流平台APP设计与实现
java·spring boot·mysql·充电桩管理系统
自强的小白6 小时前
核心功能(Service接口)
java·mybatis
乌暮7 小时前
深入理解 Java 泛型:把「万能盒子」用对、用稳
java·开发语言·后端·学习