mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 16:58:56 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -28,28 +28,20 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
*A class used to store the information of album.
|
||||
*/
|
||||
/** A class used to store the information of album. */
|
||||
@Setter
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public class Album {
|
||||
/**
|
||||
* the title of the album.
|
||||
*/
|
||||
/** the title of the album. */
|
||||
private String title;
|
||||
/**
|
||||
* the artist name of the album.
|
||||
*/
|
||||
|
||||
/** the artist name of the album. */
|
||||
private String artist;
|
||||
/**
|
||||
* is the album classical, true or false.
|
||||
*/
|
||||
|
||||
/** is the album classical, true or false. */
|
||||
private boolean isClassical;
|
||||
/**
|
||||
* only when the album is classical,
|
||||
* composer can have content.
|
||||
*/
|
||||
|
||||
/** only when the album is classical, composer can have content. */
|
||||
private String composer;
|
||||
}
|
||||
|
||||
@@ -27,16 +27,13 @@ package com.iluwatar.presentationmodel;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* The Presentation model pattern is used to divide the presentation and controlling.
|
||||
* This demo is a used to information of some albums with GUI.
|
||||
* The Presentation model pattern is used to divide the presentation and controlling. This demo is a
|
||||
* used to information of some albums with GUI.
|
||||
*/
|
||||
@Slf4j
|
||||
public final class App {
|
||||
/**
|
||||
* the constructor.
|
||||
*/
|
||||
private App() {
|
||||
}
|
||||
/** the constructor. */
|
||||
private App() {}
|
||||
|
||||
/**
|
||||
* main method.
|
||||
@@ -48,4 +45,3 @@ public final class App {
|
||||
view.createView();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+8
-17
@@ -29,21 +29,14 @@ import java.util.List;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* a class used to deal with albums.
|
||||
*
|
||||
*/
|
||||
/** a class used to deal with albums. */
|
||||
@Slf4j
|
||||
@Getter
|
||||
public class DisplayedAlbums {
|
||||
/**
|
||||
* albums a list of albums.
|
||||
*/
|
||||
/** albums a list of albums. */
|
||||
private final List<Album> albums;
|
||||
|
||||
/**
|
||||
* a constructor method.
|
||||
*/
|
||||
/** a constructor method. */
|
||||
public DisplayedAlbums() {
|
||||
this.albums = new ArrayList<>();
|
||||
}
|
||||
@@ -51,15 +44,13 @@ public class DisplayedAlbums {
|
||||
/**
|
||||
* a method used to add a new album to album list.
|
||||
*
|
||||
* @param title the title of the album.
|
||||
* @param artist the artist name of the album.
|
||||
* @param title the title of the album.
|
||||
* @param artist the artist name of the album.
|
||||
* @param isClassical is the album classical, true or false.
|
||||
* @param composer only when the album is classical,
|
||||
* composer can have content.
|
||||
* @param composer only when the album is classical, composer can have content.
|
||||
*/
|
||||
public void addAlbums(final String title,
|
||||
final String artist, final boolean isClassical,
|
||||
final String composer) {
|
||||
public void addAlbums(
|
||||
final String title, final String artist, final boolean isClassical, final String composer) {
|
||||
if (isClassical) {
|
||||
this.albums.add(new Album(title, artist, true, composer));
|
||||
} else {
|
||||
|
||||
+25
-30
@@ -26,22 +26,16 @@ package com.iluwatar.presentationmodel;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* The class between view and albums, it is used to control the data.
|
||||
*/
|
||||
/** The class between view and albums, it is used to control the data. */
|
||||
@Slf4j
|
||||
public class PresentationModel {
|
||||
/**
|
||||
* the data of all albums that will be shown.
|
||||
*/
|
||||
/** the data of all albums that will be shown. */
|
||||
private final DisplayedAlbums data;
|
||||
/**
|
||||
* the no of selected album.
|
||||
*/
|
||||
|
||||
/** the no of selected album. */
|
||||
private int selectedAlbumNumber;
|
||||
/**
|
||||
* the selected album.
|
||||
*/
|
||||
|
||||
/** the selected album. */
|
||||
private Album selectedAlbum;
|
||||
|
||||
/**
|
||||
@@ -50,17 +44,23 @@ public class PresentationModel {
|
||||
* @return a instance of DsAlbum which store the data.
|
||||
*/
|
||||
public static DisplayedAlbums albumDataSet() {
|
||||
var titleList = new String[]{"HQ", "The Rough Dancer and Cyclical Night",
|
||||
"The Black Light", "Symphony No.5"};
|
||||
var artistList = new String[]{"Roy Harper", "Astor Piazzola",
|
||||
"The Black Light", "CBSO"};
|
||||
var isClassicalList = new boolean[]{false, false, false, true};
|
||||
var composerList = new String[]{null, null, null, "Sibelius"};
|
||||
var titleList =
|
||||
new String[] {
|
||||
"HQ", "The Rough Dancer and Cyclical Night",
|
||||
"The Black Light", "Symphony No.5"
|
||||
};
|
||||
var artistList =
|
||||
new String[] {
|
||||
"Roy Harper", "Astor Piazzola",
|
||||
"The Black Light", "CBSO"
|
||||
};
|
||||
var isClassicalList = new boolean[] {false, false, false, true};
|
||||
var composerList = new String[] {null, null, null, "Sibelius"};
|
||||
|
||||
var result = new DisplayedAlbums();
|
||||
for (var i = 1; i <= titleList.length; i++) {
|
||||
result.addAlbums(titleList[i - 1], artistList[i - 1],
|
||||
isClassicalList[i - 1], composerList[i - 1]);
|
||||
result.addAlbums(
|
||||
titleList[i - 1], artistList[i - 1], isClassicalList[i - 1], composerList[i - 1]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -82,8 +82,7 @@ public class PresentationModel {
|
||||
* @param albumNumber the number of album which is shown on the view.
|
||||
*/
|
||||
public void setSelectedAlbumNumber(final int albumNumber) {
|
||||
LOGGER.info("Change select number from {} to {}",
|
||||
this.selectedAlbumNumber, albumNumber);
|
||||
LOGGER.info("Change select number from {} to {}", this.selectedAlbumNumber, albumNumber);
|
||||
this.selectedAlbumNumber = albumNumber;
|
||||
this.selectedAlbum = data.getAlbums().get(this.selectedAlbumNumber - 1);
|
||||
}
|
||||
@@ -103,8 +102,7 @@ public class PresentationModel {
|
||||
* @param value the title which user want to user.
|
||||
*/
|
||||
public void setTitle(final String value) {
|
||||
LOGGER.info("Change album title from {} to {}",
|
||||
selectedAlbum.getTitle(), value);
|
||||
LOGGER.info("Change album title from {} to {}", selectedAlbum.getTitle(), value);
|
||||
selectedAlbum.setTitle(value);
|
||||
}
|
||||
|
||||
@@ -123,8 +121,7 @@ public class PresentationModel {
|
||||
* @param value the name want artist to be.
|
||||
*/
|
||||
public void setArtist(final String value) {
|
||||
LOGGER.info("Change album artist from {} to {}",
|
||||
selectedAlbum.getArtist(), value);
|
||||
LOGGER.info("Change album artist from {} to {}", selectedAlbum.getArtist(), value);
|
||||
selectedAlbum.setArtist(value);
|
||||
}
|
||||
|
||||
@@ -143,8 +140,7 @@ public class PresentationModel {
|
||||
* @param value is the album classical.
|
||||
*/
|
||||
public void setIsClassical(final boolean value) {
|
||||
LOGGER.info("Change album isClassical from {} to {}",
|
||||
selectedAlbum.isClassical(), value);
|
||||
LOGGER.info("Change album isClassical from {} to {}", selectedAlbum.isClassical(), value);
|
||||
selectedAlbum.setClassical(value);
|
||||
}
|
||||
|
||||
@@ -164,8 +160,7 @@ public class PresentationModel {
|
||||
*/
|
||||
public void setComposer(final String value) {
|
||||
if (selectedAlbum.isClassical()) {
|
||||
LOGGER.info("Change album composer from {} to {}",
|
||||
selectedAlbum.getComposer(), value);
|
||||
LOGGER.info("Change album composer from {} to {}", selectedAlbum.getComposer(), value);
|
||||
selectedAlbum.setComposer(value);
|
||||
} else {
|
||||
LOGGER.info("Composer can not be changed");
|
||||
|
||||
@@ -35,70 +35,52 @@ import javax.swing.JList;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* Generates the GUI of albums.
|
||||
*/
|
||||
/** Generates the GUI of albums. */
|
||||
@Getter
|
||||
@Slf4j
|
||||
public class View {
|
||||
/**
|
||||
* the model that controls this view.
|
||||
*/
|
||||
/** the model that controls this view. */
|
||||
private final PresentationModel model;
|
||||
|
||||
/**
|
||||
* the filed to show and modify title.
|
||||
*/
|
||||
/** the filed to show and modify title. */
|
||||
private TextField txtTitle;
|
||||
/**
|
||||
* the filed to show and modify the name of artist.
|
||||
*/
|
||||
|
||||
/** the filed to show and modify the name of artist. */
|
||||
private TextField txtArtist;
|
||||
/**
|
||||
* the checkbox for is classical.
|
||||
*/
|
||||
|
||||
/** the checkbox for is classical. */
|
||||
private JCheckBox chkClassical;
|
||||
/**
|
||||
* the filed to show and modify composer.
|
||||
*/
|
||||
|
||||
/** the filed to show and modify composer. */
|
||||
private TextField txtComposer;
|
||||
/**
|
||||
* a list to show all the name of album.
|
||||
*/
|
||||
|
||||
/** a list to show all the name of album. */
|
||||
private JList<String> albumList;
|
||||
/**
|
||||
* a button to apply of all the change.
|
||||
*/
|
||||
|
||||
/** a button to apply of all the change. */
|
||||
private JButton apply;
|
||||
/**
|
||||
* roll back the change.
|
||||
*/
|
||||
|
||||
/** roll back the change. */
|
||||
private JButton cancel;
|
||||
|
||||
/**
|
||||
* the value of the text field size.
|
||||
*/
|
||||
/** the value of the text field size. */
|
||||
static final int WIDTH_TXT = 200;
|
||||
|
||||
static final int HEIGHT_TXT = 50;
|
||||
|
||||
/**
|
||||
* the value of the GUI size and location.
|
||||
*/
|
||||
/** the value of the GUI size and location. */
|
||||
static final int LOCATION_X = 200;
|
||||
|
||||
static final int LOCATION_Y = 200;
|
||||
static final int WIDTH = 500;
|
||||
static final int HEIGHT = 300;
|
||||
|
||||
/**
|
||||
* constructor method.
|
||||
*/
|
||||
/** constructor method. */
|
||||
public View() {
|
||||
model = new PresentationModel(PresentationModel.albumDataSet());
|
||||
}
|
||||
|
||||
/**
|
||||
* save the data to PresentationModel.
|
||||
*/
|
||||
/** save the data to PresentationModel. */
|
||||
public void saveToMod() {
|
||||
LOGGER.info("Save data to PresentationModel");
|
||||
model.setArtist(txtArtist.getText());
|
||||
@@ -107,9 +89,7 @@ public class View {
|
||||
model.setComposer(txtComposer.getText());
|
||||
}
|
||||
|
||||
/**
|
||||
* load the data from PresentationModel.
|
||||
*/
|
||||
/** load the data from PresentationModel. */
|
||||
public void loadFromMod() {
|
||||
LOGGER.info("Load data from PresentationModel");
|
||||
txtArtist.setText(model.getArtist());
|
||||
@@ -119,22 +99,21 @@ public class View {
|
||||
txtComposer.setText(model.getComposer());
|
||||
}
|
||||
|
||||
/**
|
||||
* initialize the GUI.
|
||||
*/
|
||||
/** initialize the GUI. */
|
||||
public void createView() {
|
||||
var frame = new JFrame("Album");
|
||||
var b1 = Box.createHorizontalBox();
|
||||
|
||||
frame.add(b1);
|
||||
albumList = new JList<>(model.getAlbumList());
|
||||
albumList.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(final MouseEvent e) {
|
||||
model.setSelectedAlbumNumber(albumList.getSelectedIndex() + 1);
|
||||
loadFromMod();
|
||||
}
|
||||
});
|
||||
albumList.addMouseListener(
|
||||
new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(final MouseEvent e) {
|
||||
model.setSelectedAlbumNumber(albumList.getSelectedIndex() + 1);
|
||||
loadFromMod();
|
||||
}
|
||||
});
|
||||
b1.add(albumList);
|
||||
|
||||
var b2 = Box.createVerticalBox();
|
||||
@@ -148,30 +127,33 @@ public class View {
|
||||
|
||||
chkClassical = new JCheckBox();
|
||||
txtComposer = new TextField();
|
||||
chkClassical.addActionListener(itemEvent -> {
|
||||
txtComposer.setEditable(chkClassical.isSelected());
|
||||
if (!chkClassical.isSelected()) {
|
||||
txtComposer.setText("");
|
||||
}
|
||||
});
|
||||
chkClassical.addActionListener(
|
||||
itemEvent -> {
|
||||
txtComposer.setEditable(chkClassical.isSelected());
|
||||
if (!chkClassical.isSelected()) {
|
||||
txtComposer.setText("");
|
||||
}
|
||||
});
|
||||
txtComposer.setSize(WIDTH_TXT, HEIGHT_TXT);
|
||||
txtComposer.setEditable(model.getIsClassical());
|
||||
|
||||
apply = new JButton("Apply");
|
||||
apply.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(final MouseEvent e) {
|
||||
saveToMod();
|
||||
loadFromMod();
|
||||
}
|
||||
});
|
||||
apply.addMouseListener(
|
||||
new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(final MouseEvent e) {
|
||||
saveToMod();
|
||||
loadFromMod();
|
||||
}
|
||||
});
|
||||
cancel = new JButton("Cancel");
|
||||
cancel.addMouseListener(new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(final MouseEvent e) {
|
||||
loadFromMod();
|
||||
}
|
||||
});
|
||||
cancel.addMouseListener(
|
||||
new MouseAdapter() {
|
||||
@Override
|
||||
public void mouseClicked(final MouseEvent e) {
|
||||
loadFromMod();
|
||||
}
|
||||
});
|
||||
|
||||
b2.add(txtArtist);
|
||||
b2.add(txtTitle);
|
||||
@@ -186,5 +168,4 @@ public class View {
|
||||
frame.setBounds(LOCATION_X, LOCATION_Y, WIDTH, HEIGHT);
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user