How to get a singleton in one class and not the whole application?

0 like 0 dislike
6 views
Developing a game like HearthStone. Is the state table
public class Table { public Table() { Players = new List(); } public List Players { get; set; } public int Queue { get; set; } }

And there is a Manager triggers that alerts you to the fact that the game occurred any event (map attacked, they are going to play a spell, etc.)
public class TriggerDispatcher { public TriggerDispatcher() { if (Triggers == null) Triggers = new Dictionary(); } public ITrigger GetTrigger() where T : ITrigger { if (Triggers.ContainsKey(typeof(T).Name)) return Triggers[typeof(T).Name]; var trigger = Activator.CreateInstance(); Triggers.Add(typeof(T).Name, trigger); return trigger; } public Dictionary Triggers { get; } } public interface ITrigger { void AddAction(IAction action); void RemoveAction(IAction action); void ExecuteActions(object param); }

It is necessary that these two classes were accessed from anywhere within the same session. The server creates session consisting of two players and they are instances of the table Manager. How to organize their reception in other places of code within the session?
by | 6 views

2 Answers

0 like 0 dislike
If it is correct - connect the DI Container can do everything through him.

If no container is to pass the dependencies manually. Usually they are not needed everywhere

What you tell personally I have done a little differently. Events, as such, no. There is a team, which is the event. For Example AttackCommand, MoveCommand, DeployCommand. All communication goes through the team, all other classes - dry data. Accordingly, all such things needs only commands, but they are created through a single center, which makes the injection.

Then I wrote the article:
\rhttps://habrahabr.ru/post/322258/
\rhttps://habrahabr.ru/post/322268/

And yet, if I write an email (in profile), I can throw an example of the server of the CCT in C#.
by
0 like 0 dislike
And Session.TriggerDispatcher and Session.Table do not suit you?
by

Related questions

0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
32,810 users