ueb07 b)
This commit is contained in:
parent
fcc25f0c21
commit
221d9cb5d8
@ -4,10 +4,19 @@ 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 class Client implements Observer {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
Client client = new Client();
|
||||||
|
client.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
public 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");
|
||||||
|
|
||||||
@ -23,9 +32,18 @@ public class Client {
|
|||||||
System.out.print(masterCollection.getOverviewInformation());
|
System.out.print(masterCollection.getOverviewInformation());
|
||||||
System.out.println("\n===Contents===\n");
|
System.out.println("\n===Contents===\n");
|
||||||
System.out.print(masterCollection.getDetailedInformation());
|
System.out.print(masterCollection.getDetailedInformation());
|
||||||
|
|
||||||
|
masterCollection.addObserver(this);
|
||||||
|
|
||||||
System.out.println("\n===After change===\n");
|
System.out.println("\n===After change===\n");
|
||||||
|
|
||||||
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 ###");
|
||||||
|
System.out.println(arg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,29 @@
|
|||||||
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 Set<Observer> observers = new HashSet<>();
|
||||||
private List<NewsItem> newsItems = new LinkedList<NewsItem>();
|
private List<NewsItem> newsItems = new LinkedList<NewsItem>();
|
||||||
|
|
||||||
public NewsCollection(String topic) {
|
public NewsCollection(String topic) {
|
||||||
this.title = topic;
|
this.title = topic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void addObserver(Observer o) {
|
||||||
|
super.addObserver(o);
|
||||||
|
observers.add(o);
|
||||||
|
newsItems.forEach(i -> i.addObserver(o));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void deleteObserver(Observer o) {
|
||||||
|
super.deleteObserver(o);
|
||||||
|
observers.remove(o);
|
||||||
|
newsItems.forEach(i -> i.deleteObserver(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
|
||||||
@ -25,7 +38,11 @@ public class NewsCollection extends NewsItem {
|
|||||||
* @return this collection (enables method chaining)
|
* @return this collection (enables method chaining)
|
||||||
*/
|
*/
|
||||||
public NewsCollection addItem(NewsItem item) {
|
public NewsCollection addItem(NewsItem item) {
|
||||||
|
String updateNotification = getTitle() + "\n+ " + item.getTitle() + "\n";
|
||||||
newsItems.add(item);
|
newsItems.add(item);
|
||||||
|
setChanged();
|
||||||
|
notifyObservers(updateNotification);
|
||||||
|
observers.forEach(item::addObserver);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,5 +71,5 @@ public class NewsCollection extends NewsItem {
|
|||||||
builder.append("\n");
|
builder.append("\n");
|
||||||
}
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +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;
|
||||||
|
|
||||||
@ -17,7 +19,10 @@ public abstract class NewsItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setTitle(String newTitle) {
|
public void setTitle(String newTitle) {
|
||||||
|
String updateNotification = "- " + title + "\n+ " + newTitle + "\n";
|
||||||
title = newTitle;
|
title = newTitle;
|
||||||
|
setChanged();
|
||||||
|
notifyObservers(updateNotification);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user