赞
踩
单例模式(Singleton Pattern)是一种常用的软件设计模式,用于确保一个类仅有一个实例,并提供一个全局访问点来获取这个实例。这种模式在需要控制资源访问,如配置文件读取、数据库连接、线程池等场景中非常有用。
new
关键字创建实例。- public class SingletonLazy {
- private static SingletonLazy instance;
-
- private SingletonLazy() {}
-
- public static SingletonLazy getInstance() {
- if (instance == null) {
- instance = new SingletonLazy();
- }
- return instance;
- }
-
- // 注意:这个实现在多线程环境下是不安全的
- }
通过在getInstance()
方法上添加synchronized
关键字来保证线程安全,但这样会影响性能。
- public class SingletonLazyThreadSafe {
- private static SingletonLazyThreadSafe instance;
-
- private SingletonLazyThreadSafe() {}
-
- public static synchronized SingletonLazyThreadSafe getInstance() {
- if (instance == null) {
- instance = new SingletonLazyThreadSafe();
- }
- return instance;
- }
- }
这种方式既保证了线程安全,又提高了性能,但需要注意volatile
关键字的使用来防止指令重排序。
- public class SingletonDoubleChecked {
- private static volatile SingletonDoubleChecked instance;
-
- private SingletonDoubleChecked() {}
-
- public static SingletonDoubleChecked getInstance() {
- if (instance == null) {
- synchronized (SingletonDoubleChecked.class) {
- if (instance == null) {
- instance = new SingletonDoubleChecked();
- }
- }
- }
- return instance;
- }
- }

这种方式利用了classloader的机制来保证实例的唯一性,并且实现了懒加载。
- public class SingletonStaticInner {
- private SingletonStaticInner() {}
-
- private static class SingletonHolder {
- private static final SingletonStaticInner INSTANCE = new SingletonStaticInner();
- }
-
- public static final SingletonStaticInner getInstance() {
- return SingletonHolder.INSTANCE;
- }
- }
这是实现单例模式的最佳方法,它更简洁,自动支持序列化机制,绝对防止多次实例化。
- public enum SingletonEnum {
- INSTANCE;
-
- public void someMethod() {
- // 方法实现
- }
- }
-
- // 使用
- SingletonEnum.INSTANCE.someMethod();
单例模式是一种简单但强大的设计模式,用于确保类只有一个实例。不同的实现方式有不同的特点和适用场景,需要根据实际需求来选择。枚举方式是推荐的首选实现方式,因为它既简洁又安全。
多例模式(Multiton Pattern)是单例模式的一种变体,它允许一个类有多个实例,但实例的数量是有限且固定的。在多例模式中,通常会有一个枚举或者一个映射(如HashMap
)来管理这些有限的实例。每个实例都与一个唯一的标识符(如键或枚举值)相关联。
new
关键字创建实例。以下是一个使用HashMap
来实现多例模式的示例
- import java.util.HashMap;
- import java.util.Map;
-
- public class Multiton {
- // 私有静态映射,用于存储所有实例
- private static final Map<KeyType, Multiton> instances = new HashMap<>();
-
- // 私有构造函数
- private Multiton() {}
-
- // 公共静态方法,根据KeyType返回对应的实例
- public static synchronized Multiton getInstance(KeyType key) {
- Multiton instance = instances.get(key);
- if (instance == null) {
- instance = new Multiton();
- instances.put(key, instance);
- }
- return instance;
- }
-
- // 假设的KeyType枚举,定义了所有可能的实例键
- public enum KeyType {
- INSTANCE1, INSTANCE2, INSTANCE3;
- }
-
- // 类的其他方法和属性...
- }
-
- // 使用示例
- public class Main {
- public static void main(String[] args) {
- Multiton instance1 = Multiton.getInstance(Multiton.KeyType.INSTANCE1);
- Multiton instance2 = Multiton.getInstance(Multiton.KeyType.INSTANCE2);
-
- // instance1 和 instance2 是不同的实例
- System.out.println(instance1 == instance2); // 输出 false
-
- // 再次获取相同的实例
- Multiton instance1Again = Multiton.getInstance(Multiton.KeyType.INSTANCE1);
-
- // instance1 和 instance1Again 是相同的实例
- System.out.println(instance1 == instance1Again); // 输出 true
- }
- }

getInstance
方法需要是同步的,以防止多个线程同时创建同一个键的实例。然而,同步可能会影响性能。如果实例的创建不是非常频繁,或者可以通过其他方式(如双重检查锁定)来优化同步,那么这种影响可能是可以接受的。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。