This commit is contained in:
myluckagain 2018-02-02 03:17:53 +05:00 committed by Predrag Maric
parent 310e31e2bd
commit 55660e7702
8 changed files with 180 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package com.baeldung.designpatterns.observer;
public interface Channel {
public void update(Object o);
}

View File

@ -0,0 +1,24 @@
package com.baeldung.designpatterns.observer;
import java.util.ArrayList;
import java.util.List;
public class NewsAgency {
private String news;
private List<Channel> channels = new ArrayList<>();
public void addObserver(Channel channel) {
this.channels.add(channel);
}
public void removeObserver(Channel channel) {
this.channels.remove(channel);
}
public void setNews(String news) {
this.news = news;
for (Channel channel : this.channels) {
channel.update(this.news);
}
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.designpatterns.observer;
public class NewsChannel implements Channel {
private String news;
@Override
public void update(Object news) {
this.setNews((String) news);
}
public String getNews() {
return news;
}
public void setNews(String news) {
this.news = news;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.designpatterns.observer;
import java.util.Observable;
public class ONewsAgency extends Observable {
private String news;
public void setNews(String news) {
this.news = news;
setChanged();
notifyObservers(news);
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.designpatterns.observer;
import java.util.Observable;
import java.util.Observer;
public class ONewsChannel implements Observer {
private String news;
@Override
public void update(Observable o, Object news) {
this.setNews((String) news);
}
public String getNews() {
return news;
}
public void setNews(String news) {
this.news = news;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.designpatterns.observer;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class PCLNewsAgency {
private String news;
private PropertyChangeSupport support;
public PCLNewsAgency() {
support = new PropertyChangeSupport(this);
}
public void addPropertyChangeListener(PropertyChangeListener pcl) {
support.addPropertyChangeListener(pcl);
}
public void removePropertyChangeListener(PropertyChangeListener pcl) {
support.removePropertyChangeListener(pcl);
}
public void setNews(String value) {
support.firePropertyChange("news", this.news, value);
this.news = value;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.designpatterns.observer;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class PCLNewsChannel implements PropertyChangeListener {
private String news;
public void propertyChange(PropertyChangeEvent evt) {
this.setNews((String) evt.getNewValue());
}
public String getNews() {
return news;
}
public void setNews(String news) {
this.news = news;
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.designpatterns.observer;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.baeldung.designpatterns.observer.NewsAgency;
import com.baeldung.designpatterns.observer.NewsChannel;
public class ObserverIntegrationTest {
@Test
public void whenChangingNewsAgencyState_thenNewsChannelNotified() {
NewsAgency observable = new NewsAgency();
NewsChannel observer = new NewsChannel();
observable.addObserver(observer);
observable.setNews("news");
assertEquals(observer.getNews(), "news");
}
@Test
public void whenChangingONewsAgencyState_thenONewsChannelNotified() {
ONewsAgency observable = new ONewsAgency();
ONewsChannel observer = new ONewsChannel();
observable.addObserver(observer);
observable.setNews("news");
assertEquals(observer.getNews(), "news");
}
@Test
public void whenChangingPCLNewsAgencyState_thenONewsChannelNotified() {
PCLNewsAgency observable = new PCLNewsAgency();
PCLNewsChannel observer = new PCLNewsChannel();
observable.addPropertyChangeListener(observer);
observable.setNews("news");
assertEquals(observer.getNews(), "news");
}
}