Skip to content

Instantly share code, notes, and snippets.

@maritaria
Created January 29, 2018 13:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save maritaria/673e29ef84b4199ccd4adc943bf097f9 to your computer and use it in GitHub Desktop.
public interface Button {
void Click();
}
public interface Label {
void setText(string text);
string getText();
}
// TRADITIONAL
public class MyWindow {
private HelloButton hello;
private WorldButton world;
private MyLabel label;
public MyWindow() {
label = new MyLabel();
hello = new HelloButton(label);
world = new WorldButton(world);
}
}
public class HelloButton : Button {
private MyLabel label;
public HelloButton(MyLabel label) {
this.label = label;
}
public void Click() {
label.setText("Hello");
}
}
public class WorldButton : Button {
private MyLabel label;
public WorldButton(MyLabel label) {
this.label = label;
}
public void Click() {
label.setText("World");
}
}
public class MyLabel {
private String text = "";
public void setText(string text) {
this.text = text;
}
}
// MEDIATOR
public class MyWindow {//The mediator
void onClickHello() {
label.setText("Hello");
}
void onClickWorld() {
label.setText("World");
}
}
public class HelloButton : Button {
MyWindow med;
public void Click() {
med.onClickHello();
}
}
public class WorldButton : Button {
MyWindow med;
public void Click() {
med.onClickWorld();
}
}
// EVOLUTION: add checkbox for all capitals label
public interface Checkbox {
public void Check(){}
public void Uncheck(){}
}
public class CapitalsCheckbox : Checkbox {
private MyWindow med;
public void Check() {
med.onCapitalsChecked();
}
public void Uncheck() {
med.onCapitalsUnchecked();
}
}
public class MyWindow {//The mediator
private bool allCaps;
private string originalLabelText;
void onClickHello() {
originalLabelText = "Hello";
label.setText(allCaps ? "HELLO" : "Hello");
}
void onClickWorld() {
originalLabelText = "World";
label.setText(allCaps ? "WORLD" : "World");
}
void onCapitalsChecked() {
allCaps = true;
label.setText(originalLabelText.toUpperCase());
}
void onCapitalsUnchecked() {
allCaps = false;
label.setText(originalLabelText);
}
}
// The mediator became a lot more complex, this will keep going on the more UI we add
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment