Skip to content

Instantly share code, notes, and snippets.

@amatiasq
Last active November 25, 2022 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amatiasq/ca8be125acb55158465dc0bf4b80d1da to your computer and use it in GitHub Desktop.
Save amatiasq/ca8be125acb55158465dc0bf4b80d1da to your computer and use it in GitHub Desktop.
using System;
// Interface segregation
interface ISimulationReporter<TResult>
{
event Action<TResult> OnComplete;
}
interface ISimulationRunner<TInput>
{
void Run(TInput values);
}
interface ISimulation<TInput, TResult>
: ISimulationRunner<TInput>, ISimulationReporter<TResult> {}
interface IConfigurableSimulation<TInput, TResult, TBehaviour>
: ISimulation<TInput, TResult>
{
TBehaviour Behaviour { get; set; }
}
interface ISimulationBehaviour<TSim, TInput>
where TSim : ISimulationRunner<TInput>
{
TInput GetInput(TSim simulation);
}
// Implementation
class MySimulation : IConfigurableSimulation<bool, int, MyBehaviour>
{
public event Action<int> OnComplete;
public void Run(bool values) {}
public MyBehaviour Behaviour { get; set; }
}
class MyBehaviour : ISimulationBehaviour<MySimulation, bool>
{
public bool GetInput(MySimulation simulation) => true;
}
// Testing
public class Testing
{
public static void Main() {
MyBehaviour a = new MyBehaviour();
ISimulationBehaviour<ISimulation<bool, int>, bool> b;
// Cannot convert source type 'MyBehaviour' to target type
// 'ISimulationBehaviour<ISimulation<bool,int>,bool>'
//
// Why?
// MyBehaviour implements ISimulationBehaviour<MySimulation, bool>
// MySimulation implements IConfigurableSimulation<bool, int, MyBehaviour>
// which itself extends ISimulation<TInput, TResult>
b = a;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment