• 设计模式之工厂模式


    1. 工厂模式概述

    在这里插入图片描述
    工厂模式是一种创建型设计模式,用于创建对象的方式和过程,以便将对象的创建与其使用分离开来。这种模式提供了一种通用的接口,以创建对象,但具体的对象创建过程由实现工厂接口的具体子类决定。
    工厂模式通常包括以下几种变体:

    • 简单工厂模式:在这个模式中,有一个工厂类,负责根据客户的需求创建对象。客户不需要了解对象的具体创建过程,只需告诉工厂需要哪种类型的对象。

    • 工厂方法模式:工厂方法模式引入了抽象工厂类,其中包含一个或多个抽象方法,用于创建对象。具体的工厂子类实现这些抽象方法来创建具体的对象。

    • 抽象工厂模式:抽象工厂模式提供了一组相关的工厂类,每个工厂类可以创建一组相关的对象。这种模式有多个抽象方法,每个方法用于创建不同种类的对象。

    工厂模式有助于降低对象的创建和使用之间的耦合度,使代码更容易维护和扩展。根据需要,您可以选择使用简单工厂、工厂方法或抽象工厂模式,以满足不同的设计需求。
    在这里插入图片描述

    2. Java实现工厂模式

    2.1简单工厂模式的示例:

    // 抽象产品
    interface Product {
        void create();
    }
    
    // 具体产品A
    class ConcreteProductA implements Product {
        public void create() {
            System.out.println("Product A created.");
        }
    }
    
    // 具体产品B
    class ConcreteProductB implements Product {
        public void create() {
            System.out.println("Product B created.");
        }
    }
    
    // 简单工厂
    class SimpleFactory {
        public Product createProduct(String productType) {
            if ("A".equals(productType)) {
                return new ConcreteProductA();
            } else if ("B".equals(productType)) {
                return new ConcreteProductB();
            } else {
                throw new IllegalArgumentException("Invalid product type.");
            }
        }
    }
    
    public class FactoryPatternExample {
        public static void main(String[] args) {
            SimpleFactory factory = new SimpleFactory();
            Product productA = factory.createProduct("A");
            productA.create();
    
            Product productB = factory.createProduct("B");
            productB.create();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    在这个示例中,SimpleFactory 负责根据客户的请求创建不同类型的产品(Product)。客户只需提供产品的类型,而不需要了解产品的具体创建过程。

    2.2 工厂方法模式:创建不同类型的电视

    工厂方法模式是创建型设计模式,它定义了一个创建对象的接口,但将具体的对象创建过程延迟到子类。以下是一个使用Java的工厂方法模式的示例,结合实际场景:创建不同类型的电视机。
    首先,定义一个抽象产品接口 TV:

    public interface TV {
        void play();
    }
    
    
    • 1
    • 2
    • 3
    • 4

    然后,创建两个具体产品类,分别实现 TV 接口:

    public class SamsungTV implements TV {
        public void play() {
            System.out.println("Samsung TV is playing.");
        }
    }
    
    public class SonyTV implements TV {
        public void play() {
            System.out.println("Sony TV is playing.");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    接下来,定义一个抽象工厂接口 TVFactory,它包含一个用于创建 TV 对象的抽象方法:

    public interface TVFactory {
        TV createTV();
    }
    
    
    • 1
    • 2
    • 3
    • 4

    创建两个具体工厂类,分别实现 TVFactory 接口:

    public class SamsungTVFactory implements TVFactory {
        public TV createTV() {
            return new SamsungTV();
        }
    }
    
    public class SonyTVFactory implements TVFactory {
        public TV createTV() {
            return new SonyTV();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    现在,您可以使用工厂方法模式来创建不同品牌的电视机,而不需要直接调用具体的产品类。示例:

    public class FactoryMethodExample {
        public static void main(String[] args) {
            TVFactory samsungFactory = new SamsungTVFactory();
            TV samsungTV = samsungFactory.createTV();
            samsungTV.play();
    
            TVFactory sonyFactory = new SonyTVFactory();
            TV sonyTV = sonyFactory.createTV();
            sonyTV.play();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这个示例中,TVFactory 接口是工厂方法的核心,每个具体工厂类负责创建特定品牌的电视机。这种模式使得客户端代码可以轻松切换不同品牌的电视机,同时保持了高层代码与具体产品的解耦。

    2.3 抽象工厂模式:创建不同操作系统下的应用程序界面

    抽象工厂模式是一种创建型设计模式,用于创建一组相关或依赖对象的家族,而无需指定其具体类。以下是一个使用Java的抽象工厂模式的示例,结合实际场景:创建不同操作系统下的应用程序界面。

    首先,定义两个抽象产品接口 Button 和 Checkbox:

    public interface Button {
        void render();
    }
    
    public interface Checkbox {
        void render();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    然后,创建不同操作系统下的具体产品类,分别实现 Button 和 Checkbox 接口:

    public class WindowsButton implements Button {
        public void render() {
            System.out.println("Render a Windows button");
        }
    }
    
    public class WindowsCheckbox implements Checkbox {
        public void render() {
            System.out.println("Render a Windows checkbox");
        }
    }
    
    public class MacOSButton implements Button {
        public void render() {
            System.out.println("Render a macOS button");
        }
    }
    
    public class MacOSCheckbox implements Checkbox {
        public void render() {
            System.out.println("Render a macOS checkbox");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    接下来,定义两个抽象工厂接口 GUIFactory,它包含用于创建 Button 和 Checkbox 对象的抽象方法:

    public interface GUIFactory {
        Button createButton();
        Checkbox createCheckbox();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    创建两个具体工厂类,分别实现 GUIFactory 接口,每个工厂类负责创建特定操作系统下的界面元素:

    public class WindowsFactory implements GUIFactory {
        public Button createButton() {
            return new WindowsButton();
        }
    
        public Checkbox createCheckbox() {
            return new WindowsCheckbox();
        }
    }
    
    public class MacOSFactory implements GUIFactory {
        public Button createButton() {
            return new MacOSButton();
        }
    
        public Checkbox createCheckbox() {
            return new MacOSCheckbox();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    现在,您可以使用抽象工厂模式来创建操作系统相关的界面元素,而不需要直接调用具体的产品类。示例:

    public class AbstractFactoryExample {
        public static void main(String[] args) {
            GUIFactory factory;
            String os = "macOS"; // 切换操作系统
            if (os.equalsIgnoreCase("Windows")) {
                factory = new WindowsFactory();
            } else if (os.equalsIgnoreCase("macOS")) {
                factory = new MacOSFactory();
            } else {
                throw new UnsupportedOperationException("Unsupported OS: " + os);
            }
    
            Button button = factory.createButton();
            Checkbox checkbox = factory.createCheckbox();
    
            button.render();
            checkbox.render();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    这个示例中,GUIFactory 接口是抽象工厂的核心,每个具体工厂类负责创建特定操作系统下的界面元素。抽象工厂模式允许客户端代码在不改变的情况下创建不同操作系统下的界面元素,同时保持了高层代码与具体产品的解耦。

  • 相关阅读:
    数据挖掘原理与算法
    Hibernate 函数 ,子查询 和原生SQL查询
    想找手续费低的期货公司开户不难
    mysql的约束和表关系
    MySQL数据库中乐观锁和悲观锁【杭州多测师】【杭州多测师_王sir】
    MySQL数据库索引
    JavaFX Scene Builder Miscellaneous 控件详解
    【论文笔记】Population Based Training of Neural Networks(PBT)
    js数据类型和判断数据类型的方法
    【华为机试真题 JAVA】字符串删除后的最大数值-100
  • 原文地址:https://blog.csdn.net/m0_54187478/article/details/133976599