开发者

C# ProgressBar design pattern

开发者 https://www.devze.com 2023-01-02 08:19 出处:网络
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

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 upgrade开发者_开发技巧r 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.

0

精彩评论

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