关于工厂模式和单例模式

扫测资讯 2025-02-12 12:07   91 0

工厂模式

工厂模式就是将对象的创建过程封装在一个工厂类中,将创建对象的任务交给工厂完成。外部只能通过工厂类来指定创建或查找一个什么类型的对象,但不能直接创建对象。这样的好处在于实现了创建逻辑和业务逻辑的解耦。让代码变得更好看。

工厂模式又可以细分为简单工厂模式、工厂方法模式和抽象工厂模式。

简单工厂模式(静态工厂)

public class AnimalFactory {
    public static Animal createAnimal(String type) {
        if ("dog".equals(type)) {
            return new Dog();
        } else if ("cat".equals(type)) {
            return new Cat();
        }
        throw new IllegalArgumentException("Invalid animal type!");
    }
}
// 使用示例
Animal dog = AnimalFactory.createAnimal("dog");

静态工厂只能生产已经固定好的某几种类

工厂方法模式 (多态工厂)

public interface AnimalFactory {
    Animal createAnimal();
}

public class DogFactory implements AnimalFactory {
    @Override
    public Animal createAnimal() {
        return new Dog();
    }
}

public class CatFactory implements AnimalFactory {
    @Override
    public Animal createAnimal() {
        return new Cat();
    }
}
// 使用示例
AnimalFactory factory = new DogFactory();
Animal dog = factory.createAnimal();

几个工厂类实现某一个父工厂接口,专门负责阐述某一种对象。类似于子公司?或者外包?

这种工厂方法模式是用的最频繁的。(折中的艺术)。在一个抽象工厂中定义抽象产品,然后由具体的工厂区实现具体的产品。在客户端使用的时候,只需要选择对应的工厂,然后根据接口选择方法就行了,不需要关注工厂内部(产品)具体是怎么实现的。

抽象工厂模式 (创建产品族):

// 定义抽象工厂
public interface AbstractFactory {
    Button createButton();
    TextField createTextField();
}

// 具体工厂实现(例如 Windows 风格)
public class WindowsFactory implements AbstractFactory {
    @Override
    public Button createButton() { return new WindowsButton(); }
    
    @Override
    public TextField createTextField() { return new WindowsTextField(); }
}

基本思想和多态工厂是类似的,不过是侧重点不一样。

单例模式

单例模式就是某一个类只有一个具体的实现(对象),提供全局访问。

单例模式由饿汉式、懒汉式、静态内部类和枚举单例

饿汉式 (线程安全,但可能浪费资源)

public class Singleton {
    private static final Singleton instance = new Singleton();
    private Singleton() {} // 私有构造函数
    public static Singleton getInstance() {
        return instance;
    }
}

将构造方法私有并且将实例用final标记,这样外部就不能再创建这个类的对象,只能用get方法请求这个类在初始化时建立的对象。

懒汉式 (延迟加载,需处理线程安全)

public class Singleton {
    private static volatile Singleton instance;
    private Singleton() {}
    
    public static Singleton getInstance() {
        if (instance == null) {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

不再在初始化的时候创建实例,而是用到的时候再创建,但这样就带来线程安全的问题。

静态内部类 (推荐方式,线程安全且懒加载)

public class Singleton {
    private Singleton() {}
    
    private static class Holder {
        private static final Singleton instance = new Singleton();
    }
    
    public static Singleton getInstance() {
        return Holder.instance;
    }
}

这种是单例模式中最常用的。首先,这种模式在singleton被初始化时并不会生成对象,只有调用get方法之后才会创建holder类的对象,今儿创建一个singleton的对象,这个对象的唯一性由final关键字保证。并且也不需要像懒汉式一样使用Synchronized关键字。

枚举单例 (最安全的方式,防止反射破坏单例)

public enum Singleton {
    INSTANCE;
    public void doSomething() { ... }
}

在Spring中的应用

在Spring中,混合使用了工厂方法模式和抽象工厂模式。单例模式也用了,但并不是上述的几种单例模式,而是通过Spring容器来对单例进行管理(其中单例类似于饿汉式)