From f265b48e8a4f49f6599a34858b4e06997fabf2af Mon Sep 17 00:00:00 2001 From: "DESKTOP-3BO4HSG\\ksat" Date: Mon, 3 Apr 2023 15:22:20 +0800 Subject: [PATCH] chore: optimized singleton pattern is lazy and thread-safe --- STranslate/Helper/ConfigHelper.cs | 4 +--- STranslate/Helper/MvvmHelper.cs | 12 ++++++++++++ STranslate/Helper/SqliteHelper.cs | 1 + STranslate/ViewModel/MainVM.cs | 4 ++-- STranslate/ViewModel/SettingsVM.cs | 4 ++-- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/STranslate/Helper/ConfigHelper.cs b/STranslate/Helper/ConfigHelper.cs index 910c04e..852aee9 100644 --- a/STranslate/Helper/ConfigHelper.cs +++ b/STranslate/Helper/ConfigHelper.cs @@ -10,7 +10,7 @@ using System.Threading.Tasks; namespace STranslate.Helper { - public class ConfigHelper + public class ConfigHelper : Singleton { public T ReadConfig() { @@ -54,7 +54,5 @@ namespace STranslate.Helper /// private static string _ApplicationData => $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\{_AppName}"; private static readonly string _AppName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location); - private static ConfigHelper _Instance; - public static ConfigHelper Instance { get => _Instance ?? (_Instance = new ConfigHelper()); } } } diff --git a/STranslate/Helper/MvvmHelper.cs b/STranslate/Helper/MvvmHelper.cs index 8919b8f..2400018 100644 --- a/STranslate/Helper/MvvmHelper.cs +++ b/STranslate/Helper/MvvmHelper.cs @@ -5,6 +5,18 @@ using System.Runtime.CompilerServices; namespace STranslate.Helper { + /// + /// Generate Singleton + /// + /// + public class Singleton where T : class + { + private static readonly Lazy _instance = new Lazy(() + => Activator.CreateInstance(typeof(T), true) as T, true); + + public static T Instance => _instance.Value; + } + /// /// 通知 /// diff --git a/STranslate/Helper/SqliteHelper.cs b/STranslate/Helper/SqliteHelper.cs index ae59ac0..b4ae1fe 100644 --- a/STranslate/Helper/SqliteHelper.cs +++ b/STranslate/Helper/SqliteHelper.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; namespace STranslate.Helper { + //https://github.com/praeclarum/sqlite-net public class SqliteHelper : IDisposable { public SqliteHelper() diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index d7662c4..53e0896 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -474,8 +474,8 @@ namespace STranslate.ViewModel /// public MainWindow Mainwin; - private static MainVM _instance; - public static MainVM Instance => _instance ?? (_instance = new MainVM()); + private static Lazy _instance = new Lazy(() => new MainVM()); + public static MainVM Instance => _instance.Value; public Hotkeys NHotkeys; private bool IsTopmost { get; set; } diff --git a/STranslate/ViewModel/SettingsVM.cs b/STranslate/ViewModel/SettingsVM.cs index ab8a920..0b183ef 100644 --- a/STranslate/ViewModel/SettingsVM.cs +++ b/STranslate/ViewModel/SettingsVM.cs @@ -110,8 +110,8 @@ namespace STranslate.ViewModel public ICommand EscCmd { get; private set; } - private static SettingsVM _instance; - public static SettingsVM Instance => _instance ?? (_instance = new SettingsVM()); + private static Lazy _instance = new Lazy(() => new SettingsVM()); + public static SettingsVM Instance => _instance.Value; /// /// 是否开机自启