菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

软件设计-Tutorial21

类图:

复制代码
```mermaid
classDiagram
    class Stock {
        -String stockName
        -double price
        -List~Investor~ investors
        +Stock(String, double)
        +void addInvestor(Investor)
        +void removeInvestor(Investor)
        +void setPrice(double)
        -void notifyPriceIncrease(double)
        -void notifyPriceDecrease(double)
    }

    class Investor {
        <<interface>>
        +void onPriceIncrease(double)
        +void onPriceDecrease(double)
    }

    class InvestorImpl {
        -String name
        +InvestorImpl(String)
        +void onPriceIncrease(double)
        +void onPriceDecrease(double)
    }

    Stock o-- Investor : 观察者
    InvestorImpl ..|> Investor
    Stock --> InvestorImpl : 通知

```
复制代码

源码:

复制代码
package Tutorial21;

import java.util.ArrayList;
import java.util.List;

// 观察者接口
interface Investor {
    void onPriceIncrease(double price);
    void onPriceDecrease(double price);
}

// 具体的观察者类
class InvestorImpl implements Investor {
    private String name;

    public InvestorImpl(String name) {
        this.name = name;
    }

    @Override
    public void onPriceIncrease(double price) {
        System.out.println(name + "收到消息:股票价格上涨至 " + price + ",买入股票!");
    }

    @Override
    public void onPriceDecrease(double price) {
        System.out.println(name + "收到消息:股票价格下跌至 " + price + ",大哭一场!");
    }
}

// 被观察者类
class Stock {
    private String stockName;
    private double price;
    private List<Investor> investors = new ArrayList<>();

    public Stock(String stockName, double price) {
        this.stockName = stockName;
        this.price = price;
    }

    // 添加观察者
    public void addInvestor(Investor investor) {
        investors.add(investor);
    }

    // 移除观察者
    public void removeInvestor(Investor investor) {
        investors.remove(investor);
    }

    // 设置新价格并判断是否通知观察者
    public void setPrice(double newPrice) {
        double changePercentage = ((newPrice - price) / price) * 100;

        if (Math.abs(changePercentage) >= 5) {
            System.out.println("\n股票 " + stockName + " 的价格变动了 " + String.format("%.2f", changePercentage) + "%!");
            if (changePercentage > 0) {
                notifyPriceIncrease(newPrice);
            } else {
                notifyPriceDecrease(newPrice);
            }
        }

        // 更新价格
        this.price = newPrice;
    }

    // 通知价格上涨的观察者
    private void notifyPriceIncrease(double price) {
        for (Investor investor : investors) {
            investor.onPriceIncrease(price);
        }
    }

    // 通知价格下跌的观察者
    private void notifyPriceDecrease(double price) {
        for (Investor investor : investors) {
            investor.onPriceDecrease(price);
        }
    }
}

// 测试类
public class ObserverExample {
    public static void main(String[] args) {
        // 创建股票
        Stock stock = new Stock("科技股份", 100.0);

        // 添加观察者
        Investor investor1 = new InvestorImpl("小张");
        Investor investor2 = new InvestorImpl("小李");
        stock.addInvestor(investor1);
        stock.addInvestor(investor2);

        // 价格变化测试
        stock.setPrice(105.0);  // 上涨5%
        stock.setPrice(95.0);   // 下跌9.5%
        stock.setPrice(100.0);  // 不通知变化
    }
}
复制代码

 

posted on   hhmzd233  阅读(3)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示