观察者模式java例子_观察者模式最佳案例实现[JAVA][原创]

这个Java程序演示了观察者模式在股票市场的应用。StockMarket类代表股票市场,包含多个股票对象。当股票价格发生变化时,通过Observer接口通知订阅的投资者(Investor)更新最新的价格信息。投资者可以根据股票价格的特定条件选择投资。
摘要由CSDN通过智能技术生成

packagecom.v5ent.rapid4j.pattern;importjava.util.ArrayList;importjava.util.List;importjava.util.Observable;importjava.util.Observer;importjava.util.Random;/*** American Stock Exchange market(ASE) has a list of stocks.A stock object has two perspective information,symbol and price.

* Class StockMarket is a class that represents the stock market.

* Its constructor generates a collection of stocks using random numbers to build 3-letter stock symbols and random numbers for initial stock price.

* Implement a Java application when the stock price has been changed,all those investors who are interested in the stock market will be notified by receiving the most recent price.

* Create a driver class to test your implementation.

*@authorMignet

**/

public classStockTest {public static voidmain(String[] args) {

StockMarket market= new StockMarket(10);

market.show();

market.invest();

market.shuffle();

market.show();

}

}classStockMarket{private Listlist;private intcapacity;public StockMarket(intcapacity){this.capacity=capacity;

init(this.capacity);

}private List init(intn){

list= new ArrayList();for(int i=0;i

list.add(emitStock());

}returnlist;

}/*** 股票看板*/

public voidshow(){

System.out.println("-------------Welcome to American Stock Exchange-------------------");for(Stock s:list){s.show();}

System.out.println("------------------------------------------------------------------------");

}/*** 让投资人随机投资*/

public voidinvest(){

Investor inv1= new Investor("巴菲特");

Investor inv2= new Investor("索罗斯");//让投资人随机投资

for(Stock s:list){//比如巴菲特只投资价格是偶数的股票

if(Math.round(s.price)%2==0){

s.addObserver(inv1);

System.out.println(String.format("[%s]投资了[%s]:[%.2f]", inv1.name,s.symbol,s.price));

}//比如索罗斯只投资价格是3的倍数的股票

if(Math.round(s.price)%3==0){

s.addObserver(inv2);

System.out.println(String.format("[%s]投资了[%s]:[%.2f]", inv2.name,s.symbol,s.price));

}

}

}//生成随机股票

privateStock emitStock() {

StringBuilder val= newStringBuilder(); Stock s;

Random random= newRandom();for(int i = 0; i < 3; i++) {int temp = 65; //or 97

val .append((char)(random.nextInt(26) +temp));

}

s= new Stock(val.toString(),random.nextFloat()*100);returns;

}public voidshuffle(){for(Stock s:list){

s.shuffle();

}

}class Stock extendsObservable {public Stock(String symbol, floatprice) {this.symbol=symbol;this.price=price;

}public voidshow(){

System.out.println(String.format("[%s]:[%.2f]", this.symbol,this.price));

}privateString symbol;private floatprice;//价格随机波动

public voidshuffle(){this.price = this.price+new Random().nextInt(10)-5;this.setChanged();this.notifyObservers();

}

}class Investor implementsObserver{publicInvestor(String name){this.name =name;

}privateString name;

@Overridepublic voidupdate(Observable o, Object arg) {

Stock s=(Stock)o;

System.out.println(String.format("[%s]获取到[%s]的最新价格[%.2f]",this.name, s.symbol,s.price));

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值