Last active
November 25, 2022 12:33
-
-
Save amatiasq/ca8be125acb55158465dc0bf4b80d1da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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