File tree Expand file tree Collapse file tree 6 files changed +82
-0
lines changed
src/com/anxpp/designpattern/mediator Expand file tree Collapse file tree 6 files changed +82
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .mediator ;
2+ //同事(接口)
3+ public interface IPersistent {
4+ void getData (Object data );
5+ void getData (Object data ,Midiator midiator );
6+ void saveData ();
7+ }
Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .mediator ;
2+ //具体中介者
3+ public class Midiator {
4+ PersistentDB persistentDB ;
5+ PersistentFile persistentFile ;
6+ public Midiator setPersistentDB (PersistentDB persistentDB ) {
7+ this .persistentDB = persistentDB ;
8+ return this ;
9+ }
10+ public Midiator setPersistentFile (PersistentFile persistentFile ) {
11+ this .persistentFile = persistentFile ;
12+ return this ;
13+ }
14+ public void notifyOther (IPersistent persistent ,Object data ){
15+ if (persistent instanceof PersistentDB )
16+ persistentFile .getData (data );
17+ if (persistent instanceof PersistentFile )
18+ persistentDB .getData (data );
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .mediator ;
2+ //具体同事
3+ public class PersistentDB implements IPersistent {
4+ private Object data ;
5+ @ Override
6+ public void getData (Object data , Midiator midiator ) {
7+ getData (data );
8+ midiator .notifyOther (this , data );
9+ }
10+ @ Override
11+ public void saveData () {
12+ System .out .println (data + " 已保存到数据库" );
13+ }
14+ @ Override
15+ public void getData (Object data ) {
16+ this .data = data ;
17+ saveData ();
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .mediator ;
2+ //具体同事
3+ public class PersistentFile implements IPersistent {
4+ private Object data ;
5+ @ Override
6+ public void getData (Object data , Midiator midiator ) {
7+ getData (data );
8+ midiator .notifyOther (this , data );
9+ }
10+ @ Override
11+ public void saveData () {
12+ System .out .println (data + " 已保存到文件" );
13+ }
14+ @ Override
15+ public void getData (Object data ) {
16+ this .data = data ;
17+ saveData ();
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .mediator ;
2+ public class TestUse {
3+ public static void main (String args []){
4+ Object data = "数据" ;
5+ PersistentDB persistentDB = new PersistentDB ();
6+ PersistentFile persistentFile = new PersistentFile ();
7+ Midiator midiator = new Midiator ();
8+ midiator .setPersistentDB (persistentDB ).setPersistentFile (persistentFile );
9+ persistentDB .getData (data , midiator );
10+ persistentFile .getData (data , midiator );
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * @author Administrator
3+ * 中介者模式
4+ */
5+ package com .anxpp .designpattern .mediator ;
You can’t perform that action at this time.
0 commit comments