Eclipse中IAdaptable 思想简单应用
Eclipse中大量地使用IAdaptable来动态的进行类型转化,想来这个方法还是挺好的,如果不是,它怎么能成eclipse的核心呢。下面给出应用IAdaptable的一个简单例子。
下面给出实现:
具有转换类型能力的IAdaptable接口:
public
interface
IAdaptable
{
public Object getAdapter(Class clazz);
}
{
public Object getAdapter(Class clazz);
}
实体User:
public
class
User
implements
IAdaptable
{
private int id;
private String name;
private String sex;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
public Object getAdapter(Class clazz)
{
if(clazz==Model.class){
return getModelAdapter();
}
return null;
}
private Object getModelAdapter()
{
Model model = new Model();
model.addAttribute("id", id);
model.addAttribute("name", name);
model.addAttribute("sex", sex);
model.setData(this);
return model;
}
}
{
private int id;
private String name;
private String sex;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getSex()
{
return sex;
}
public void setSex(String sex)
{
this.sex = sex;
}
public Object getAdapter(Class clazz)
{
if(clazz==Model.class){
return getModelAdapter();
}
return null;
}
private Object getModelAdapter()
{
Model model = new Model();
model.addAttribute("id", id);
model.addAttribute("name", name);
model.addAttribute("sex", sex);
model.setData(this);
return model;
}
}
实体Address:
public
class
Address
implements
IAdaptable
{
private int id;
private String name;
private String street;
private String zipcode;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getStreet()
{
return street;
}
public void setStreet(String street)
{
this.street = street;
}
public String getZipcode()
{
return zipcode;
}
public void setZipcode(String zipcode)
{
this.zipcode = zipcode;
}
public Object getAdapter(Class clazz)
{
if(clazz==Model.class){
return getModelAdapter();
}
return null;
}
private Object getModelAdapter()
{
Model model = new Model();
model.addAttribute("id", id);
model.addAttribute("name", name);
model.addAttribute("street", street);
model.addAttribute("zipcode", zipcode);
model.setData(this);
return model;
}
}
{
private int id;
private String name;
private String street;
private String zipcode;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getStreet()
{
return street;
}
public void setStreet(String street)
{
this.street = street;
}
public String getZipcode()
{
return zipcode;
}
public void setZipcode(String zipcode)
{
this.zipcode = zipcode;
}
public Object getAdapter(Class clazz)
{
if(clazz==Model.class){
return getModelAdapter();
}
return null;
}
private Object getModelAdapter()
{
Model model = new Model();
model.addAttribute("id", id);
model.addAttribute("name", name);
model.addAttribute("street", street);
model.addAttribute("zipcode", zipcode);
model.setData(this);
return model;
}
}
模型,兼容User和Address
public
class
Model
implements
IAdaptable
{
private Map<String, Object> attributes;
private Object data;
public Model()
{
attributes = new HashMap<String, Object>();
}
public Object getAdapter(Class clazz)
{
if(clazz==User.class){
return getUserAdapter();
}
if(clazz==Address.class){
return getAddressAdapter();
}
return null;
}
private Object getAddressAdapter()
{
Address address = (Address)data;
address.setId(Integer.parseInt(String.valueOf(this.getAttribute("id"))));
address.setName(String.valueOf(this.getAttribute("name")));
address.setStreet(String.valueOf(this.getAttribute("street")));
address.setZipcode(String.valueOf(this.getAttribute("zipcode")));
return address;
}
private Object getUserAdapter()
{
User user = (User)data;
user.setId(Integer.parseInt(String.valueOf(this.getAttribute("id"))));
user.setName(String.valueOf(this.getAttribute("name")));
user.setSex(String.valueOf(this.getAttribute("sex")));
return user;
}
public void addAttribute(String key,Object value){
attributes.put(key, value);
}
public Object getAttribute(String key){
return attributes.get(key);
}
public Map<String, Object> getAttributes()
{
return attributes;
}
public Object getData()
{
return data;
}
public void setData(Object data)
{
this.data = data;
}
}
{
private Map<String, Object> attributes;
private Object data;
public Model()
{
attributes = new HashMap<String, Object>();
}
public Object getAdapter(Class clazz)
{
if(clazz==User.class){
return getUserAdapter();
}
if(clazz==Address.class){
return getAddressAdapter();
}
return null;
}
private Object getAddressAdapter()
{
Address address = (Address)data;
address.setId(Integer.parseInt(String.valueOf(this.getAttribute("id"))));
address.setName(String.valueOf(this.getAttribute("name")));
address.setStreet(String.valueOf(this.getAttribute("street")));
address.setZipcode(String.valueOf(this.getAttribute("zipcode")));
return address;
}
private Object getUserAdapter()
{
User user = (User)data;
user.setId(Integer.parseInt(String.valueOf(this.getAttribute("id"))));
user.setName(String.valueOf(this.getAttribute("name")));
user.setSex(String.valueOf(this.getAttribute("sex")));
return user;
}
public void addAttribute(String key,Object value){
attributes.put(key, value);
}
public Object getAttribute(String key){
return attributes.get(key);
}
public Map<String, Object> getAttributes()
{
return attributes;
}
public Object getData()
{
return data;
}
public void setData(Object data)
{
this.data = data;
}
}
视图,将模型显示:
public
class
View
{
private Model model;
public Model getModel()
{
return model;
}
public void setModel(Model model)
{
this.model = model;
}
//显示模型数据,这里你可以用别的方式,甚至应用到web上
public void viewModel(){
System.out.println(model.getData().getClass().getName());
Set<Entry<String, Object>> entry = model.getAttributes().entrySet();
for(Entry<String, Object> entry2 : entry)
{
System.out.println(entry2.getKey() + " = " + entry2.getValue());
}
}
}
{
private Model model;
public Model getModel()
{
return model;
}
public void setModel(Model model)
{
this.model = model;
}
//显示模型数据,这里你可以用别的方式,甚至应用到web上
public void viewModel(){
System.out.println(model.getData().getClass().getName());
Set<Entry<String, Object>> entry = model.getAttributes().entrySet();
for(Entry<String, Object> entry2 : entry)
{
System.out.println(entry2.getKey() + " = " + entry2.getValue());
}
}
}
跑一下:
public
class
AdapterTest
{
public static void main(String[] args)
{
User user = new User();
user.setId(1001);
user.setName("useraaa");
user.setSex("M");
//将实体转换成模型
Model model = (Model)user.getAdapter(Model.class);
assert(model!=null):" model should not be null !";
View view = new View();
view.setModel(model);//注入模型
view.viewModel();//显示
//把模型转换为实体
User newUser = (User)model.getAdapter(User.class);
assert(newUser!=null):" entity should not be null !";
System.out.println(newUser.getId()+ " " + newUser.getName());
Address address = new Address();
address.setId(2002);
address.setName("addressaaa");
address.setStreet("street 1");
address.setZipcode("5002");
//将实体转换成模型
model = (Model)address.getAdapter(Model.class);
assert(model!=null):" model should not be null !";
view.setModel(model);//注入模型
view.viewModel();//显示
//把模型转换为实体
Address newAddress = (Address)model.getAdapter(Address.class);
assert(newAddress!=null):" entity should not be null !";
System.out.println(newAddress.getId()+ " " + newAddress.getName() + " "+newAddress.getStreet() + " "+ newAddress.getZipcode());
}
}
{
public static void main(String[] args)
{
User user = new User();
user.setId(1001);
user.setName("useraaa");
user.setSex("M");
//将实体转换成模型
Model model = (Model)user.getAdapter(Model.class);
assert(model!=null):" model should not be null !";
View view = new View();
view.setModel(model);//注入模型
view.viewModel();//显示
//把模型转换为实体
User newUser = (User)model.getAdapter(User.class);
assert(newUser!=null):" entity should not be null !";
System.out.println(newUser.getId()+ " " + newUser.getName());
Address address = new Address();
address.setId(2002);
address.setName("addressaaa");
address.setStreet("street 1");
address.setZipcode("5002");
//将实体转换成模型
model = (Model)address.getAdapter(Model.class);
assert(model!=null):" model should not be null !";
view.setModel(model);//注入模型
view.viewModel();//显示
//把模型转换为实体
Address newAddress = (Address)model.getAdapter(Address.class);
assert(newAddress!=null):" entity should not be null !";
System.out.println(newAddress.getId()+ " " + newAddress.getName() + " "+newAddress.getStreet() + " "+ newAddress.getZipcode());
}
}
输出结果:
adapter.entity.User
sex = M
name = useraaa
id = 1001
1001 useraaa
adapter.entity.Address
street = street 1
name = addressaaa
zipcode = 5002
id = 2002
2002 addressaaa street 1 5002
这里为了简单化,省去控制层,但这并不妨碍对IAdaptable的理解。你可以将转换工作从实体中抽取出来,使结构更加清晰。你可以在这里下载例子代码。