public sealed class Client
{
private Simulation _simulation;
public Client(ITickProvider tickProvider, ICommandReceiver commandReceiver)
{
_simulation = new Simulation();
tickProvider.Tick += HandleTick;
commandReceiver.CommandReceived += HandleCommandReceived;
}
private void HandleTick ()
{
//Distribute tick to _simulation
}
private void HandleCommandReceived(Command command)
{
//Inject command to _simulation
}
}
Client
operates a multithreaded Simulation, distributing ticks and external input to the Simulation.
ITickProvider
provides a Tick event that runs every e.g. fixed interval, button click, etc..
ICommandReceiver
receives external input (Commands); i.e. User input, server input, serialized input, etc..
Because the interfaces’ implementations are usually exclusive, I decided to make Client sealed and inject the functionality with the constructor. This also helps with controlling thread-sensitive access to Simulation.
What is the pattern of composing a sealed class with modular interfaces? Is this a well-known pattern and if so, have I implemented it correctly?