1b: composite pattern

This commit is contained in:
Daniel Langbein 2024-11-24 18:37:47 +01:00
parent 64c1d0e743
commit 05650caa2b
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
5 changed files with 155 additions and 121 deletions

View File

@ -17,61 +17,8 @@ public class Client {
.addImage(new Image("Missing cat", 1280, 720, "anonymous")));
System.out.println("\n===List===\n");
System.out.print(getOverviewInformation(masterCollection));
System.out.print(masterCollection.getOverview());
System.out.println("\n===Contents===\n");
System.out.print(getDetailedInformation(masterCollection));
System.out.print(masterCollection.getDetails());
}
/**
* Gets overview information about all directly or transitively
* referenced items in the given collection.
*/
public static String getOverviewInformation(NewsCollection rootCollection) {
StringBuilder builder = new StringBuilder();
builder.append("###" + rootCollection.getTopic() + "###");
builder.append("\n");
for (Article article : rootCollection.getArticles()) {
builder.append("Article: " + article.getTitle() + ", Author: "
+ article.getAuthor());
builder.append("\n");
}
for (Image image : rootCollection.getImages()) {
builder.append("Image: " + image.getTitle()
+ ", Resolution: " + image.getWidth()
+ "x" + image.getHeight()
+ ", Author: " + image.getAuthor());
builder.append("\n");
}
for (NewsCollection collection : rootCollection.getCollections()) {
builder.append(getOverviewInformation(collection));
builder.append("\n");
}
return builder.toString();
}
/**
* Gets the contents of all directly or transitively referenced items in
* the given collection.
*/
public static String getDetailedInformation(NewsCollection rootCollection) {
StringBuilder builder = new StringBuilder();
builder.append("###" + rootCollection.getTopic() + "###");
builder.append("\n");
for (Article article : rootCollection.getArticles()) {
builder.append("###" + article.getTitle() + "###");
builder.append("\n");
builder.append(article.getContent());
builder.append("\n\n");
}
for (Image image : rootCollection.getImages()) {
builder.append("###" + image.getTitle() + "###");
builder.append("\n");
builder.append(image.renderToString());
builder.append("\n\n");
}
for (NewsCollection collection : rootCollection.getCollections()) {
builder.append(getDetailedInformation(collection));
}
return builder.toString();
}
}

View File

@ -1,6 +1,6 @@
package model;
public class Article {
public class Article extends Item {
private String title;
private String author;
private String content;
@ -22,7 +22,21 @@ public class Article {
this.title = title;
this.author = author;
}
public String getOverview(){
return "Article: " + getTitle() + ", Author: "
+ getAuthor();
}
public String getDetails(){
return "###" + getTitle() + "###"
+ "\n"
+ getContent()
+ "\n\n";
}
//=================================
/**
* Get the title of the article.
* @return title

View File

@ -1,66 +1,88 @@
package model;
public class Image {
private String title;
private String author;
private int width;
private int height;
/**
* Creates an image with the given title and author and a resolution
* defined by width and height.
* @param title
* @param width
* @param height
* @param author
*/
public Image(String title, int width, int height, String author) {
this.title = title;
this.width = width;
this.height = height;
this.author = author;
}
/**
* Get the title of the image.
* @return title
*/
public String getTitle() {
return title;
}
/**
* Get the author of the image.
* @return author
*/
public String getAuthor() {
return author;
}
/**
* Get the width of the image.
* @return width
*/
public int getWidth() {
return width;
}
public class Image extends Item {
private String title;
private String author;
private int width;
private int height;
/**
* Get the height of the image.
* @return height
*/
public int getHeight() {
return height;
}
/**
* Renders the image as String and return the rendered image.
* @return String representing the rendered image
*/
public String renderToString() {
/*
* Dummy implementation
*/
return " /\\_/\\ \n( o.o )\n > ^ < ";
}
/**
* Creates an image with the given title and author and a resolution
* defined by width and height.
*
* @param title
* @param width
* @param height
* @param author
*/
public Image(String title, int width, int height, String author) {
this.title = title;
this.width = width;
this.height = height;
this.author = author;
}
public String getOverview() {
return "Image: " + getTitle()
+ ", Resolution: " + getWidth()
+ "x" + getHeight()
+ ", Author: " + getAuthor();
}
public String getDetails(){
return "###" + getTitle() + "###"
+ "\n"
+ getContent()
+ "\n\n";
}
//=================================
/**
* Get the title of the image.
*
* @return title
*/
public String getTitle() {
return title;
}
/**
* Get the author of the image.
*
* @return author
*/
public String getAuthor() {
return author;
}
/**
* Get the width of the image.
*
* @return width
*/
public int getWidth() {
return width;
}
/**
* Get the height of the image.
*
* @return height
*/
public int getHeight() {
return height;
}
/**
* Renders the image as String and return the rendered image.
*
* @return String representing the rendered image
*/
public String getContent() {
/*
* Dummy implementation
*/
return " /\\_/\\ \n( o.o )\n > ^ < ";
}
}

View File

@ -0,0 +1,6 @@
package model;
public abstract class Item {
public abstract String getOverview();
public abstract String getDetails();
}

View File

@ -3,7 +3,7 @@ package model;
import java.util.LinkedList;
import java.util.List;
public class NewsCollection {
public class NewsCollection extends Item {
private String topic;
private List<Article> articles = new LinkedList<Article>();
@ -13,7 +13,52 @@ public class NewsCollection {
public NewsCollection(String topic) {
this.topic = topic;
}
/**
* Gets overview information about all directly or transitively
* referenced items in the given collection.
*/
public String getOverview(){
StringBuilder builder = new StringBuilder();
builder.append("###" + getTopic() + "###");
builder.append("\n");
for (Article article : getArticles()) {
builder.append(article.getOverview());
builder.append("\n");
}
for (Image image : getImages()) {
builder.append(image.getOverview());
builder.append("\n");
}
for (NewsCollection collection : getCollections()) {
builder.append(collection.getOverview());
builder.append("\n");
}
return builder.toString();
}
/**
* Gets the contents of all directly or transitively referenced items in
* the given collection.
*/
public String getDetails(){
StringBuilder builder = new StringBuilder();
builder.append("###" + getTopic() + "###");
builder.append("\n");
for (Article article : getArticles()) {
builder.append(article.getDetails());
}
for (Image image : getImages()) {
builder.append(image.getDetails());
}
for (NewsCollection collection : getCollections()) {
builder.append(collection.getDetails());
}
return builder.toString();
}
//=================================
/**
* Get the topic of the collection.
* @return