Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5e389ff

Browse files
观察者模式
1 parent e320bd6 commit 5e389ff

File tree

5 files changed

+16
-1
lines changed

5 files changed

+16
-1
lines changed

‎src/cn/ucaner/pattern/action/observer/ObserverMain.java‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static void main(String[] args) {
3434
getObservers(nvShen);
3535
//女神要出去逛街了!!!
3636
nvShen.goShopping();
37+
3738

3839
}
3940

@@ -42,7 +43,11 @@ public static void getObservers(Subject nvShen){
4243
for (int i = 0; i < 100 ; i++) {
4344
DiaoSi diaosi=new DiaoSi("屌丝"+i);
4445
nvShen.Attach(diaosi);//添加
45-
nvShen.Detach(diaosi);//删除
46+
if (i%2!=0) {
47+
//添加一个观察者
48+
nvShen.Detach(diaosi);//删除
49+
}
50+
4651
}
4752
}
4853

‎src/cn/ucaner/pattern/action/observer/observerAbs/Observer.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ public interface Observer {
2525

2626
public void Update();
2727

28+
public void Say();
29+
2830
}

‎src/cn/ucaner/pattern/action/observer/observerAbs/Subject.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ public interface Subject {
3131

3232
//通知所有的观察者
3333
public void Notify();
34+
3435
}

‎src/cn/ucaner/pattern/action/observer/observerIml/DiaoSi.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,10 @@ public void Update() {
4848
System.out.println(name+"已经知道女神出门,准备跟(wei)踪(sui)");
4949
}
5050

51+
@Override
52+
public void Say() {
53+
System.out.println(name+"在下想约你可以吗?");
54+
}
55+
5156

5257
}

‎src/cn/ucaner/pattern/action/observer/observerIml/NvShen.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ public void Notify() {
6565
for (Observer observer:observersList) {
6666
//通知所有的跟随人员
6767
observer.Update();
68+
observer.Say();
6869
}
6970
System.out.print("尾随大军已经出动!!!");
7071
}
72+
7173
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /