import java.util.ArrayList;
import java.util.List;
// 定义观察者接口NewsObserver
interface NewsObserver {
void update(String newsTitle, String newsContent);
}
// 定义被观察对象NewsPublisher,它实现了注册、移除观察者以及发布新闻的方法
class NewsPublisher {
private List<NewsObserver> observers;
public NewsPublisher() {
observers = new ArrayList<>();
}
public void registerObserver(NewsObserver observer) {
observers.add(observer);
}
public void removeObserver(NewsObserver observer) {
observers.remove(observer);
}
public void publishNews(String newsTitle, String newsContent) {
System.out.println("Publishing news: " + newsTitle);
for (NewsObserver observer : observers) {
observer.update(newsTitle, newsContent);
}
}
}
// 创建几个具体的观察者类,它们实现了NewsObserver接口,并定义了各自的处理逻辑
class EmailSubscriber implements NewsObserver {
@Override
public void update(String newsTitle, String newsContent) {
System.out.println("EmailSubscriber received news: " + newsTitle + "\nContent: " + newsContent);
// 这里可以添加发送邮件的代码
}
}
class MobileAppSubscriber implements NewsObserver {
@Override
public void update(String newsTitle, String newsContent) {
System.out.println("MobileAppSubscriber received news: " + newsTitle);
// 这里可以添加推送通知到移动应用的代码
}
}
class WebPageSubscriber implements NewsObserver {
@Override
public void update(String newsTitle, String newsContent) {
System.out.println("WebPageSubscriber received news: " + newsTitle + "\nContent: " + newsContent);
// 这里可以添加更新网页内容的代码
}
}
public class NewsSystem {
public static void main(String[] args) {
// 创建新闻发布者
NewsPublisher newsPublisher = new NewsPublisher();
// 创建订阅者并注册到新闻发布者
NewsObserver emailSubscriber = new EmailSubscriber();
NewsObserver mobileAppSubscriber = new MobileAppSubscriber();
NewsObserver webPageSubscriber = new WebPageSubscriber();
newsPublisher.registerObserver(emailSubscriber);
newsPublisher.registerObserver(mobileAppSubscriber);
newsPublisher.registerObserver(webPageSubscriber);
// 发布新闻
newsPublisher.publishNews("Breaking News", "An earthquake hit the city!");
// 移除某个订阅者(如果需要)
// newsPublisher.removeObserver(emailSubscriber);
}
}
Publishing news: Breaking News
EmailSubscriber received news: Breaking News
Content: An earthquake hit the city!
MobileAppSubscriber received news: Breaking News
WebPageSubscriber received news: Breaking News
Content: An earthquake hit the city!
import java.util.ArrayList;
import java.util.List;
// 首先,我们定义Observer接口和Subject接口
interface Observer {
void update(String message);
}
interface Subject {
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObservers(String message);
}
// 实现Player类作为Subject
class Player implements Subject {
private List<Observer> observers;
private int healthPoints;
public Player(int initialHealth) {
this.observers = new ArrayList<>();
this.healthPoints = initialHealth;
}
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
@Override
public void removeObserver(Observer o) {
observers.remove(o);
}
@Override
public void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
public void takeDamage(int damage) {
healthPoints -= damage;
if (healthPoints < 0) {
healthPoints = 0;
}
notifyObservers("Player took " + damage + " damage. Remaining health: " + healthPoints);
}
public int getHealthPoints() {
return healthPoints;
}
public boolean isAlive() {
return healthPoints > 0;
}
}
// 创建HUDDisplay和GameLog作为Observer
class HUDDisplay implements Observer {
@Override
public void update(String message) {
System.out.println("HUD: " + message);
}
}
class GameLog implements Observer {
@Override
public void update(String message) {
System.out.println("Log: " + message);
}
}
// 创建一个Enemy类,它负责攻击玩家
class Enemy {
private String name;
private int attackPower;
public Enemy(String name, int attackPower) {
this.name = name;
this.attackPower = attackPower;
}
public void attack(Player target) {
int damage = this.attackPower;
target.takeDamage(damage);
System.out.println(this.name + " attacked the player!");
}
}
public class GameTest {
public static void main(String[] args) {
// 创建玩家对象
Player player = new Player(100);
// 创建HUD显示和游戏日志对象,并注册为观察者
HUDDisplay hudDisplay = new HUDDisplay();
GameLog gameLog = new GameLog();
player.registerObserver(hudDisplay);
player.registerObserver(gameLog);
// 创建敌人对象
Enemy enemy = new Enemy("Goblin", 20);
// 敌人攻击玩家
enemy.attack(player);
// 检查玩家是否存活,并显示相关信息
if (player.isAlive()) {
System.out.println("Player is still alive with " + player.getHealthPoints() + " health points.");
} else {
System.out.println("Player has been defeated!");
}
}
}
HUD: Player took 20 damage. Remaining health: 80
Log: Player took 20 damage. Remaining health: 80
Goblin attacked the player!
Player is still alive with 80 health points.