mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 16:59:24 +00:00
Added new design pattern.
Implementation for the Model-View-Presenter pattern is now available.
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Every instance of this class represents the Presenter component
|
||||
* in the Model-View-Presenter architectural pattern.
|
||||
*
|
||||
* It is responsible for reacting to the user's actions and update the View component.
|
||||
*/
|
||||
public class FileSelectorPresenter {
|
||||
|
||||
/**
|
||||
* The View component that the presenter interacts with.
|
||||
*/
|
||||
private FileSelectorView view;
|
||||
|
||||
/**
|
||||
* The Model component that the presenter interacts with.
|
||||
*/
|
||||
private FileLoader loader;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param view The view component that the presenter will interact with.
|
||||
*/
|
||||
public FileSelectorPresenter(FileSelectorView view) {
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the FileLoader object, to the value given as parameter.
|
||||
*
|
||||
* @param loader The new FileLoader object(the Model component).
|
||||
*/
|
||||
public void setLoader(FileLoader loader) {
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the presenter.
|
||||
*/
|
||||
public void start() {
|
||||
view.setPresenter(this);
|
||||
view.open();
|
||||
}
|
||||
|
||||
/**
|
||||
* An "event" that fires when the name of the file to be loaded changes.
|
||||
*/
|
||||
public void fileNameChanged() {
|
||||
loader.setFileName(view.getFileName());
|
||||
}
|
||||
|
||||
public void confirmed() {
|
||||
if(loader.getFileName() == null || loader.getFileName().equals("")) {
|
||||
view.showMessage("Please give the name of the file first!");
|
||||
return;
|
||||
}
|
||||
|
||||
if(loader.fileExists()) {
|
||||
String data = loader.loadData();
|
||||
view.displayData(data);
|
||||
}
|
||||
|
||||
else {
|
||||
view.showMessage("The file specified does not exist.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the file loading process.
|
||||
*/
|
||||
public void cancelled() {
|
||||
view.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user