Created
October 27, 2021 12:04
-
-
Save EgorBo/70cd91a2713df54643e851a9f83a6f16 to your computer and use it in GitHub Desktop.
ConstextInsensitivePGO.cs
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; | |
using System.Threading; | |
public class Prog | |
{ | |
static void DoWorkCommon(JobFactory factory, int i) => factory.CreateJob(i).DoWork(); | |
static void DoWork1(JobFactory factory) => DoWorkCommon(factory, 1); | |
static void DoWork2(JobFactory factory) => DoWorkCommon(factory, 2); | |
static void DoWork3(JobFactory factory) => DoWorkCommon(factory, 3); | |
static void DoWork4(JobFactory factory) => DoWorkCommon(factory, 4); | |
static void Main() | |
{ | |
var factory = new JobFactory(); | |
for (int i = 0; i < 50; i++) | |
{ | |
DoWork1(factory); | |
DoWork2(factory); | |
DoWork3(factory); | |
DoWork4(factory); | |
Thread.Sleep(16); | |
} | |
} | |
} | |
public interface IJob { | |
void DoWork(); | |
} | |
public class Job1 : IJob { | |
public void DoWork() => Console.WriteLine("DoWork1"); | |
} | |
public class Job2 : IJob { | |
public void DoWork() => Console.WriteLine("DoWork2"); | |
} | |
public class Job3 : IJob { | |
public void DoWork() => Console.WriteLine("DoWork3"); | |
} | |
public class Job4 : IJob { | |
public void DoWork() => Console.WriteLine("DoWork4"); | |
} | |
public class JobFactory | |
{ | |
public virtual IJob CreateJob(int i) => | |
i switch | |
{ | |
1 => new Job1(), | |
2 => new Job2(), | |
3 => new Job3(), | |
4 => new Job4(), | |
_ => throw new NotSupportedException() | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment