/**
* American Stock Exchange market(ASE) has a list of stocks.A stock object has two perspective information,symbol and price.<br>
* Class <b>StockMarket</b> is a class that represents the stock market.<br>
* Its constructor generates a collection of stocks using random numbers to build 3-letter stock symbols and random numbers for initial stock price.<br>
* 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.<br>
* Create a driver class to test your implementation.
*
*/

package com.v5ent.rapid4j.pattern;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Random; /**
* American Stock Exchange market(ASE) has a list of stocks.A stock object has two perspective information,symbol and price.<br>
* Class <b>StockMarket</b> is a class that represents the stock market.<br>
* Its constructor generates a collection of stocks using random numbers to build 3-letter stock symbols and random numbers for initial stock price.<br>
* 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.<br>
* Create a driver class to test your implementation.
* @author Mignet
*
*/
public class StockTest {
public static void main(String[] args) {
StockMarket market = new StockMarket(10);
market.show();
market.invest();
market.shuffle();
market.show();
}
} class StockMarket{
private List<Stock> list;
private int capacity;
public StockMarket(int capacity){
this.capacity=capacity;
init(this.capacity);
}
private List<Stock> init(int n){
list= new ArrayList<Stock>();
for(int i=0;i<n;i++){
list.add(emitStock());
}
return list;
}
/**
* 股票看板
*/
public void show(){
System.out.println("-------------Welcome to American Stock Exchange-------------------");
for(Stock s:list){s.show();}
System.out.println("------------------------------------------------------------------------");
}
/**
* 让投资人随机投资
*/
public void invest(){
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));
}
}
}
//生成随机股票
private Stock emitStock() {
StringBuilder val = new StringBuilder(); Stock s;
Random random = new Random();
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); return s;
} public void shuffle(){
for(Stock s:list){
s.shuffle();
}
} class Stock extends Observable {
public Stock(String symbol, float price) {
this.symbol=symbol;
this.price=price;
}
public void show(){
System.out.println(String.format("[%s]:[%.2f]", this.symbol,this.price));
}
private String symbol;
private float price;
//价格随机波动
public void shuffle(){
this.price = this.price+new Random().nextInt(10)-5;
this.setChanged();
this.notifyObservers();
}
}
class Investor implements Observer{
public Investor(String name){
this.name = name;
}
private String name;
@Override
public void update(Observable o, Object arg) {
Stock s = (Stock)o;
System.out.println(String.format("[%s]获取到[%s]的最新价格[%.2f]",this.name, s.symbol,s.price));
} }
}

观察者模式最佳案例实现[JAVA][原创]的更多相关文章

  1. (转载)Android之三种网络请求解析数据(最佳案例)

    [置顶] Android之三种网络请求解析数据(最佳案例) 2016-07-25 18:02 4725人阅读 评论(0) 收藏 举报  分类: Gson.Gson解析(1)  版权声明:本文为博主原创 ...

  2. Openresty最佳案例 | 汇总

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616856 本文出自方志朋的博客 目录 Openresty最佳案例 | 第1篇:Ngin ...

  3. atitit.泛型编程总结最佳实践 vO99 java c++ c#.net php

    atitit.泛型编程总结最佳实践 vO99 java c++ c#.net php \ 1. 泛型历史 1 由来 1 2. 泛型的机制编辑 1 机制 1 编译机制 2 3. 泛型方法定义1::前定义 ...

  4. JVM:从实际案例聊聊Java应用的GC优化

    原文转载自美团从实际案例聊聊Java应用的GC优化,感谢原作者的贡献 当Java程序性能达不到既定目标,且其他优化手段都已经穷尽时,通常需要调整垃圾回收器来进一步提高性能,称为GC优化.但GC算法复杂 ...

  5. Atitit. 拉开拉链zip文件 最佳实践实施 java c# .net php

    Atitit. 拉开拉链zip文件 的实现最佳实践 java c# .net php 1. Jdk zip 跟apache ant zip 1 2. Apache Ant包进行ZIP文件压缩,upzi ...

  6. Openresrt最佳案例

    第1篇:Nginx介绍 Nginx是一个高性能的Web 服务器,同时是一个高效的反向代理服务器,它还是一个IMAP/POP3/SMTP 代理服务器. 由于Nginx采用的是事件驱动的架构,能够处理并发 ...

  7. 最佳案例 | QQ 相册云原生容器化之路

    关于我们 更多关于云原生的案例和知识,可关注同名[腾讯云原生]公众号~ 福利: ①公众号后台回复[手册],可获得<腾讯云原生路线图手册>&<腾讯云原生最佳实践>~ ②公 ...

  8. 最佳案例 | 游戏知几 AI 助手的云原生容器化之路

    作者 张路,运营开发专家工程师,现负责游戏知几 AI 助手后台架构设计和优化工作. 游戏知几 随着业务不断的拓展,游戏知几AI智能问答机器人业务已经覆盖了自研游戏.二方.海外的多款游戏.游戏知几研发团 ...

  9. Android之三种网络请求解析数据(最佳案例)

    AsyncTask解析数据 AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法. ...

随机推荐

  1. NYOJ-517 最小公倍数 TLE 分类: NYOJ 2013-12-29 14:49 253人阅读 评论(0) 收藏

    #include <stdio.h> int main(){ int num[101]={0}; int result[21]={0}; int sum[101][21]={0}; int ...

  2. Science:给青年科研工作者的忠告

  3. Sencha Touch 2.4 callParent() 用法

    callParent() 用法 方法介绍 用来调用父类的同名方法,并传参,这在从一个框架类派生且要重写诸如onRender这样的方法时会经常看到. 传参方式 1.arguments Ext.defin ...

  4. Unity Texture 2D Compress

    测试了一下 unity 图片 对 apk 的影响. 上两种测试环境    1024 * 1024     带 alpha的话 默认压缩就是RBA 16bit就是2M     不带的话就是 etc 的话 ...

  5. Find the smallest number whose digits multiply to a given number n

    Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ...

  6. A Product Array Puzzle

    Given an array arr[] of n integers, construct a Product Array prod[] (of same size) such that prod[i ...

  7. c#知识库同步的总结

    C#知识库下载地址: http://www.51aspx.com/Code/FileCollector 新版下载地址:http://pan.baidu.com/s/1P3Hk 对于这一款平时用来收集知 ...

  8. Git 10 周年之际,创始人 Linus Torvalds 访谈

    点这里 十年前的这一周,linux 内核社区面临一个根本性的挑战:他们不再能够使用他们的修复控制系统:BitKeeper,同时其他的软件配置管理遇到了对分布式系统的新需求.Linus Torvalds ...

  9. Java常用类库

    System System:类中的方法和属性都是静态的. out:标准输出,默认是控制台. in:标准输入,默认是键盘. System描述系统一些信息.获取系统属性信息:Properties getP ...

  10. Shell练习 行列转换

    原题:https://leetcode.com/problems/transpose-file/Given a text file file.txt, transpose its content. Y ...