package 观察者模式;
import java.util.ArrayList;
public class Stock {
private ArrayList<Investor> investors;
private String stockName;
private double price;
public Stock(String stockName,double price) {
this.stockName=stockName;
this.price=price;
investors=new ArrayList<Investor>();
}
public void attach(Investor investor){
investors.add(investor);
}
public void detach(Investor investor){
investors.remove(investor);
}
public String getStockName() {
return stockName;
}
public void setStockName(String stockName) {
this.stockName = stockName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
double range=Math.abs(price-this.price)/this.price;
this.price=price;
if(range>=0.05){
this.notifyInvestor();
}
}
public void notifyInvestor() {
for(Investor obj:investors){
obj.reponse(this);
}
}
}
package 观察者模式;
public interface Investor {
public void reponse(Stock stock);
}
package 观察者模式;
public class ConcreteInvestor implements Investor {
private String name;
public ConcreteInvestor(String name){
this.name=name;
}
@Override
public void reponse(Stock stock) {
System.out.println("提示股民:"+name);
System.out.println("-------股票"+stock.getStockName());
System.out.println("-------价格波动幅度超过5%");
System.out.println("新价格是:"+stock.getPrice());
System.out.println("========================");
}
}
package 观察者模式;
public class Test {
public static void main(String[] args) {
Investor investor1,investor2;
investor1=new ConcreteInvestor("小王");
investor2=new ConcreteInvestor("小孟");
Stock bilibili=new Stock("Anything",99.0);
Stock a=new Stock("Whatever",80.6);
bilibili.attach(investor1);
a.attach(investor2);
bilibili.setPrice(199.0);
a.setPrice(120.6);
}
}
某商品管理系统的商品名称存储在一个字符串数组中,现需要自定义一个双向迭代器(MyIterator)实现对该商品名称数组的双向(前向和后向)遍历。绘制类图并编程实现。
package 迭代器模式;
//抽象迭代器接口,定义向前和向后的双向遍历
public interface Iterator {
void forward();
void backward();
boolean hashNext();
Object currentItem();
}
package 迭代器模式;
//抽象聚合类,存储对象并定义创建迭代器对象的接口
public interface Aggregate {
Iterator createInterator();
}
package 迭代器模式;
//具体聚合类,实现创建迭代器的接口,返回一个与具体聚合对象对应的具体迭代器实例
public class MyAggregate implements Aggregate {
public String[] names={"衣服","鞋子","化妆品","包包"};//名称存在一个字符串数组中
public Iterator createInterator() {
return new MyIterator();
}
public Iterator createInterator1() {
return new MyIterator1();
}//返回具体迭代器实例
public class MyIterator implements Iterator {
private int currentIndex;
public void forward() {
currentIndex=names.length;
for(int i=currentIndex;i>0;i--){
System.out.println(names[i]);
currentIndex--;
}
}
public void backward() {
currentIndex=0;
for(int i=currentIndex;i<names.length;i++){
System.out.println(names[i]);
currentIndex++;
}
}
public boolean hashNext() {
if(currentIndex<names.length){
return true;
}
return false;
}
public Object currentItem() {
if(this.hashNext()){
return names[currentIndex++];
}
return null;
}
}
public class MyIterator1 implements Iterator{
int flag=names.length-1;
public void forward(){}
public void backward(){}
public boolean hashNext(){
if(flag>-1){
return true;
}
return false;
}
public Object currentItem(){
if(this.hashNext()){
return names[flag--];
}
return null;
}
}
}
package 迭代器模式;
public class Test {
public static void main(String[] args) {
MyAggregate a=new MyAggregate();
System.out.println("向后遍历:");
for(Iterator i=a.createInterator();i.hashNext();){
String name=(String)i.currentItem();
System.out.println(name);
}
System.out.println("-----------");
System.out.println("向前遍历:");
for(Iterator i=a.createInterator1();i.hashNext();){
String name=(String)i.currentItem();
System.out.println(name);
}
}
}