Java 实现MVC模式的例子

类别:Java 点击:0 评论:0 推荐:


/*data.java
* Created on 2004-6-17
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package mvcTest2;

/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class Data
{
public int value; // 学生年龄值
public String name; // 学生名
}

/*model.java
* Created on 2004-6-17
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package mvcTest2;

/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.util.*;
import java.util.Observable;

public class Model extends Observable
{
protected ArrayList data = new ArrayList();

public Model()
{
super();
}
public Model(int[] value, String[] name)
{
for ( int i = 0; i< value.length; i++ )
{
addData(value[i],name[i]);
}
}
public Model(Data[] data)
{
for ( int i = 0; i< data.length; i++ )
{
addData(data[i]);
}
}
public void addData(int value, String name)
{
Data data = new Data();
data.value = value;
data.name = name;
this.data.add(data);
setChanged(); // Indicates that the model has changed
notifyObservers(this);
}
public void addData(Data data)
{
this.data.add(data);
setChanged(); // Indicates that the model has changed
notifyObservers(this);
}
public Data getData(int idx)
{
return (Data)(data.get(idx));
}

public int size()
{
return data.size();
}

// 当数据改变时,由Controller调用此方法,通知各个Observer,刷新视图.
public void changeModel(Model model)
{
data.clear();
for (int i=0; i

本文地址:http://com.8s8s.com/it/it16320.htm