mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 18:59:10 +00:00
refactoring: Rename presentation to presentation model (#2291)
* Rename presentation to presentation-model * Rename image * Rename UML diagram * Rename presentation to presentation-model * Rename package to presentationmodel * Rename module reference in pom.xml
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 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.presentationmodel;
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 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.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.
|
||||
*/
|
||||
@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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 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.presentationmodel;
|
||||
|
||||
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, ""));
|
||||
}
|
||||
}
|
||||
}
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 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.presentationmodel;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
|
||||
*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2022 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.presentationmodel;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user