某在线股票软件需要提供如下功能:当股票购买者所购买的某支股票价格变化幅度达到5%时,系统将自动发送通知(包括新价格)给购买该股票的股民。现使用观察者模式设计该系统。

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);

}

}

}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
观察者模式来实这个功能需要定义两个角色:观察者和被观察者。 首先,我们需要定义一个被观察者类,该类包含股票价格信息,并且可以添加和删除观察者。当股票价格发生变化,该类会通知所有观察者。 ```java import java.util.ArrayList; import java.util.List; public class Stock { private String name; private double price; private List<Observer> observers; public Stock(String name, double price) { this.name = name; this.price = price; this.observers = new ArrayList<>(); } public void attach(Observer observer) { observers.add(observer); } public void detach(Observer observer) { observers.remove(observer); } public void setPrice(double price) { double oldPrice = this.price; this.price = price; if (Math.abs(price - oldPrice) / oldPrice >= 0.05) { notifyObservers(); } } public String getName() { return name; } public double getPrice() { return price; } private void notifyObservers() { for (Observer observer : observers) { observer.update(this); } } } ``` 然后,我们需要定义一个观察者接口,该接口包含一个更方法,用于在被观察者通知观察者调用。 ```java public interface Observer { void update(Stock stock); } ``` 最后,我们需要定义一个具体的观察者类,该类实了观察者接口,在更方法中实了具体的通知逻辑。 ```java public class StockObserver implements Observer { private String name; public StockObserver(String name) { this.name = name; } @Override public void update(Stock stock) { System.out.println(name + " received notification: " + stock.getName() + " price has changed to " + stock.getPrice()); } } ``` 使用示例: ```java public class Test { public static void main(String[] args) { Stock stock = new Stock("Apple", 100.0); StockObserver observer1 = new StockObserver("John"); StockObserver observer2 = new StockObserver("Mike"); stock.attach(observer1); stock.attach(observer2); stock.setPrice(110.0); // no notification stock.setPrice(120.0); // no notification stock.setPrice(130.0); // no notification stock.setPrice(140.0); // notification sent to John and Mike stock.detach(observer2); stock.setPrice(150.0); // notification sent to John only } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值