lundi 24 septembre 2018

Problems in refactoring code / figuring out the design pattern to use

Chiranjeev Thomas shared a link. 2 hrs Issues in refactoring the code :

So , I had quite a few lines of code to be executed in the main method of the Application (Main) class of a java project . To get rid of all the clutter , I ended up refactoring it using the extract method refactoring technique , but then I got a lot of static methods in the Main class which I'm not a fan of .... What would be a better way to get a clean main Application method ? Should I use a singleton class perhaps ?What other design pattern / technique should I use to avoid having all these static methods in my main class and keeping the main method as succinct as possible ?

Initial Code :

import controller.Statistics;
import model.primary.customer.CustomerInfo;
import model.primary.movie.MovieInfo;
import model.primary.rating.RatingInfo;
import util.FileParsing.FileParser;
import util.mapping.CustomerMapper;
import util.mapping.MovieMapper;
import util.mapping.RatingsMapper;
import view.DisplayStatistics;
import view.UserInterface;

public class Application {

    public static void main(String[] args) {
        Statistics statistics = processStatistics();
        DisplayStatistics displayStatistics = new DisplayStatistics(statistics);
        initiateUI(displayStatistics);
    }

    private static Statistics processStatistics() {
        MovieInfo movieInfo = processMovies();
        CustomerInfo customerInfo = processCustomers();
        RatingInfo ratingInfo = processRatings();

        return new Statistics(customerInfo, movieInfo, ratingInfo);
    }

    private static void initiateUI(DisplayStatistics displayStatistics) {
        UserInterface userInterface = new UserInterface(displayStatistics);
        userInterface.start();
    }

    private static RatingInfo processRatings() {
        FileParser ratingsFile = new FileParser("ratings.dat", "::");
        RatingsMapper ratingsMapper = new RatingsMapper(ratingsFile, 4);
        return new RatingInfo(ratingsMapper.getCustomerIDMovieIDRatingAndTimeMap());
    }

    private static CustomerInfo processCustomers() {
        FileParser customerFile = new FileParser("users.dat", "::");
        CustomerMapper customerMapper = new CustomerMapper(customerFile, 5);
        return new CustomerInfo(customerMapper.getIdCustomerMap());
    }

    private static MovieInfo processMovies() {
        FileParser movieFile = new FileParser("movies.dat", "::");
        MovieMapper movieMapper = new MovieMapper(movieFile, 3);
        return new MovieInfo(movieMapper.getIdMovieMap());
    }

}

Changed Code after using Singleton (Processor ):

import controller.Statistics;
import view.DisplayStatistics;
import view.UserInterface;

public class Application {

    public static void main(String[] args) {

        Statistics statistics = Processor.getInstance().processStatistics();

        DisplayStatistics displayStatistics = new DisplayStatistics(statistics);

        UserInterface userInterface = new UserInterface(displayStatistics);

        userInterface.start();
    }


}

Singleton Class (Processor) code :

import controller.Statistics;
import model.primary.customer.CustomerInfo;
import model.primary.movie.MovieInfo;
import model.primary.rating.RatingInfo;
import util.FileParsing.FileParser;
import util.mapping.CustomerMapper;
import util.mapping.MovieMapper;
import util.mapping.RatingsMapper;


public class Processor {
    private static Processor ourInstance = new Processor();

    public static Processor getInstance() {
        return ourInstance;
    }

    private Processor() {

    }

    static Statistics processStatistics() {

        MovieInfo movieInfo = processMovies();
        CustomerInfo customerInfo = processCustomers();
        RatingInfo ratingInfo = processRatings();

        return new Statistics(customerInfo, movieInfo, ratingInfo);
    }

    private static RatingInfo processRatings() {

        FileParser ratingsFile = new FileParser("ratings.dat", "::");
        RatingsMapper ratingsMapper = new RatingsMapper(ratingsFile, 4);
        return new RatingInfo(ratingsMapper.getCustomerIDMovieIDRatingAndTimeMap());
    }

    private static CustomerInfo processCustomers() {

        FileParser customerFile = new FileParser("users.dat", "::");
        CustomerMapper customerMapper = new CustomerMapper(customerFile, 5);
        return new CustomerInfo(customerMapper.getIdCustomerMap());
    }

    private static MovieInfo processMovies() {

        FileParser movieFile = new FileParser("movies.dat", "::");
        MovieMapper movieMapper = new MovieMapper(movieFile, 3);
        return new MovieInfo(movieMapper.getIdMovieMap());
    }
}

Aucun commentaire:

Enregistrer un commentaire