User interface customization template

I have a Win32 C ++ program that validates user input and updates the interface with state and parameters information. It is currently written like this:

void ShowError() {
    SetIcon(kError);
    SetMessageString("There was an error");
    HideButton(kButton1);
    HideButton(kButton2);
    ShowButton(kButton3);
}

void ShowSuccess() {
    SetIcon(kError);

    std::String statusText (GetStatusText());
    SetMessageString(statusText);

    HideButton(kButton1);
    HideButton(kButton2);
    ShowButton(kButton3);
}

// plus several more methods to update the UI using similar mechanisms

      

I don't like this because it duplicates the code and forces me to update multiple methods if something changes in the UI.

I am wondering if there is a design pattern or best practice to remove duplication and make it easier to understand and update features.

I could consolidate the code inside a config function and pass flags to enable / disable UI elements, but I'm not sure if this is the best approach.

Any suggestions and ideas?

+2


a source to share


1 answer


I would recommend observer pattern and state pattern, when validation succeeds or fails, attached buttons can change their state according to the information provided in the "notify" method. Please refer to the GoF book for more details or just google it. Hope it helps.



+1


a source







All Articles