一对一免费咨询: 18215660330

在软件设计开发的观察者模式第一季中,我们讨论了下面的问题,没有Observer模式的问题的解决方案以及解决方案的问题。

假设我们正在构建一个cricket应用程序,通知观众有关当前得分,运行率等信息。假设我们已经制作了两个显示元素CurrentScoreDisplay和AverageScoreDisplay。CricketData具有所有数据(运行,碗等),每当数据发生变化时,显示元素都会被新数据通知,并相应地显示最新数据

将Observer模式应用于上述问题:
让我们看看如何使用观察者模式改进应用程序的设计。如果我们观察数据流,我们可以很容易地看到CricketData和显示元素遵循主题 – 观察者的关系。

新类图:
O3

Java实现:

filter_none

play_arrow

brightness_4
// Java program to demonstrate working of
// onserver pattern
import java.util.ArrayList;
import java.util.Iterator;

// Implemented by Cricket data to communicate
// with observers
interface Subject
{
public void registerObserver(Observer o);
public void unregisterObserver(Observer o);
public void notifyObservers();
}

class CricketData implements Subject
{
int runs;
int wickets;
float overs;
ArrayList<Observer> observerList;

public CricketData() {
observerList = new ArrayList<Observer>();
}

@Override
public void registerObserver(Observer o) {
observerList.add(o);
}

@Override
public void unregisterObserver(Observer o) {
observerList.remove(observerList.indexOf(o));
}

@Override
public void notifyObservers()
{
for (Iterator<Observer> it =
observerList.iterator(); it.hasNext();)
{
Observer o = it.next();
o.update(runs,wickets,overs);
}
}

// get latest runs from stadium
private int getLatestRuns()
{
// return 90 for simplicity
return 90;
}

// get latest wickets from stadium
private int getLatestWickets()
{
// return 2 for simplicity
return 2;
}

// get latest overs from stadium
private float getLatestOvers()
{
// return 90 for simplicity
return (float)10.2;
}

// This method is used update displays
// when data changes
public void dataChanged()
{
//get latest data
runs = getLatestRuns();
wickets = getLatestWickets();
overs = getLatestOvers();

notifyObservers();
}
}

// This interface is implemented by all those
// classes that are to be updated whenever there
// is an update from CricketData
interface Observer
{
public void update(int runs, int wickets,
float overs);
}

class AverageScoreDisplay implements Observer
{
private float runRate;
private int predictedScore;

public void update(int runs, int wickets,
float overs)
{
this.runRate =(float)runs/overs;
this.predictedScore = (int)(this.runRate * 50);
display();
}

public void display()
{
System.out.println(“\nAverage Score Display: \n”
+ “Run Rate: ” + runRate +
“\nPredictedScore: ” +
predictedScore);
}
}

class CurrentScoreDisplay implements Observer
{
private int runs, wickets;
private float overs;

public void update(int runs, int wickets,
float overs)
{
this.runs = runs;
this.wickets = wickets;
this.overs = overs;
display();
}

public void display()
{
System.out.println(“\nCurrent Score Display:\n”
+ “Runs: ” + runs +
“\nWickets:” + wickets +
“\nOvers: ” + overs );
}
}

// Driver Class
class Main
{
public static void main(String args[])
{
// create objects for testing
AverageScoreDisplay averageScoreDisplay =
new AverageScoreDisplay();
CurrentScoreDisplay currentScoreDisplay =
new CurrentScoreDisplay();

// pass the displays to Cricket data
CricketData cricketData = new CricketData();

// register display elements
cricketData.registerObserver(averageScoreDisplay);
cricketData.registerObserver(currentScoreDisplay);

// in real app you would have some logic to
// call this function when data changes
cricketData.dataChanged();

//remove an observer
cricketData.unregisterObserver(averageScoreDisplay);

// now only currentScoreDisplay gets the
// notification
cricketData.dataChanged();
}
}
输出:

平均分数显示:
运行率:8.823529
PredictedScore:441

当前分数显示:
运行:90
售票窗口:2
Overs:10.2

当前分数显示:
运行:90
售票窗口:2
Overs:10.2

注意: 现在我们可以添加/删除尽可能多的观察者,而无需更改主题。

需要软件开发记得找我们噢

成都软件开发 最好的成都软件定制开发公司
18215660330