As you know, hedgehogs are different - forest , eared , anti-tank and even sea. They all differ in intelligence, ingenuity and deceit. That is, to catch a hedgehog is not a trivial task, involving numerous dangers and requiring comprehensive training and modern equipment. That is why it is difficult to overestimate the importance of modern technology in this difficult matter.
So let's get started. To begin, select the primary entity - Abstract Hedgehog.
public abstract class AbstractHedgehog { public abstract void TellAboutItself(); public virtual void Snort() { Console.WriteLine("Пых-пых"); } public virtual void Stomp() { Console.WriteLine("Бадабум"); } public virtual void Fly(Human human) { Console.WriteLine(human != null ? "Я лечуууу!!!!" : "Я очень гордая птица и никуда не полечу, пока меня не пнут"); } public bool IsCaught { get; set; } }
As you can see, our Abstract Hedgehog can stomp and puff. In addition, modern science has established that you can also talk to a hedgehog . However, this is not all. In addition, hedgehogs can fly. True, they never do this without help, so a person will be needed for the flight. It is also worth noting that the hedgehog can be free or caught (IsCaught). In general, our Abstract Hedgehog can a lot. Now more about the types of hedgehogs. We will have three of them: Yozh (ordinary), Eared Yozh and Anti-Tank Yozh:
public class Hedgehog : AbstractHedgehog { public override void TellAboutItself() { Console.WriteLine("Я простой ёж, и я умею пыхтеть"); } } public class EaredHedgehog : AbstractHedgehog { public override void TellAboutItself() { Console.WriteLine("Я очень ушастый ёж"); } } public class CzechHedgehog : AbstractHedgehog { public override void TellAboutItself() { Console.WriteLine("Я противотанковый ёж"); } public override void Snort() { Console.WriteLine("К сожалению, я не умею пыхтеть"); } public override void Stomp() { Console.WriteLine("Топать я тоже не умею. Печаль ;("); } public override void Fly(Human human) { Console.WriteLine("Противотанковые ежи не умеют летать"); } }
To catch a hedgehog, we need a special tool - a hedgehog trap. We will model it with an interface - this will allow us to achieve a certain flexibility later):
public interface IHedgehogTrap { bool Catch(AbstractHedgehog hedgehog); }
Traps are different, but they all need to be able to catch a hedgehog (that is, implement the IHedgehogTrap interface). Consider two types - Simple Ezhovaya Trap (low reliability and low cost, not able to catch anti-tank hedgehogs) and Advanced Atomic Egg Trap (the road to use and maintain, but can catch any, even the most cunning and dodgy hedgehog)
public class SimpleTrap : IHedgehogTrap { public bool Catch(AbstractHedgehog hedgehog) { if(hedgehog is CzechHedgehog) throw new Exception("Нельзя поймать противотанкового ежа обычной ловушкой"); var random = new Random(); hedgehog.IsCaught = random.Next(2) == 1; Console.WriteLine(hedgehog.IsCaught ? "Ёж успешно пойман" : "Коварный ёж ускользнул"); return hedgehog.IsCaught; } } public class NucleareTrap : IHedgehogTrap { public bool Catch(AbstractHedgehog hedgehog) { hedgehog.IsCaught = true; Console.WriteLine("Ёж успешно пойман"); return true; } }
Well, the last of the entities under consideration is the Man (he is the Hedgehog Catcher):
public class Human { public Human() { } public Human(IHedgehogTrap trap) { Trap = trap; } public IHedgehogTrap Trap { get; set; } public void Catch(AbstractHedgehog hedgehog) { if (Trap == null) throw new Exception("Нельзя ловить ежа голыми руками"); Trap.Catch(hedgehog); } public void Kick(AbstractHedgehog hedgehog) { hedgehog.Fly(this); } }
In principle, it would also be nice to make a Man abstract, and to inherit several different descendants from him, but we are interested in precisely hedgehogs, and not people. A person may try to catch a hedgehog (Catch), give it an acceleration for flight (Kick), and may also have (or not) a trap for catching a hedgehog (Trap). Moreover, there can be any trap, it is only important that it implement the IHedgehogTrap interface - this allows changing the ways of catching a hedgehog in accordance with the “Strategy” pattern.
Now try to catch a few hedgehogs:
public static void CatchEmAll(Human hunter, IEnumerable<AbstractHedgehog> hedgehogs) { try { foreach (var hedgehog in hedgehogs) { hunter.Catch(hedgehog); if (hedgehog.IsCaught) { hedgehog.TellAboutItself(); hedgehog.Snort(); hedgehog.Stomp(); hunter.Kick(hedgehog); } } } catch (Exception e) { Console.WriteLine("Случилось что-то ужасное: {0}. Чип и Дейл уже спешат на помощь", e.Message); } } .... public static void Main() { var hunter = new Human(); var hedgehogs = new AbstractHedgehog[] { new Hedgehog(), new EaredHedgehog(), new CzechHedgehog() }; CatchEmAll(hunter, hedgehogs); // {1} hunter.Trap = new SimpleTrap(); CatchEmAll(hunter, hedgehogs); // {2} hunter.Trap = new NucleareTrap(); CatchEmAll(hunter, hedgehogs); // {3} Console.ReadLine(); }
As you can see, the hedgehogs were caught in three ways - with bare hands, a simple hedgehog trap and an improved atomic hedgehog trap. The best results were shown by the Advanced Atomic Egg Trap - all three hedgehogs were caught with its help.
In conclusion, it is also worth adding that in the hierarchy of hedgehogs described above, a mistake was deliberately made - Hedge Counter-Target was assigned to the descendants of the Abstract Hedgehog, which ultimately led to a lot of special logic in the Snort, Stomp and Fly methods and created more problems than advantages. This suggests that the Counter-Hedgehog should be excluded from this hierarchy.