23.3 完整解决方案
图22-4
[java] view plaincopy
-
import
java.util.*; -
-
//抽象观察类
-
interface
Observer { -
public String getName(); -
public void setName(String name); -
public void help(); //声明支援盟友方法 -
public void beAttacked(AllyControlCenter //声明遭受攻击方法acc); - }
-
-
//战队成员类:具体观察者类
-
class
Player implementsObserver { -
private String name; -
-
public Player(String name) { -
this.name = name; -
} -
-
public void setName(String name) { -
this.name = name; -
} -
-
public String getName() { -
return this.name; -
} -
-
//支援盟友方法的实现 -
public void help() { -
System.out.println("坚持住," + this.name+ "来救你!"); -
} -
-
//遭受攻击方法的实现,当遭受攻击时将调用战队控制中心类的通知方法notifyObserver()来通知盟友 -
public void beAttacked(AllyControlCenter acc) { -
System.out.println(this.name + "被攻击!"); -
acc.notifyObserver(name); -
} - }
-
-
//战队控制中心类:目标类
-
abstract
class AllyControlCenter { -
protected String //战队名称allyName; -
protected ArrayList newplayers = ArrayList(); //定义一个集合用于存储战队成员 -
-
public void setAllyName(String allyName) { -
this.allyName = allyName; -
} -
-
public String getAllyName() { -
return this.allyName; -
} -
-
//注册方法 -
public void join(Observer obs) { -
System.out.println(obs.getName() + "加入" + this.allyName+ "战队!"); -
players.add(obs); -
} -
-
//注销方法 -
public void quit(Observer obs) { -
System.out.println(obs.getName() + "退出" + this.allyName+ "战队!"); -
players.remove(obs); -
} -
-
//声明抽象通知方法 -
public abstract void notifyObserver(String name); - }
-
-
//具体战队控制中心类:具体目标类
-
class
ConcreteAllyControlCente extendsr AllyControlCenter { -
public ConcreteAllyControlCente r(String allyName) { -
System.out.println(allyName + "战队组建成功!"); -
System.out.println("----------------------------"); -
this.allyName = allyName; -
} -
-
//实现通知方法 -
public void notifyObserver(String name) { -
System.out.println(this.allyName + "战队紧急通知,盟友"+ "遭受敌人攻击!");name + -
//遍历观察者集合,调用每一个盟友(自己除外)的支援方法 -
for(Object obs : players) { -
if (!((Observer)obs).getName().equalsIgnoreCase(name)) { -
((Observer)obs).help(); -
} -
} -
} - }
[java] view plaincopy
-
class
Client { -
public static void main(String args[]) { -
//定义观察目标对象 -
AllyControlCenter
acc; -
acc = new ConcreteAllyControlCente "金庸群侠");r( -
-
//定义四个观察者对象 -
Observer player1,player2,player3,player4; -
-
player1 = new Player( "杨过"); -
acc.join(player1); -
-
player2 = new Player( "令狐冲"); -
acc.join(player2); -
-
player3 = new Player( "张无忌"); -
acc.join(player3); -
-
player4 = new Player( "段誉"); -
acc.join(player4); -
-
//某成员遭受攻击 -
Player1.beAttacked(acc); -
} - }
金庸群侠战队组建成功! ---------------------------- 杨过加入金庸群侠战队! 令狐冲加入金庸群侠战队! 张无忌加入金庸群侠战队! 段誉加入金庸群侠战队! 杨过被攻击! 金庸群侠战队紧急通知,盟友杨过遭受敌人攻击! 坚持住,令狐冲来救你! 坚持住,张无忌来救你! 坚持住,段誉来救你! |
Player.beAttacked()
【作者:刘伟