This commit is contained in:
Daniel Langbein 2024-12-07 13:11:42 +01:00
parent b1b8db8065
commit 945b313529
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
3 changed files with 52 additions and 13 deletions

View File

@ -4,10 +4,18 @@ import model.Article;
import model.Image; import model.Image;
import model.NewsCollection; import model.NewsCollection;
public class Client { import java.util.Observable;
import java.util.Observer;
public static void main(String[] args) {
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"); NewsCollection masterCollection = new NewsCollection("Daily news");
masterCollection.addObserver(this);
Article changedArticle = new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups"); 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"); changedArticle.setTitle("Harder, Better, Faster, Stronger? Doping controls loosened");
masterCollection.addItem(new Image("A tasty cat", 640, 480, "alf")); 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");
}
} }

View File

@ -1,24 +1,43 @@
package model; package model;
import java.util.LinkedList; import java.util.*;
import java.util.List;
public class NewsCollection extends NewsItem { public class NewsCollection extends NewsItem {
private List<NewsItem> newsItems = new LinkedList<NewsItem>(); private List<NewsItem> newsItems = new LinkedList<NewsItem>();
// We use a set to avoid duplicates.
Set<Observer> observers = new HashSet<>();
public NewsCollection(String topic) { public NewsCollection(String topic) {
this.title = 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. * Get the list of items stored in this collection.
* @return items stored in this collection * @return items stored in this collection
*/ */
public List<NewsItem> getNewsItems() { public List<NewsItem> getNewsItems() {
return newsItems; return newsItems;
} }
/** /**
* Store another item to this collection. * Store another item to this collection.
* @param item * @param item
@ -26,9 +45,10 @@ public class NewsCollection extends NewsItem {
*/ */
public NewsCollection addItem(NewsItem item) { public NewsCollection addItem(NewsItem item) {
newsItems.add(item); newsItems.add(item);
observers.forEach(item::addObserver);
return this; return this;
} }
/** /**
* Print a list showing information about all directly or transitively * Print a list showing information about all directly or transitively
* referenced items in the given collection. * referenced items in the given collection.
@ -54,5 +74,5 @@ public class NewsCollection extends NewsItem {
builder.append("\n"); builder.append("\n");
} }
return builder.toString(); return builder.toString();
} }
} }

View File

@ -1,7 +1,8 @@
package model; package model;
public abstract class NewsItem { import java.util.Observable;
public abstract class NewsItem extends Observable {
protected String title; protected String title;
public NewsItem() { public NewsItem() {
@ -17,6 +18,11 @@ public abstract class NewsItem {
} }
public void setTitle(String newTitle) { public void setTitle(String newTitle) {
if(title.equals(newTitle)) return;
setChanged();
notifyObservers(" " + title + "\n " + newTitle);
title = newTitle; title = newTitle;
} }