feature: Implement Presentation Model Pattern (#1710)

* #415 initial all componets

* #415 add src and tests

* #415 add diagram

* #415 add README

* #415 add README

* #415 change pom.xml

* #415 change pom.xml

* #415 change pom.xml

* #415 update pom.xml

* #415 change some code smell

* #415 change some code smell

* #415 update test

* #415 add javadoc

* #415 remove author tag

* #415 add lombok @AllArgsConstructor

* #415 fix code converge

* #415 fix code converge

* #415 fix code converge

* #415 add javadoc

* #415 fix code smell

* #415 fix code smell

* #415 add log information

* #415 remove unused import

* #415 add javadoc and more test

* #415 modify test

* #415 fix checkstyle

* #415 remove useless code and add more javadoc and test.

* #415 add package-info.java.

* #415 add package-info.java.

* #415 add more test.

* #415 fix code smell.

* #415 fix code smell and increase code coverage.

* #415 fix code smell.

* #415 update README.md

* #415 update README.md

* #415 make this demo better

* #415 satisfy checkstyle

* #415 make some field static.

* #415 make some fields static.

* #415 rename some fields static.

* Delete package-info.java

Co-authored-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
This commit is contained in:
EdisonE3
2021-05-11 21:46:11 +08:00
committed by GitHub
parent 1388e38744
commit f1feb3f6a0
16 changed files with 1023 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
package com.iluwatar.presentation;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
/**
*A class used to store the information of album.
*/
@Setter
@Getter
@AllArgsConstructor
public class Album {
/**
* the title of the album.
*/
private String title;
/**
* the artist name of the album.
*/
private String artist;
/**
* is the album classical, true or false.
*/
private boolean isClassical;
/**
* only when the album is classical,
* composer can have content.
*/
private String composer;
}

View File

@@ -0,0 +1,27 @@
package com.iluwatar.presentation;
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.
*/
@Slf4j
public final class App {
/**
* the constructor.
*/
private App() {
}
/**
* main method.
*
* @param args args
*/
public static void main(final String[] args) {
var view = new View();
view.createView();
}
}

View File

@@ -0,0 +1,46 @@
package com.iluwatar.presentation;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
/**
* a class used to deal with albums.
*
*/
@Slf4j
@Getter
public class DisplayedAlbums {
/**
* albums a list of albums.
*/
private final List<Album> albums;
/**
* a constructor method.
*/
public DisplayedAlbums() {
this.albums = new ArrayList<>();
}
/**
* 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 isClassical is the album classical, true or false.
* @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) {
if (isClassical) {
this.albums.add(new Album(title, artist, true, composer));
} else {
this.albums.add(new Album(title, artist, false, ""));
}
}
}

View File

@@ -0,0 +1,163 @@
package com.iluwatar.presentation;
import lombok.extern.slf4j.Slf4j;
/**
* 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.
*/
private final DisplayedAlbums data;
/**
* the no of selected album.
*/
private int selectedAlbumNumber;
/**
* the selected album.
*/
private Album selectedAlbum;
/**
* Generates a set of data for testing.
*
* @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 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]);
}
return result;
}
/**
* constructor method.
*
* @param dataOfAlbums the data of all the albums
*/
public PresentationModel(final DisplayedAlbums dataOfAlbums) {
this.data = dataOfAlbums;
this.selectedAlbumNumber = 1;
this.selectedAlbum = this.data.getAlbums().get(0);
}
/**
* Changes the value of selectedAlbumNumber.
*
* @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);
this.selectedAlbumNumber = albumNumber;
this.selectedAlbum = data.getAlbums().get(this.selectedAlbumNumber - 1);
}
/**
* get the title of selected album.
*
* @return the tile of selected album.
*/
public String getTitle() {
return selectedAlbum.getTitle();
}
/**
* set the title of selected album.
*
* @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);
selectedAlbum.setTitle(value);
}
/**
* get the artist of selected album.
*
* @return the artist of selected album.
*/
public String getArtist() {
return selectedAlbum.getArtist();
}
/**
* set the name of artist.
*
* @param value the name want artist to be.
*/
public void setArtist(final String value) {
LOGGER.info("Change album artist from {} to {}",
selectedAlbum.getArtist(), value);
selectedAlbum.setArtist(value);
}
/**
* Gets a boolean value which represents whether the album is classical.
*
* @return is the album classical.
*/
public boolean getIsClassical() {
return selectedAlbum.isClassical();
}
/**
* set the isClassical of album.
*
* @param value is the album classical.
*/
public void setIsClassical(final boolean value) {
LOGGER.info("Change album isClassical from {} to {}",
selectedAlbum.isClassical(), value);
selectedAlbum.setClassical(value);
}
/**
* get is classical of the selected album.
*
* @return is the album classical.
*/
public String getComposer() {
return selectedAlbum.isClassical() ? selectedAlbum.getComposer() : "";
}
/**
* Sets the name of composer when the album is classical.
*
* @param value the name of composer.
*/
public void setComposer(final String value) {
if (selectedAlbum.isClassical()) {
LOGGER.info("Change album composer from {} to {}",
selectedAlbum.getComposer(), value);
selectedAlbum.setComposer(value);
} else {
LOGGER.info("Composer can not be changed");
}
}
/**
* Gets a list of albums.
*
* @return the names of all the albums.
*/
public String[] getAlbumList() {
var result = new String[data.getAlbums().size()];
for (var i = 0; i < result.length; i++) {
result[i] = data.getAlbums().get(i).getTitle();
}
return result;
}
}

View File

@@ -0,0 +1,167 @@
package com.iluwatar.presentation;
import java.awt.TextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JList;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
/**
* Generates the GUI of albums.
*/
@Getter
@Slf4j
public class View {
/**
* the model that controls this view.
*/
private final PresentationModel model;
/**
* the filed to show and modify title.
*/
private TextField txtTitle;
/**
* the filed to show and modify the name of artist.
*/
private TextField txtArtist;
/**
* the checkbox for is classical.
*/
private JCheckBox chkClassical;
/**
* the filed to show and modify composer.
*/
private TextField txtComposer;
/**
* a list to show all the name of album.
*/
private JList<String> albumList;
/**
* a button to apply of all the change.
*/
private JButton apply;
/**
* roll back the change.
*/
private JButton cancel;
/**
* 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.
*/
static final int LOCATION_X = 200;
static final int LOCATION_Y = 200;
static final int WIDTH = 500;
static final int HEIGHT = 300;
/**
* constructor method.
*/
public View() {
model = new PresentationModel(PresentationModel.albumDataSet());
}
/**
* save the data to PresentationModel.
*/
public void saveToPMod() {
LOGGER.info("Save data to PresentationModel");
model.setArtist(txtArtist.getText());
model.setTitle(txtTitle.getText());
model.setIsClassical(chkClassical.isSelected());
model.setComposer(txtComposer.getText());
}
/**
* load the data from PresentationModel.
*/
public void loadFromPMod() {
LOGGER.info("Load data from PresentationModel");
txtArtist.setText(model.getArtist());
txtTitle.setText(model.getTitle());
chkClassical.setSelected(model.getIsClassical());
txtComposer.setEditable(model.getIsClassical());
txtComposer.setText(model.getComposer());
}
/**
* 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);
loadFromPMod();
}
});
b1.add(albumList);
var b2 = Box.createVerticalBox();
b1.add(b2);
txtArtist = new TextField();
txtTitle = new TextField();
txtArtist.setSize(WIDTH_TXT, HEIGHT_TXT);
txtTitle.setSize(WIDTH_TXT, HEIGHT_TXT);
chkClassical = new JCheckBox();
txtComposer = new TextField();
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) {
saveToPMod();
loadFromPMod();
}
});
cancel = new JButton("Cancel");
cancel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent e) {
loadFromPMod();
}
});
b2.add(txtArtist);
b2.add(txtTitle);
b2.add(chkClassical);
b2.add(txtComposer);
b2.add(apply);
b2.add(cancel);
frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
frame.setBounds(LOCATION_X, LOCATION_Y, WIDTH, HEIGHT);
frame.setVisible(true);
}
}

View File

@@ -0,0 +1,37 @@
package com.iluwatar.presentation;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class AlbumTest {
@Test
void testSetTitle(){
Album album = new Album("a", "b", false, "");
album.setTitle("b");
assertEquals("b", album.getTitle());
}
@Test
void testSetArtist(){
Album album = new Album("a", "b", false, "");
album.setArtist("c");
assertEquals("c", album.getArtist());
}
@Test
void testSetClassical(){
Album album = new Album("a", "b", false, "");
album.setClassical(true);
assertTrue(album.isClassical());
}
@Test
void testSetComposer(){
Album album = new Album("a", "b", false, "");
album.setClassical(true);
album.setComposer("w");
assertEquals("w", album.getComposer());
}
}

View File

@@ -0,0 +1,42 @@
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.presentation;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
/**
* Issue: Add at least one assertion to this test case.
*
* Solution: Inserted assertion to check whether the execution of the main method in {@link App}
* throws an exception.
*/
class AppTest {
@Test
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@@ -0,0 +1,22 @@
package com.iluwatar.presentation;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DisplayedAlbumsTest {
@Test
void testAdd_true(){
DisplayedAlbums displayedAlbums = new DisplayedAlbums();
displayedAlbums.addAlbums("title", "artist", true, "composer");
assertEquals("composer", displayedAlbums.getAlbums().get(0).getComposer());
}
@Test
void testAdd_false(){
DisplayedAlbums displayedAlbums = new DisplayedAlbums();
displayedAlbums.addAlbums("title", "artist", false, "composer");
assertEquals("", displayedAlbums.getAlbums().get(0).getComposer());
}
}

View File

@@ -0,0 +1,117 @@
/*
* The MIT License
* Copyright © 2014-2021 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.presentation;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class PresentationTest {
String[] albumList = {"HQ", "The Rough Dancer and Cyclical Night", "The Black Light", "Symphony No.5"};
@Test
void testCreateAlbumList() {
PresentationModel model = new PresentationModel(PresentationModel.albumDataSet());
String[] list = model.getAlbumList();
assertEquals(Arrays.toString(albumList), Arrays.toString(list));
}
@Test
void testSetSelectedAlbumNumber_1() {
PresentationModel model = new PresentationModel(PresentationModel.albumDataSet());
final int selectId = 2;
model.setSelectedAlbumNumber(selectId);
assertEquals(albumList[selectId - 1], model.getTitle());
}
@Test
void testSetSelectedAlbumNumber_2() {
PresentationModel model = new PresentationModel(PresentationModel.albumDataSet());
final int selectId = 4;
model.setSelectedAlbumNumber(selectId);
assertEquals(albumList[selectId - 1], model.getTitle());
}
@Test
void testSetTitle_1() {
PresentationModel model = new PresentationModel(PresentationModel.albumDataSet());
String testTitle = "TestTile";
model.setTitle(testTitle);
assertEquals(testTitle, model.getTitle());
}
@Test
void testSetTitle_2() {
PresentationModel model = new PresentationModel(PresentationModel.albumDataSet());
String testTitle = "";
model.setTitle(testTitle);
assertEquals(testTitle, model.getTitle());
}
@Test
void testSetArtist_1() {
PresentationModel model = new PresentationModel(PresentationModel.albumDataSet());
String testArtist = "TestArtist";
model.setArtist(testArtist);
assertEquals(testArtist, model.getArtist());
}
@Test
void testSetArtist_2() {
PresentationModel model = new PresentationModel(PresentationModel.albumDataSet());
String testArtist = "";
model.setArtist(testArtist);
assertEquals(testArtist, model.getArtist());
}
@Test
void testSetIsClassical() {
PresentationModel model = new PresentationModel(PresentationModel.albumDataSet());
model.setIsClassical(true);
assertTrue(model.getIsClassical());
}
@Test
void testSetComposer_false() {
PresentationModel model = new PresentationModel(PresentationModel.albumDataSet());
String testComposer = "TestComposer";
model.setIsClassical(false);
model.setComposer(testComposer);
assertEquals("", model.getComposer());
}
@Test
void testSetComposer_true() {
PresentationModel model = new PresentationModel(PresentationModel.albumDataSet());
String testComposer = "TestComposer";
model.setIsClassical(true);
model.setComposer(testComposer);
assertEquals(testComposer, model.getComposer());
}
}

View File

@@ -0,0 +1,55 @@
package com.iluwatar.presentation;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
class ViewTest {
String[] albumList = {"HQ", "The Rough Dancer and Cyclical Night", "The Black Light", "Symphony No.5"};
@Test
void testSave_setArtistAndTitle(){
View view = new View();
view.createView();
String testTitle = "testTitle";
String testArtist = "testArtist";
view.getTxtArtist().setText(testArtist);
view.getTxtTitle().setText(testTitle);
view.saveToPMod();
view.loadFromPMod();
assertEquals(testTitle, view.getModel().getTitle());
assertEquals(testArtist, view.getModel().getArtist());
}
@Test
void testSave_setClassicalAndComposer(){
View view = new View();
view.createView();
boolean isClassical = true;
String testComposer = "testComposer";
view.getChkClassical().setSelected(isClassical);
view.getTxtComposer().setText(testComposer);
view.saveToPMod();
view.loadFromPMod();
assertTrue(view.getModel().getIsClassical());
assertEquals(testComposer, view.getModel().getComposer());
}
@Test
void testLoad_1(){
View view = new View();
view.createView();
view.getModel().setSelectedAlbumNumber(2);
view.loadFromPMod();
assertEquals(albumList[1], view.getModel().getTitle());
}
@Test
void testLoad_2(){
View view = new View();
view.createView();
view.getModel().setSelectedAlbumNumber(4);
view.loadFromPMod();
assertEquals(albumList[3], view.getModel().getTitle());
}
}