From 3f30173a5b498ecc5e7f0a176eda6ec532c1dc0b Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sun, 24 Nov 2024 19:09:45 +0100 Subject: [PATCH] 1b: composite pattern --- ueb05_news_example/src/client/Client.java | 14 ++-- .../src/model/NewsCollection.java | 78 ++++--------------- 2 files changed, 20 insertions(+), 72 deletions(-) diff --git a/ueb05_news_example/src/client/Client.java b/ueb05_news_example/src/client/Client.java index baa626c6..383d49a0 100644 --- a/ueb05_news_example/src/client/Client.java +++ b/ueb05_news_example/src/client/Client.java @@ -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()); diff --git a/ueb05_news_example/src/model/NewsCollection.java b/ueb05_news_example/src/model/NewsCollection.java index fab07637..7d989220 100644 --- a/ueb05_news_example/src/model/NewsCollection.java +++ b/ueb05_news_example/src/model/NewsCollection.java @@ -6,9 +6,7 @@ import java.util.List; public class NewsCollection extends Item { private String topic; - private List
articles = new LinkedList
(); - private List images = new LinkedList(); - private List collections = new LinkedList(); + private List 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
getArticles() { - return articles; + public List getItems() { + return items; } /** - * Get the list of images stored directly in this collection. - * @return images stored in this collection - */ - public List getImages() { - return images; - } - - /** - * Get the list of collections stored directly in this collection. - * @return collections stored in this collection - */ - public List 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; } }