观察者模式最佳案例实现[JAVA]

/**
* 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.
*
*/
 
   
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.
* 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. * @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 list; private int capacity; public StockMarket(int capacity){ this.capacity=capacity; init(this.capacity); } private List init(int n){ list= new ArrayList(); for(int i=0;i


 

你可能感兴趣的:(观察者模式最佳案例实现[JAVA])