Git Product home page Git Product logo

refactoring's Introduction

Refactoring

In Controller.java

From Nuttapol's FlashGet code: https://github.com/nuttapol-kor/Final-Project-FlashGet

downloadPressed(), managingThread(), clearPressed() Method

In the src/flashget/Controller.java class

https://github.com/nuttapol-kor/Final-Project-FlashGet/blob/master/src/flashget/Controller.java

consider this code:

    public void downloadPressed(ActionEvent event) {
        ...
        threadProgressBar1.setVisible(false);
        threadProgressBar2.setVisible(false);
        threadProgressBar3.setVisible(false);
        threadProgressBar4.setVisible(false);
        threadProgressBar5.setVisible(false);
        ...
    }

    public void managingThread(URL url, File file, long size, int nThread) {
        ...
        List<ProgressBar> progressBarList = new ArrayList<>();
        progressBarList.add(threadProgressBar1);
        progressBarList.add(threadProgressBar2);
        progressBarList.add(threadProgressBar3);
        progressBarList.add(threadProgressBar4);
        progressBarList.add(threadProgressBar5);
        ...
        for (int i = 0; i < nThread; i++) {
            ...
            progressBarList.get(i).progressProperty().bind(task[i].progressProperty());
            progressBarList.get(i).setVisible(true);
        }
    }

    public void clearPressed(ActionEvent event) {
        ...
        threadProgressBar1.setVisible(false);
        threadProgressBar2.setVisible(false);
        threadProgressBar3.setVisible(false);
        threadProgressBar4.setVisible(false);
        threadProgressBar5.setVisible(false);
    }
  • Refactoring Signs:

    • many calls progressBar.setVisible() and progressBarList.add()
    • duplicate Code setVisible()
  • Refactoring: add arrayProgressBar as attribute of class and collect a bunch of progress bar in array when pressed the download button and move to downloadPressed()

    public void downloadPressed(ActionEvent event) {
        ...
        arrayProgressBar = new ProgressBar[]{threadProgressBar1, 
                        threadProgressBar2, threadProgressBar3, 
                        threadProgressBar4, threadProgressBar5};
        ...
    }

    public void managingThread(URL url, File file, long size, int nThread) {
        ...
        for (int i = 0; i < nThread; i++) {
            ...
            arrayProgressBar[i].progressProperty().bind(task[i].progressProperty());
            arrayProgressBar[i].setVisible(true);
        }
    }
  • Refactoring: create new method to set progress bar
    private void invisibleAllThreadProgressBar() {
        for (ProgressBar progressBar : arrayProgressBar) {
            progressBar.setVisible(false);
        }
    }

    public void downloadPressed(ActionEvent event) {
        ...
        arrayProgressBar = new ProgressBar[]{threadProgressBar1,
                        threadProgressBar2, threadProgressBar3,
                        threadProgressBar4, threadProgressBar5};
        invisibleAllThreadProgressBar();
        ...
    }

    public void clearPressed(ActionEvent event) {
        ...
        invisibleAllThreadProgressBar();
    }

refactoring's People

Contributors

nuttapol-kor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.