简体   繁体   中英

C# ProgressBar design pattern

I'm working on a database upgrader application. The upgrader updates the schema of a database ( adds new columns, renames columns , adds new tables, new views to an existing database by executing SQL statements ). When a user wants to upgrade from version 1.0 to 2.0 , "Upgrader" objects are taken from an object factory and the "Execute" method of each "Upgrader" is called. The GUI of the application has a progress bar and each time an upgrader object performs a SQL statement the progress bar gets incremented.

            while (!version.Equal(CurrentVersion))
            {

                 IUpgrader myUpgrader = UpgraderFactory.GetUpgrader(version);

                 myUpgrader.Execute(UpgradedFile,progressbar);

                 version.Increment();
            }

My question is very simple : how should the upgrader object communicate with the progressbar. In the code above, the upgrader object is given direct access to the progressbar but I'm wondering if some better way of doing this or better design pattern exists.

Regards, Seb

I would probably put together an interface for progress bar operations (C# sample):

interface IProgressBar
{
     int MinValue { get; set; }
     int MaxValue { get; set; }
     int Value { get; set; }
}

Then I would have the Upgrader object take a parameter of that interface type. Lastly I would create a class that implements the interface, and that also holds a reference to the progress bar component that it uses to visualize the progress.

That way you will have no tight coupling between the upgraders and how the progress is visualized.

To implement I would add a ProgessEvent to IUpgrader that is fed from the upgrader. The gui can add eventlisteners to that event and update the progessbar accordingly.

Many applications are cheeting. They advance their progressbar using a timer that keeps the illusion that the busy application is still alive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM