开发者

Pattern for UI configuration

开发者 https://www.devze.com 2022-12-27 06:58 出处:网络
I have a Win32 C++ program that validates user input and updates the UI with status information and options.Currently it is written like this:

I have a Win32 C++ program that validates user input and updates the UI with status information and options. Currently it is 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 do not likes this because it duplicates code and caus开发者_如何学编程es me to update several methods if something changes in the UI.

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

I could consolidate the code inside a config function and pass in flags to enable/disable UI items, but I am not convinced this is the best approach.

Any suggestions and ideas?


I would recommend Observer Pattern and State Pattern, when an validation happens to be successful or unsuccessful, attached buttons can change their state according to information provided in "notify" method. Please refer to GoF's book for further details, or just google them. Hope it helps.

0

精彩评论

暂无评论...
验证码 换一张
取 消