1b: composite pattern
This commit is contained in:
parent
64c1d0e743
commit
05650caa2b
@ -17,61 +17,8 @@ public class Client {
|
|||||||
.addImage(new Image("Missing cat", 1280, 720, "anonymous")));
|
.addImage(new Image("Missing cat", 1280, 720, "anonymous")));
|
||||||
|
|
||||||
System.out.println("\n===List===\n");
|
System.out.println("\n===List===\n");
|
||||||
System.out.print(getOverviewInformation(masterCollection));
|
System.out.print(masterCollection.getOverview());
|
||||||
System.out.println("\n===Contents===\n");
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package model;
|
package model;
|
||||||
|
|
||||||
public class Article {
|
public class Article extends Item {
|
||||||
private String title;
|
private String title;
|
||||||
private String author;
|
private String author;
|
||||||
private String content;
|
private String content;
|
||||||
@ -22,7 +22,21 @@ public class Article {
|
|||||||
this.title = title;
|
this.title = title;
|
||||||
this.author = author;
|
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.
|
* Get the title of the article.
|
||||||
* @return title
|
* @return title
|
||||||
|
@ -1,66 +1,88 @@
|
|||||||
package model;
|
package model;
|
||||||
|
|
||||||
public class Image {
|
public class Image extends Item {
|
||||||
private String title;
|
private String title;
|
||||||
private String author;
|
private String author;
|
||||||
private int width;
|
private int width;
|
||||||
private int height;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the height of the image.
|
* Creates an image with the given title and author and a resolution
|
||||||
* @return height
|
* defined by width and height.
|
||||||
*/
|
*
|
||||||
public int getHeight() {
|
* @param title
|
||||||
return height;
|
* @param width
|
||||||
}
|
* @param height
|
||||||
|
* @param author
|
||||||
/**
|
*/
|
||||||
* Renders the image as String and return the rendered image.
|
public Image(String title, int width, int height, String author) {
|
||||||
* @return String representing the rendered image
|
this.title = title;
|
||||||
*/
|
this.width = width;
|
||||||
public String renderToString() {
|
this.height = height;
|
||||||
/*
|
this.author = author;
|
||||||
* Dummy implementation
|
}
|
||||||
*/
|
|
||||||
return " /\\_/\\ \n( o.o )\n > ^ < ";
|
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 > ^ < ";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
6
ueb05_news_example/src/model/Item.java
Normal file
6
ueb05_news_example/src/model/Item.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package model;
|
||||||
|
|
||||||
|
public abstract class Item {
|
||||||
|
public abstract String getOverview();
|
||||||
|
public abstract String getDetails();
|
||||||
|
}
|
@ -3,7 +3,7 @@ package model;
|
|||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class NewsCollection {
|
public class NewsCollection extends Item {
|
||||||
|
|
||||||
private String topic;
|
private String topic;
|
||||||
private List<Article> articles = new LinkedList<Article>();
|
private List<Article> articles = new LinkedList<Article>();
|
||||||
@ -13,7 +13,52 @@ public class NewsCollection {
|
|||||||
public NewsCollection(String topic) {
|
public NewsCollection(String topic) {
|
||||||
this.topic = 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.
|
* Get the topic of the collection.
|
||||||
* @return
|
* @return
|
||||||
|
Loading…
x
Reference in New Issue
Block a user