Created
May 27, 2024 08:39
-
-
Save sunmeat/3689034ae8c7b26e34c022ce8930ab08 to your computer and use it in GitHub Desktop.
mediator pattern C# example
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
// абстрактный базовый класс-"коллега" | |
abstract class BodyPart | |
{ | |
protected Brain brain; // ссылка на медиатор | |
public BodyPart(Brain brain) | |
{ | |
this.brain = brain; | |
} | |
public abstract void Changed(); | |
} | |
// "коллеги" | |
class Ear : BodyPart | |
{ | |
private string? sounds; | |
public Ear(Brain brain) : base(brain) { } | |
public void HearSomething() | |
{ | |
Console.WriteLine("Enter what you hear (try to type \"cool\" or \"stupid\"): "); | |
sounds = Console.ReadLine(); | |
Changed(); | |
} | |
public string? GetSounds() | |
{ | |
return sounds; | |
} | |
public override void Changed() | |
{ | |
brain.SomethingHappenedToBodyPart(this); | |
} | |
} | |
class Eye : BodyPart | |
{ | |
public Eye(Brain brain) : base(brain) { } | |
public override void Changed() | |
{ | |
// throw new NotImplementedException(); | |
} | |
} | |
class Face : BodyPart | |
{ | |
public Face(Brain brain) : base(brain) { } | |
public override void Changed() | |
{ | |
// throw new NotImplementedException(); | |
} | |
public void Smile() | |
{ | |
Console.WriteLine("FACE: Smiling :)"); | |
} | |
} | |
class Hand : BodyPart | |
{ | |
private bool isSoft; | |
private bool isHurting; | |
public Hand(Brain brain) : base(brain) { } | |
public void FeelSomething() | |
{ | |
Console.WriteLine("What you feel is soft? (Yes/No) "); | |
string? answer = Console.ReadLine(); | |
if (answer != null && answer.Length > 0 && Char.ToLower(answer[0]) == 'y') | |
{ | |
isSoft = true; | |
} | |
Console.WriteLine("What you feel is hurting? (Yes/No) "); | |
answer = Console.ReadLine(); | |
if (answer != null && answer.Length > 0 && Char.ToLower(answer[0]) == 'y') | |
{ | |
isHurting = true; | |
} | |
Changed(); | |
} | |
public void HitPersonNearYou() | |
{ | |
Console.WriteLine("HAND: Just hit offender..."); | |
} | |
public void Embrace() | |
{ | |
Console.WriteLine("HAND: Embracing what is in front of you..."); | |
} | |
public bool DoesItHurt() | |
{ | |
return isHurting; | |
} | |
public bool IsItNice() | |
{ | |
return !isHurting && isSoft; | |
} | |
public override void Changed() | |
{ | |
brain.SomethingHappenedToBodyPart(this); | |
} | |
} | |
class Leg : BodyPart | |
{ | |
public Leg(Brain brain) : base(brain) { } | |
public override void Changed() | |
{ | |
// throw new NotImplementedException(); | |
} | |
public void Kick() | |
{ | |
Console.WriteLine("LEG: Just kicked offender in front of you..."); | |
} | |
public void StepBack() | |
{ | |
Console.WriteLine("LEG: Stepping back..."); | |
} | |
public void StepForward() | |
{ | |
Console.WriteLine("LEG: Stepping forward..."); | |
} | |
} | |
// медиатор | |
class Brain | |
{ | |
public Ear? ear; | |
public Eye? eye; | |
public Face? face; | |
public Hand? hand; | |
public Leg? leg; | |
public Brain() | |
{ | |
CreateBodyParts(); | |
} | |
private void CreateBodyParts() | |
{ | |
ear = new Ear(this); | |
eye = new Eye(this); | |
face = new Face(this); | |
hand = new Hand(this); | |
leg = new Leg(this); | |
} | |
public void SomethingHappenedToBodyPart(BodyPart bodyPart) | |
{ | |
if (bodyPart is Ear) | |
{ | |
string? heardSounds = ((Ear)bodyPart).GetSounds(); | |
if (heardSounds.Contains("stupid")) | |
{ | |
leg?.StepForward(); | |
hand?.HitPersonNearYou(); | |
leg?.Kick(); | |
} | |
else if (heardSounds.Contains("cool")) | |
{ | |
face?.Smile(); | |
} | |
else | |
{ | |
Console.WriteLine("OK, I give you another try"); | |
} | |
} | |
else if (bodyPart is Eye) | |
{ | |
Console.WriteLine("Brain can analyze what you see and can react appropriately using different body parts"); | |
} | |
else if (bodyPart is Hand) | |
{ | |
Hand? h = bodyPart as Hand; | |
if (h.DoesItHurt()) | |
{ | |
leg?.StepBack(); | |
} | |
else if (h.IsItNice()) | |
{ | |
leg?.StepForward(); | |
h.Embrace(); | |
} | |
else | |
{ | |
Console.WriteLine("OK, you can touch this :)"); | |
} | |
} | |
else if (bodyPart is Leg) | |
{ | |
Console.WriteLine("Leg can also feel something if you would like it to"); | |
} | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Brain brain = new Brain(); | |
Ear? ear = brain.ear; | |
Eye? eye = brain.eye; | |
Hand? hand = brain.hand; | |
Leg? leg = brain.leg; | |
ear?.HearSomething(); | |
hand?.FeelSomething(); | |
Console.ReadKey(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment