wip
This commit is contained in:
parent
b1b8db8065
commit
945b313529
@ -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");
|
||||
}
|
||||
}
|
||||
|
@ -1,24 +1,43 @@
|
||||
package model;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
public class NewsCollection extends NewsItem {
|
||||
|
||||
|
||||
private List<NewsItem> newsItems = new LinkedList<NewsItem>();
|
||||
|
||||
|
||||
// We use a set to avoid duplicates.
|
||||
Set<Observer> 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<NewsItem> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user