设计模式-抽象工厂模式

文章目录

一、详解

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

二、代码

  • 源码: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.
相关推荐
Boilermaker19921 小时前
[Java 并发编程] Synchronized 锁升级
java·开发语言
Cherry的跨界思维1 小时前
28、AI测试环境搭建与全栈工具实战:从本地到云平台的完整指南
java·人工智能·vue3·ai测试·ai全栈·测试全栈·ai测试全栈
alonewolf_992 小时前
JDK17新特性全面解析:从语法革新到模块化革命
java·开发语言·jvm·jdk
一嘴一个橘子2 小时前
spring-aop 的 基础使用(啥是增强类、切点、切面)- 2
java
sheji34162 小时前
【开题答辩全过程】以 中医药文化科普系统为例,包含答辩的问题和答案
java
恋爱绝缘体12 小时前
2020重学C++重构你的C++知识体系
java·开发语言·c++·算法·junit
xiaolyuh1232 小时前
Spring 框架 核心架构设计 深度详解
spring·设计模式·spring 设计模式
wszy18093 小时前
新文章标签:让用户一眼发现最新内容
java·python·harmonyos
wszy18093 小时前
顶部标题栏的设计与实现:让用户知道自己在哪
java·python·react native·harmonyos
程序员小假4 小时前
我们来说一下无锁队列 Disruptor 的原理
java·后端