// Open closed principle // Our class is opened to extension but closed to modification // By adding the PersonFormat delegate we created the extension point // We changed how the class behaves without changing the class itself public string ToString(PersonFormat format) { if (format != null) { return(format(this)); } return(this.ToString()); }
public string ToString(PersonFormat format) { if (format != null) { return(format(this)); } else { return(string.Empty); } }
/// <summary> /// Gets from the parent if the parent is the PersonListingControl /// </summary> private void CaptureSettings() { PersonListingControl parent = Parent as PersonListingControl; if (parent != null) { personFormat = parent.PersonFormat; firstNameField = parent.DataFirstNameField; lastNameField = parent.DataLastNameField; birthDateField = parent.DataBirthDateField; birthDateFormat = parent.DataBirthDateFormat; cityField = parent.DataCityField; countryField = parent.DataCountryField; } }
// In order to assign a value to formatPerson viariable we check the values of radio buttons inside the UI // Then we assign an appropriate method private void AssignDelegate() { if (Option1Button.IsChecked.Value) { formatPerson = Formatters.Default; } else if (Option2Button.IsChecked.Value) { formatPerson = Formatters.LastNameToUpper; } else if (Option3Button.IsChecked.Value) { formatPerson = Formatters.FirstNameToLower; } else if (Option4Button.IsChecked.Value) { formatPerson = Formatters.FullName; } }
private void AssignToDelegate() { if (rbOptionOne.Checked) { proc = Formatters.Default; } else if (rbOptionTwo.Checked) { proc = Formatters.LastNameToUpper; } else if (rbOptionThree.Checked) { proc = Formatters.FirstNameToLower; } else if (rbOptionFour.Checked) { proc = Formatters.FullNameToUpper; } else { proc = Formatters.Default; } }
/* * here , a delegate is sent as a parameter to * this function */ public string ToString(PersonFormat format) { return(format(this)); }