题目
类图

Java
Investor接口
package com.gazikel;
public interface Investor {
void update(float range);
}
Subject接口
package com.gazikel;
public interface Subject {
void addInvestor(Investor investor);
void removeInvestor(Investor investor);
void notifyInvestors();
}
Investor1实现Investor接口
package com.gazikel;
public class Investor1 implements Investor {
private float range;
@Override
public void update(float range) {
this.range = range;
display();
}
public void display() {
if (this.range > 0) {
System.out.println("买股票");
} else if(this.range < 0) {
System.out.println("大哭一场");
}
}
}
Stock实现Subject接口
package com.gazikel;
public class Investor1 implements Investor {
private float range;
@Override
public void update(float range) {
this.range = range;
display();
}
public void display() {
if (this.range > 0) {
System.out.println("买股票");
} else if(this.range < 0) {
System.out.println("大哭一场");
}
}
}
Client
package com.gazikel;
public class Client {
public static void main(String[] args) {
Investor investor1 = new Investor1();
Stock stock = new Stock();
stock.addInvestor(investor1);
stock.setData(0.03f);
System.out.println("------------------------");
stock.setData(0.06f);
System.out.println("------------------------");
stock.setData(-0.07f);
System.out.println("------------------------");
}
}

C++
#include<iostream>
#include<list>
using namespace std;
class Investor {
public:
virtual void update(double range) = 0;
};
class Investor1 :public Investor {
private:
double range;
public:
void update(double range) {
this->range = range;
display();
}
void display() {
if (this->range > 0) {
cout << "买股票" << endl;
}
else if (this->range < 0) {
cout << "大哭一场" << endl;
}
}
};
class Subject {
public:
virtual void AddInvestor(Investor* investor) = 0;
virtual void NotifyInvestors() = 0;
};
class Stock :public Subject {
private:
double range;
list<Investor*> investors;
public:
void SetData(double range) {
this->range = range;
if (range > 0) {
cout << "股票价格上涨了" << endl;
}
else if (this->range < 0) {
cout << "股票价格下降了" << endl;
}
else {
cout << "股票价格保持平稳" << endl;
}
if (this->range >= 0.05 || this->range <= -0.05) {
NotifyInvestors();
}
}
void AddInvestor(Investor *investor) {
investors.push_back(investor);
}
void NotifyInvestors() {
list<Investor*>::iterator it;
for (it = investors.begin(); it != investors.end(); it++) {
(*it)->update(this->range);
}
}
};
int main() {
Investor* investor1 = new Investor1();
Stock* stock = new Stock();
stock->AddInvestor(investor1);
stock->SetData(0.03);
cout << "-------------------------------" << endl;
stock->SetData(0.06f);
cout << "-------------------------------" << endl;
stock->SetData(-0.07f);
cout << "-------------------------------" << endl;
}

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
2020-11-29 JSON