How to make the cleanest code when reporting progress to a user?

后端 未结 13 1709
失恋的感觉
失恋的感觉 2021-02-08 09:10

I\'ve struggled for the last couple of months to come up with some clean code to report progress to a user. Everything always seems to boil down to:

ReportProgr         


        
13条回答
  •  没有蜡笔的小新
    2021-02-08 09:50

    A fairly simple and clean way would be to create an abstract class that has a do() method and abstract doTask() and getName() methods:

    do() {
        ReportProgress("Starting " + this.getName());
        doTask();
        ReportProgress("Finished " + this.getName());
    }
    

    Then in your tasks:

    class Task1 extends Task {
        getName(){return "Task 1";}
        doTask() {
            //do stuff here
        }
    }
    

    You could then have a Task that has a doTask() method that runs do() on a whole bunch of other tasks. This could easily be recursive, in that any Task might then run a number of sub-Tasks.

提交回复
热议问题