−using System;
using System.Collections.Generic;
using System.Linq;
var button = new SubmitButton();
IFormMediator mediator = new FormMediator(button);
mediator.Changed("details", true);
mediator.Changed("confirmed", true);
Console.WriteLine(button.Enabled);
mediator.Changed("details", false);
Console.WriteLine(button.Enabled);
Console.WriteLine(mediator.Changed("unknown", true));
Console.WriteLine(button.Enabled);
sealed class SubmitButton { public bool Enabled { get; set; } }
interface IFormMediator { bool Changed(string field, bool ready); }
sealed class FormMediator(SubmitButton button) : IFormMediator
{
private bool details;
private bool confirmed;
public bool Changed(string field, bool ready)
{
switch (field)
{
case "details": details = ready; break;
case "confirmed": confirmed = ready; break;
default: return false;
}
button.Enabled = details && confirmed;
return true;
}
}
sealed class Toggle(string field, IFormMediator mediator)
{
public bool Set(bool ready) => mediator.Changed(field, ready);
}
+using System;
using System.Collections.Generic;
using System.Linq;
var button = new SubmitButton();
IFormMediator mediator = new FormMediator(button);
mediator.Changed("details", true);
mediator.Changed("confirmed", true);
Console.WriteLine(button.Enabled);
mediator.Changed("details", false);
Console.WriteLine(button.Enabled);
Console.WriteLine(mediator.Changed("unknown", true));
Console.WriteLine(button.Enabled);
sealed class SubmitButton { public bool Enabled { get; set; } }
interface IFormMediator { bool Changed(string field, bool ready); }
// 여러 위젯 사이의 관계를 이 중재자 한곳에서 계산한다.
sealed class FormMediator(SubmitButton button) : IFormMediator
{
private bool details;
private bool confirmed;
public bool Changed(string field, bool ready)
{
switch (field)
{
case "details": details = ready; break;
case "confirmed": confirmed = ready; break;
default: return false;
}
button.Enabled = details && confirmed;
return true;
}
}
// 위젯은 값 변경만 알리고 다른 위젯을 직접 제어하지 않는다.
sealed class Toggle(string field, IFormMediator mediator)
{
public bool Set(bool ready) => mediator.Changed(field, ready);
}