Source
Quite fancied a Monday morning teaser, so here's my attempt: using System; namespace ChickenEgg { public class Program { public static void Main(string[] args) { var chicken1 = new Chicken(); var egg = chicken1.Lay(); var childChicken = egg.Hatch(); var childChicken2 = egg.Hatch(); } } public interface IBird { Egg Lay(); } public class Egg { private readonly Func<IBird> _createBird; private bool _hatched; public Egg(Func<IBird> createBird) { _createBird = createBird; } public IBird Hatch() { if (_hatched) throw new InvalidOperationException("Egg already hatched."); _hatched = true; return _createBird(); } } public class Chicken : IBird { public Egg Lay() { var egg = new Egg(() => new Chicken()); return egg; } } } Please don't just copy/paste this to your prospective employer without fully understanding what's going on. That would be a waste of everyone's time.
No comments:
Post a Comment