Skip to content

EventHandler

jsutlive edited this page May 17, 2023 · 5 revisions

class EventHandler

Generic Class

Extends: N/A

Implements: IEvent

This class is used to create event handlers to transmit data between systems. This is a C#-style event handler system (with IEvent), ported to Java. It is a slightly altered script from Danilow's C# Like Event Handlers Pattern in Java. This class is generic, meaning it is parameterized (the same way List, HashSet, etc. are). Events provide an excellent way to separate and organize logic for different program systems such as data handling, rendering, and input.

Fields:

Accessibility Type Field Name Description
private ArrayList<IEvent>> eventDelegateArray list of objects that are subscribing to the event

Methods

Accessibility Return Type Method Name Description
public void setDefaultColor sets the value for defaultColor
public void setTargetLengthRatio sets the value for targetLengthRatio
public void highlightColor If this object is included in a list of entities, call alter colors, else call resetToDefaultColor
private void alterColors change object color to highlightedColor
public void resetToDefaultColor changes object color to defaultColor

How events work:

See more detailed information in the code examples here.

Opening

To create/ open an event, simply create an object, passing in a java class as the type or parameter of this class instance.

Subscribers

The type defined when the event is opened determines what functions are allowed to be added as subscribers. For instance, if the type was Integer, only functions that take a single integer as their input are allowed to subscribe to the event. When an object subscribes to an event, whenever invoke is called, the subscriber function will run.

If the function no longer needs to respond to an event, but the event should not be closed, the user can unsubscribe from the event. It is best practice to unsubscribe to an event before destroying an object that holds a subscribed method; not doing this can lead to memory management issues.

Closing

When an event is not going to be used by the program, memory can be saved by closing the event. This removes all subscribers from its list and will remove the event object from memory.

Clone this wiki locally