Skip to content

Instantly share code, notes, and snippets.

@iwouldnot
Created October 24, 2017 03:07
Show Gist options
  • Save iwouldnot/479921a3150ed738904a2fd6a2d624b6 to your computer and use it in GitHub Desktop.
Save iwouldnot/479921a3150ed738904a2fd6a2d624b6 to your computer and use it in GitHub Desktop.
mediator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace constr_PO_laba7
{
abstract class Mediator
{
public abstract void Send(string msg, Person colleague);
}
abstract class Person
{
protected Mediator mediator;
public Person(Mediator mediator)
{
this.mediator = mediator;
}
}
class PersonOne : Person
{
public PersonOne(Mediator mediator)
: base(mediator)
{ }
public void Send(string message)
{
mediator.Send(message, this);
}
public void Notify(string message)
{
Console.WriteLine($"Notified: {message}");
}
}
class PersonTwo : Person
{
public PersonTwo(Mediator mediator)
: base(mediator)
{ }
public void Send(string message)
{
mediator.Send(message, this);
}
public void Notify(string message)
{
Console.WriteLine($"Notified: {message}");
}
}
class PersonMediator : Mediator
{
public PersonOne Person1 { get; set; }
public PersonTwo Person2 { get; set; }
public override void Send(string msg, Person person)
{
if (Person1 == person)
{
Console.WriteLine(person + ": ");
Person2.Notify(msg);
}
else
{
Console.WriteLine(person + ": ");
Person1.Notify(msg);
}
}
}
class Program
{
static void Main(string[] args)
{
PersonMediator mediator = new PersonMediator();
PersonOne vasya = new PersonOne(mediator);
PersonTwo petya = new PersonTwo(mediator);
mediator.Person1 = vasya;
mediator.Person2 = petya;
vasya.Send("Как дела?");
petya.Send("Да вроде нормально");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment