From 945b313529fe92a45a5145ac4acfef82f6a89a12 Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Sat, 7 Dec 2024 13:11:42 +0100 Subject: [PATCH] wip --- ueb07_news_example_b/src/client/Client.java | 19 ++++++++-- .../src/model/NewsCollection.java | 38 ++++++++++++++----- ueb07_news_example_b/src/model/NewsItem.java | 8 +++- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/ueb07_news_example_b/src/client/Client.java b/ueb07_news_example_b/src/client/Client.java index 94f2afd4..678aee2e 100644 --- a/ueb07_news_example_b/src/client/Client.java +++ b/ueb07_news_example_b/src/client/Client.java @@ -4,10 +4,18 @@ import model.Article; import model.Image; import model.NewsCollection; -public class Client { - - public static void main(String[] args) { +import java.util.Observable; +import java.util.Observer; + +public class Client implements Observer { + public static void main(String[] args) { + Client client = new Client(); + client.run(); + } + + private void run() { NewsCollection masterCollection = new NewsCollection("Daily news"); + masterCollection.addObserver(this); Article changedArticle = new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups"); @@ -28,4 +36,9 @@ public class Client { changedArticle.setTitle("Harder, Better, Faster, Stronger? Doping controls loosened"); masterCollection.addItem(new Image("A tasty cat", 640, 480, "alf")); } + + @Override + public void update(Observable o, Object arg) { + System.out.println("### UPDATE ###\n" + arg + "\n"); + } } diff --git a/ueb07_news_example_b/src/model/NewsCollection.java b/ueb07_news_example_b/src/model/NewsCollection.java index eab02042..341fb81d 100644 --- a/ueb07_news_example_b/src/model/NewsCollection.java +++ b/ueb07_news_example_b/src/model/NewsCollection.java @@ -1,24 +1,43 @@ package model; -import java.util.LinkedList; -import java.util.List; +import java.util.*; public class NewsCollection extends NewsItem { - + private List newsItems = new LinkedList(); - + + // We use a set to avoid duplicates. + Set observers = new HashSet<>(); + public NewsCollection(String topic) { this.title = topic; } - - /** + + @Override + public void addObserver(Observer o) { + super.addObserver(o); + newsItems.forEach(item -> item.addObserver(o)); + + // Remember for later. + observers.add(o); + } + + @Override + public void deleteObserver(Observer o){ + super.deleteObserver(o); + newsItems.forEach(item -> item.deleteObserver(o)); + + observers.remove(o); + } + + /** * Get the list of items stored in this collection. * @return items stored in this collection */ public List getNewsItems() { return newsItems; } - + /** * Store another item to this collection. * @param item @@ -26,9 +45,10 @@ public class NewsCollection extends NewsItem { */ public NewsCollection addItem(NewsItem item) { newsItems.add(item); + observers.forEach(item::addObserver); return this; } - + /** * Print a list showing information about all directly or transitively * referenced items in the given collection. @@ -54,5 +74,5 @@ public class NewsCollection extends NewsItem { builder.append("\n"); } return builder.toString(); - } + } } diff --git a/ueb07_news_example_b/src/model/NewsItem.java b/ueb07_news_example_b/src/model/NewsItem.java index e7b7a86a..3aa3e87e 100644 --- a/ueb07_news_example_b/src/model/NewsItem.java +++ b/ueb07_news_example_b/src/model/NewsItem.java @@ -1,7 +1,8 @@ package model; -public abstract class NewsItem { +import java.util.Observable; +public abstract class NewsItem extends Observable { protected String title; public NewsItem() { @@ -17,6 +18,11 @@ public abstract class NewsItem { } public void setTitle(String newTitle) { + if(title.equals(newTitle)) return; + + setChanged(); + notifyObservers("āž– " + title + "\nāž• " + newTitle); + title = newTitle; }