Skip to content

Commit 298fc1d

Browse files
committed
observer
1 parent 125e2d3 commit 298fc1d

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

src/main/java/helloworld/Main.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import helloworld.behavioral.mediator.HelloWorldSlogan;
1212
import helloworld.behavioral.mediator.HelloWorldTarget;
1313
import helloworld.behavioral.memento.HelloWorldMementoOriginator;
14+
import helloworld.behavioral.observer.HelloWorldObserver;
15+
import helloworld.behavioral.observer.Subject;
1416
import helloworld.creational.abstract_factory.AbstractFactory;
1517
import helloworld.creational.abstract_factory.SplitHelloWorldFactory;
1618
import helloworld.creational.builder.HelloWorldBuilder;
@@ -114,5 +116,8 @@ public static void main(String[] args) throws InstantiationException, IllegalAcc
114116
helloWorldMementoOriginator.restoreFromMemento(memento);
115117
System.out.println(helloWorldMementoOriginator.helloWorld());
116118

119+
Subject subject = new Subject().attach(new HelloWorldObserver());
120+
subject.notifyObservers();
121+
117122
}
118123
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package helloworld.behavioral.observer;
2+
3+
import java.io.PrintStream;
4+
5+
/**
6+
* @author yihua.huang@dianping.com
7+
*/
8+
public class HelloWorldObserver implements Observer{
9+
10+
private PrintStream printer = System.out;
11+
12+
public void setPrinter(PrintStream printer) {
13+
this.printer = printer;
14+
}
15+
16+
@Override
17+
public void update() {
18+
printer.println("Hello Observer!");
19+
}
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package helloworld.behavioral.observer;
2+
3+
/**
4+
* @author yihua.huang@dianping.com
5+
*/
6+
public interface Observer {
7+
8+
public void update();
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package helloworld.behavioral.observer;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
7+
/**
8+
* @author yihua.huang@dianping.com
9+
*/
10+
public class Subject {
11+
12+
private List<Observer> observers;
13+
14+
public Subject() {
15+
this.observers = new LinkedList<Observer>();
16+
}
17+
18+
public Subject attach(Observer observer){
19+
observers.add(observer);
20+
return this;
21+
}
22+
23+
public void notifyObservers(){
24+
for (Observer observer : observers) {
25+
observer.update();
26+
}
27+
}
28+
29+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package helloworld.behavioral.observer;
2+
3+
import org.junit.Test;
4+
5+
import java.io.PrintStream;
6+
7+
import static org.mockito.Mockito.*;
8+
9+
/**
10+
* @author yihua.huang@dianping.com
11+
*/
12+
public class HelloWorldObserverTest {
13+
14+
@Test
15+
public void testHelloWorldObserver(){
16+
HelloWorldObserver observer = new HelloWorldObserver();
17+
PrintStream mockPrinter = mock(PrintStream.class);
18+
observer.setPrinter(mockPrinter);
19+
Subject subject = new Subject().attach(observer);
20+
subject.notifyObservers();
21+
verify(mockPrinter,times(1)).println("Hello Observer!");
22+
}
23+
}

0 commit comments

Comments
 (0)