How to Subscribe to a single Event from multiple classes? - vb.net

I am creating a Draft Room for a football game I am making and have several classes involved with it.
The DraftPick Class will be responsible for raising an Event once the current team has made their selection. However, there are multiple other classes that need to "know" when this event has been fired as well. The Draft Class needs to know so it can remove the player from the list of available players, restart the clock and update the current team on the clock. The DraftNews Class needs to know about this event so it can update the in-draft ListView with the Pick information, and the DraftTicker class needs to know about the event to add the selection that was just made to the scrolling draft ticker at the bottom of the screen.
I'm working in VB.Net, so its not as straightforward as it would be in C#. However, I am thinking the easiest way would be to make the PickMade Event shared so the other classes would be able to simply access the event via DraftPick.PickMade() and simply use an AddHandler. I was wondering if there is an established "method" for doing this, or if what I am considering doing would be OK.
Any advice would be appreciated, first time I've had the need for multiple classes to be listening to an event...

Related

Can this be modeled in a state machine pattern?

I wanted to model a game (for practice reasons) using a state machine design pattern involving the following states:
Initial: The game-host sets up the game with some configuration parameters
Published: The game is visible to other players, so that they can join
Ready: As soon as the game is full (maximum number of players are reached), the host can start the game
Running: The game is running
Finished: The game is over, players can see some statistics about the game
However, after reading up on the state machine pattern, it appears to me that it is only applicable when the states all expose a common interface (e.g. the handleInput-method in this article: http://gameprogrammingpatterns.com/state.html).
What I intended to have is for the Game to have a publish method in the initial state, that will guide it into the Published state, where it then has a join method, which will guide it to Ready once enough players have joined, and so on.
The goal is to separate concerns here, I don't want a huge Game class having to deal with gathering all the players as well as handling the actual game logic.
Can this be done with a state machine? Or is there any other design pattern I could use for this?
The states expose a common interface but, this has nothing to do with your actual game.
The minimum requirement is to have the set of transitions from one state to the next and a mechanism to create the resulting state based on the previous state and the transition. The common interface deals only with this aspect.
For your initial state, the host sets some values on a GameConfig object. Pressing a "Publish" button is the transition to the next state. On the transition handler you need to create a GameLobby object.
The GameLobby is created with the game's name and expected number of players and deals strictly with adding players and other lobby related functions (such as player chat, which should also be implemented in their own class).
Here, the transition handler is called each time a player joins the lobby. Only when the total number of players is reached do you transition to the next state, "Game Ready". (If the number of playes is not yet equal to the requirements you can transition to a new GameLobby object with one extra player if you want to keep things immutable.)
Along with the GameConfig object, this new state contains a list of Player objects.
From this state, when the host clicks "Ready", in your transition handler you finally create your GameLogic object, based on the GameConfig object and the list of Player objects.
When the game is won, you transition to the "Finished" state which contains the game's statistics and a final transition to "end program".
Each of these main states can, in turn, contain their own state machines.
So, the state machine related interfaces do not restrict the interfaces of the payload.
I also recommend you separate the part related to displaying items on the screen from the part that determines "what" to display. So, each of those state objects would have something like an Init method where they generate a list of sprites that need to be displayed (and attaches any event handlers that are needed for the transitions). A separate object would then take that list and show it on the screen and call the even handlers on user input.

A program sharing TEXT with its copy (opened twice)

What i'm trying to do, is a simple program where i have 2 textboxes, and a button.(there is more but no need to say for resolving my problem)
When i write text in the 1st textbox and click the button, it will be written in the 2nd textbox. Everything is ok here.
Now, when i run this program more than once, i want the text to be written in the 2nd textbox of the others programs.
Sorry for the bad explaination i gave you before, hope it's better :)
OLD DESCRIPTION
i'm actually trying to make a program that works pretty much like a
messenger (basically 2 textboxes, 1 to send, 1 to view, and a button
to send) and i can't figure out how to link the program with itself
(copies). I want it to be able to read the textboxes contained
in the other copies and i don't want it to be 1 program with 2 forms,
but 1 single form, running severeal times (2 or more) How can i
do that? /!\ NOTE : It will be on a single computer, just like 2
forms!
I am assuming you are doing this in WinForms and only running one executable.
Sounds like you you want to create a single instance of a class that is shared between several forms. It should raise an event when the values change. After you create each instance of your form you pass in the class. In the form you would declare it With Events. When one form updates the class it would raise an event notifying the other forms to refresh themselves.
If you are familiar with WPF and MVVM this sort of design would be a little cleaner, but that takes some time to get up to speed on.
This design would not be appropriate if you want to run seperate applications and communicate between them.
You will have to do some kind of IPC, respectively remoting.
You could use:
anonymous pipes
named pipes
UDP
TCP
WCF (would be overkill IMHO)
windows messaging (would be the easiest way)

Track changes in a Java source

I want to track changes in a java source file in my eclipse plugin to update some references I keep of methods/variables/types if they are renamed.
I am interested in the IJavaElement before it was changed and how it is after the change.
I found
JavaCore.addElementChangedListener(/**/, ElementChangedEvent.POST_RECONCILE);
to obtain notifications of changes. The JavaElementDelta that the listener-event provide enables me to get the IJavaElement. Unfortunately, the notifications are difficult to handle.
For instance, if the user renames a method and pauses for a fraction of a second an event is fired and once the user finish typing the new name a further event is fired. That makes it necessary to track all those (incomplete) changes. That is cumbersome.
Is there a better way to obtain the before-after name an IJavaElement in a source file?

Implementing auto-save in WPF / MVVM / NHibernate

My client likes programs like Microsoft OneNote where changes are saved automatically, and he can choose to discard when he explicitly wants to do so. I will have to implement some undo functionality, but I'll figure that out some other time.
With NHibernate, I suppose I can call ISession.Update on every single property / binding change, but I can see real pain with this approach down the road. I am not a fan of timers, but maybe a 5 second timer that starts on property / binding change and at timer end use BackgroundWorker thread to save to db.
What do you think?
A ISession.Update on every property-change isn't a good idea normally. The property-change-events are fired quite often. It could slow down your application when you do ISession.Update on every change. This will probably lead to a bad user experience.
In our Application has the same behavior. We store the changes when a View is closed (an some other related event). For example when the user closes a tab, the data which was displayed in that tab is closed.
An additional timer is probably a good idea to prevent data loss when the application crashes / unexpected happens.

Notifications in wxWidgets?

I'm working on a small application using C++/wxWidgets, where several parts of the GUI need to be updated based on e.g. received UDP datagrams. More specifically, a secondary thread tries to keep a list of available "clients" in the network (which may come and go away) and e.g. corresponding comboboxes in the UI need to be updated to reflect the changes.
The documentation mentions that for this kind of thing EVT_UPDATE_UI would be a good choice. As far as I can understand from the sparse documentation, this event is sent automatically by the system and provides some support for assisted UI change.
However, I'd feel more comfortable using a more direct approach, i.e. where e.g. a window object could register/subscribe to receive notifications (either events or callbacks) upon particular events and another part of the code is sending out these notifications when required. I could do this in C++ using my own code, however I guess if wxWidgets already supports something like that, I should make use of it. However I haven't found anything in that regards.
So, the question is: does wxWidgets support this kind of notification system (or similar alternatives) or would I be best served coding my own?
AFAIK there is nothing directly usable in wxWidgets, but doing it on your own seems easy.
What I would do:
Create a wxEvtHandler-descendent class to hold the list of available "clients" in the network. Let this class have a wxCriticalSection, and use a wxCriticalSectionLocker for that in all methods that add or delete "clients".
Create a worker thread class by inheriting wxThread to handle your UDP datagrams, using blocking calls. The thread should directly call methods of the client list object whenever a client has to be added or removed. In these methods update the list of clients, and ::wxPostEvent() an event to itself (this will execute the whole notification calls in the main GUI thread).
Handle the event in the client list class, and notify all listeners that the list of clients has changed. The observer pattern seems to me a good fit. You could either call a method of all registered listeners directly, or send a wxCommandEvent to them.
Have you tried calling Update() on the widget(s) that change? Once you update the contents of the combo box, call Update(), and the contents should update.