因此,我在Java项目的Application(Main)类的main方法中要执行很多行代码。为了摆脱所有混乱,我最终使用了extract方法重构技术对其进行了重构,但是随后在Main类中得到了很多静态方法,因此我不喜欢....哪一种更好呢?干净的主要应用方法的方法?我应该使用单例类吗?应该使用什么其他设计模式/技术来避免在我的主类中包含所有这些静态方法,并使主方法尽可能简洁?
初始代码:
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());
}
}
在使用Singleton类(处理器)之后更改了代码:
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();
}
}
单类(处理器)代码:
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());
}
}
答案 0 :(得分:0)
很难判断您的设计,因为不清楚您的应用程序将如何更改,特别是您发布的代码是在应用程序的根目录(main
方法)中编写的,欢迎更改。
为了提出更好的设计,我认为:
XXXInfo
的方式,例如它们可以从SQL数据库,网络中加载和处理... Statistics
,DisplayStatistics
和UserInterface
的合成发生在您应用中除main
方法之外的其他地方,这意味着您可能想在另一个应用程序中重用此合成代码。下面是代码:
interface InfoLoader {
RatingInfo loadRatingInfo();
CustomerInfo loadCustomerInfo();
MovieInfo loadMovieInfo();
}
class FileInfoLoader implements InfoLoader {
public RatingInfo loadRatingInfo() {
FileParser ratingsFile = new FileParser("ratings.dat", "::");
RatingsMapper ratingsMapper = new RatingsMapper(ratingsFile, 4);
return new RatingInfo(ratingsMapper.getCustomerIDMovieIDRatingAndTimeMap());
}
public CustomerInfo loadCustomerInfo () {
FileParser customerFile = new FileParser("users.dat", "::");
CustomerMapper customerMapper = new CustomerMapper(customerFile, 5);
return new CustomerInfo(customerMapper.getIdCustomerMap());
}
public MovieInfo loadMovieInfo () {
FileParser movieFile = new FileParser("movies.dat", "::");
MovieMapper movieMapper = new MovieMapper(movieFile, 3);
return new MovieInfo(movieMapper.getIdMovieMap());
}
}
class App {
public App(InfoLoader infoLoader) {
this.infoLoader = infoLoader;
}
public void start() {
Statistics stat = new Statistics(
infoLoader.loadCustomerInfo(),
infoLoader.loadMovieInfo(),
infoLoader.loadRatingInfo()
);
DisplayStatistics ds = new DisplayStatistics(stat);
UserInterface ui = new UserInterface(ds);
ui.start();
}
private InfoLoader infoLoader;
}
public class Application {
public static void main(String[] args) {
InfoLoader infoLoader = new FileInfoLoader();
// or InfoLoader infoLoader = new SqlInfoLoader();
App app = new App(infoLoader);
app.start();
}
}