1b: composite pattern

This commit is contained in:
Daniel Langbein 2024-11-24 19:09:45 +01:00
parent 67e5565b54
commit 3f30173a5b
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
2 changed files with 20 additions and 72 deletions

View File

@ -8,13 +8,13 @@ public class Client {
public static void main(String[] args) {
NewsCollection masterCollection = new NewsCollection("Daily news");
masterCollection.addCollection(new NewsCollection("Sports")
.addArticle(new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups"))
.addImage(new Image("Athletic cat contest", 800, 600, "zdf")))
.addCollection(new NewsCollection("Local news")
.addArticle(new Article("Missing Cat missed.", "mfg"))
.addArticle(new Article("Corrupted local politician selling cats.", "ard"))
.addImage(new Image("Missing cat", 1280, 720, "anonymous")));
masterCollection.addItem(new NewsCollection("Sports")
.addItem(new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups"))
.addItem(new Image("Athletic cat contest", 800, 600, "zdf")))
.addItem(new NewsCollection("Local news")
.addItem(new Article("Missing Cat missed.", "mfg"))
.addItem(new Article("Corrupted local politician selling cats.", "ard"))
.addItem(new Image("Missing cat", 1280, 720, "anonymous")));
System.out.println("\n===List===\n");
System.out.print(masterCollection.getOverview());

View File

@ -6,9 +6,7 @@ import java.util.List;
public class NewsCollection extends Item {
private String topic;
private List<Article> articles = new LinkedList<Article>();
private List<Image> images = new LinkedList<Image>();
private List<NewsCollection> collections = new LinkedList<NewsCollection>();
private List<Item> items = new LinkedList<>();
public NewsCollection(String topic) {
this.topic = topic;
@ -22,16 +20,8 @@ public class NewsCollection extends Item {
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());
for(Item item : getItems()){
builder.append(item.getOverview());
builder.append("\n");
}
return builder.toString();
@ -45,14 +35,8 @@ public class NewsCollection extends Item {
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());
for (Item item : getItems()) {
builder.append(item.getDetails());
}
return builder.toString();
}
@ -67,56 +51,20 @@ public class NewsCollection extends Item {
return topic;
}
/**
* Get the list of articles stored directly in this collection.
* @return articles stored in this collection
* Get the list of items stored directly in this collection.
* @return items stored in this collection
*/
public List<Article> getArticles() {
return articles;
public List<Item> getItems() {
return items;
}
/**
* Get the list of images stored directly in this collection.
* @return images stored in this collection
*/
public List<Image> getImages() {
return images;
}
/**
* Get the list of collections stored directly in this collection.
* @return collections stored in this collection
*/
public List<NewsCollection> getCollections() {
return collections;
}
/**
* Store an article directly in this collection.
* @param article
* Store an item directly in this collection.
* @param item
* @return this collection (enables method chaining)
*/
public NewsCollection addArticle(Article article) {
articles.add(article);
return this;
}
/**
* Store an image directly in this collection.
* @param image
* @return this collection (enables method chaining)
*/
public NewsCollection addImage(Image image) {
images.add(image);
return this;
}
/**
* Store another collection directly in this collection.
* @param collection
* @return this collection (enables method chaining)
*/
public NewsCollection addCollection(NewsCollection collection) {
collections.add(collection);
public NewsCollection addItem(Item item) {
items.add(item);
return this;
}
}