委托仓库

 1 public class ActionsRepository
 2 {
 3     private Dictionary<Type, IActionBase> _actions = new Dictionary<Type, IActionBase>();
 4 
 5     private static ActionsRepository _instance;
 6 
 7     private static object syncRoot = new object();
 8 
 9     public static ActionsRepository Instance => GetInstance();
10 
11     public IActionBase this[Type type]
12     {
13         get
14         {
15             if (_actions.ContainsKey(type))
16             {
17                 return _actions[type];
18             }
19 
20             return null;
21         }
22     }
23 
24     private ActionsRepository()
25     {
26     }
27 
28     public static ActionsRepository GetInstance()
29     {
30         lock (syncRoot)
31         {
32             if (_instance == null)
33             {
34                 _instance = new ActionsRepository();
35             }
36 
37             return _instance;
38         }
39     }
40 
41     public IActionBase GetIAction(Type type)
42     {
43         return this[type];
44     }
45 
46     public void RegisterAction(IActionBase action)
47     {
48         try
49         {
50             Type type = action.GetType();
51             if (!_actions.ContainsKey(type))
52             {
53                 _actions.Add(type, action);
54                 return;
55             }
56 
57             throw new ArgumentException("Action already registered");
58         }
59         catch
60         {
61         }
62     }
63 }
1 public interface IActionBase
2 {
3     void Execute();
4 }

 

1 public interface IActionBaseExt<T> : IActionBase
2 {
3     void Execute(T param);
4 }

 

posted @ 2025-12-02 10:01  家煜宝宝  阅读(3)  评论(0)    收藏  举报