ueb07 a)
This commit is contained in:
parent
5b45264ce0
commit
5fc0f28281
@ -1,8 +1,6 @@
|
||||
package client;
|
||||
|
||||
import model.Article;
|
||||
import model.Image;
|
||||
import model.NewsCollection;
|
||||
import model.*;
|
||||
|
||||
public class Client {
|
||||
|
||||
@ -20,9 +18,13 @@ public class Client {
|
||||
.addItem(new Image("Missing cat", 1280, 720, "anonymous")));
|
||||
|
||||
System.out.println("\n===List===\n");
|
||||
System.out.print(masterCollection.getOverviewInformation());
|
||||
StringBuilder sb = new StringBuilder();
|
||||
masterCollection.accept(new OverviewPrinter(sb));
|
||||
System.out.print(sb);
|
||||
System.out.println("\n===Contents===\n");
|
||||
System.out.print(masterCollection.getDetailedInformation());
|
||||
sb = new StringBuilder();
|
||||
masterCollection.accept(new DetailedPrinter(sb));
|
||||
System.out.print(sb);
|
||||
System.out.println("\n===After change===\n");
|
||||
|
||||
changedArticle.setTitle("Harder, Better, Faster, Stronger? Doping controls loosened");
|
||||
|
@ -20,7 +20,12 @@ public class Article extends NewsItem {
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void accept(NewsItemVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the author of the article.
|
||||
* @return author
|
||||
@ -36,19 +41,4 @@ public class Article extends NewsItem {
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getOverviewInformation() {
|
||||
return "Article: " + this.getTitle() + ", Author: " + this.getAuthor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetailedInformation() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("###" + this.getTitle() + "###");
|
||||
builder.append("\n");
|
||||
builder.append(this.getContent());
|
||||
builder.append("\n");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
50
ueb07_news_example/src/model/DetailedPrinter.java
Normal file
50
ueb07_news_example/src/model/DetailedPrinter.java
Normal file
@ -0,0 +1,50 @@
|
||||
package model;
|
||||
|
||||
public class DetailedPrinter implements NewsItemVisitor {
|
||||
StringBuilder sb;
|
||||
|
||||
public DetailedPrinter(StringBuilder sb) {
|
||||
this.sb = sb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print details about news article.
|
||||
*/
|
||||
@Override
|
||||
public void visit(final Article article) {
|
||||
sb.append("###")
|
||||
.append(article.getTitle())
|
||||
.append("###");
|
||||
sb.append("\n");
|
||||
sb.append(article.getContent());
|
||||
sb.append("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Print details about image.
|
||||
*/
|
||||
@Override
|
||||
public void visit(final Image image) {
|
||||
sb.append("###")
|
||||
.append(image.getTitle())
|
||||
.append("###");
|
||||
sb.append("\n");
|
||||
sb.append(image.renderToString());
|
||||
sb.append("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Print details about news collection.
|
||||
*/
|
||||
@Override
|
||||
public void visit(final NewsCollection collection) {
|
||||
sb.append("###")
|
||||
.append(collection.getTitle())
|
||||
.append("###");
|
||||
sb.append("\n");
|
||||
for (NewsItem item : collection.getNewsItems()) {
|
||||
item.accept(this);
|
||||
sb.append("\n");
|
||||
}
|
||||
}
|
||||
}
|
@ -19,6 +19,11 @@ public class Image extends NewsItem {
|
||||
this.height = height;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(NewsItemVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the author of the image.
|
||||
@ -54,26 +59,4 @@ public class Image extends NewsItem {
|
||||
*/
|
||||
return " /\\_/\\ \n( o.o )\n > ^ < ";
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a list showing information about all directly or transitively
|
||||
* referenced items in the given collection.
|
||||
*/
|
||||
@Override
|
||||
public String getOverviewInformation() {
|
||||
return "Image: " + this.getTitle()
|
||||
+ ", Resolution: " + this.getWidth()
|
||||
+ "x" + this.getHeight()
|
||||
+ ", Author: " + this.getAuthor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetailedInformation() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("###" + this.getTitle() + "###");
|
||||
builder.append("\n");
|
||||
builder.append(renderToString());
|
||||
builder.append("\n");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,12 @@ public class NewsCollection extends NewsItem {
|
||||
public NewsCollection(String topic) {
|
||||
this.title = topic;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void accept(NewsItemVisitor visitor) {
|
||||
visitor.visit(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of items stored in this collection.
|
||||
* @return items stored in this collection
|
||||
@ -28,31 +33,4 @@ public class NewsCollection extends NewsItem {
|
||||
newsItems.add(item);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a list showing information about all directly or transitively
|
||||
* referenced items in the given collection.
|
||||
*/
|
||||
public String getOverviewInformation() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("###" + this.getTitle() + "###");
|
||||
builder.append("\n");
|
||||
for (NewsItem item : this.getNewsItems()) {
|
||||
builder.append(item.getOverviewInformation());
|
||||
builder.append("\n");
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDetailedInformation() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("###" + this.getTitle() + "###");
|
||||
builder.append("\n");
|
||||
for (NewsItem item : this.getNewsItems()) {
|
||||
builder.append(item.getDetailedInformation());
|
||||
builder.append("\n");
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,8 @@ public abstract class NewsItem {
|
||||
super();
|
||||
}
|
||||
|
||||
public abstract void accept(NewsItemVisitor visitor);
|
||||
|
||||
/**
|
||||
* Get the topic of the collection.
|
||||
* @return
|
||||
@ -19,14 +21,4 @@ public abstract class NewsItem {
|
||||
public void setTitle(String newTitle) {
|
||||
title = newTitle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get overview information about the news item.
|
||||
*/
|
||||
public abstract String getOverviewInformation();
|
||||
|
||||
/**
|
||||
* Get the contents of the news item.
|
||||
*/
|
||||
public abstract String getDetailedInformation();
|
||||
}
|
7
ueb07_news_example/src/model/NewsItemVisitor.java
Normal file
7
ueb07_news_example/src/model/NewsItemVisitor.java
Normal file
@ -0,0 +1,7 @@
|
||||
package model;
|
||||
|
||||
public interface NewsItemVisitor {
|
||||
void visit(final Article article);
|
||||
void visit(final Image image);
|
||||
void visit(final NewsCollection collection);
|
||||
}
|
52
ueb07_news_example/src/model/OverviewPrinter.java
Normal file
52
ueb07_news_example/src/model/OverviewPrinter.java
Normal file
@ -0,0 +1,52 @@
|
||||
package model;
|
||||
|
||||
public class OverviewPrinter implements NewsItemVisitor {
|
||||
final StringBuilder sb;
|
||||
|
||||
/**
|
||||
* @param sb StringBuilder to print overview into.
|
||||
*/
|
||||
public OverviewPrinter(StringBuilder sb) {
|
||||
this.sb = sb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print overview about news article.
|
||||
*/
|
||||
@Override
|
||||
public void visit(final Article article) {
|
||||
sb.append("Article: ")
|
||||
.append(article.getTitle())
|
||||
.append(", Author: ")
|
||||
.append(article.getAuthor());
|
||||
}
|
||||
|
||||
/**
|
||||
* Print overview about image.
|
||||
*/
|
||||
@Override
|
||||
public void visit(final Image image) {
|
||||
sb.append("Image: ")
|
||||
.append(image.getTitle())
|
||||
.append(", Resolution: ")
|
||||
.append(image.getWidth())
|
||||
.append("x")
|
||||
.append(image.getHeight())
|
||||
.append(", Author: ")
|
||||
.append(image.getAuthor());
|
||||
}
|
||||
|
||||
/**
|
||||
* Print overview about news collection.
|
||||
*/
|
||||
@Override
|
||||
public void visit(final NewsCollection collection) {
|
||||
sb.append("###")
|
||||
.append(collection.getTitle())
|
||||
.append("###\n");
|
||||
for (NewsItem item : collection.getNewsItems()) {
|
||||
item.accept(this);
|
||||
sb.append("\n");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user