Answer the question
In order to leave comments, you need to log in
How is it more convenient to aggregate events that notify about property changes?
For example, let there be a ship. It is made up of compartments. Any equipment can be installed in the compartments. A ship has a mass that is made up of the mass of all its compartments. The mass of each compartment, for example, depends on whether equipment is installed in it or not. And the mass of the equipment itself can also vary, for example, depending on the mass of fuel in the fuel tank. And now, each component from this chain must notify about a change in its mass.
For clarity, the spherical class hierarchy:
public class Spacecraft
{
public IEnumerable<Hardpoint> Hardpoints { get; private set; }
public Single Mass => Hardpoints.Select(hp => hp.Mass).Sum();
}
public class Hardpoint
{
public Equipment InstalledEquipment { get; private set; }
public Boolean IsEquipmentInstalled => InstalledEquipment != null;
public Single Mass => IsEquipmentInstalled ? InstalledEquipment.Mass : 0;
}
public abstract class Equipment
{
public event Action MassChanged;
public Single Mass
{
get { return _mass; }
protected set
{
_mass = value;
MassChanged?.Invoke();
}
}
private Single _mass;
}
Answer the question
In order to leave comments, you need to log in
The ship at each moment of time has a certain state.
Something can change it, and someone must subscribe to these changes.
Flux -> Redux , you can borrow the implementation.
Implementing Redux in C#
Or the standard INotifyPropertyChanged interface.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question