UJO - JavaBean框架


Apache
跨平台
Java

软件简介

UJO
Framework为对象提供了一种有别于JavaBeans的架构。最初的想法来源于Java5.0的范型。但是,随着时间的流逝,这种架构表现一些激动人心的特性:

  • 很轻松实现对象的自省(introspection)而不再需要笨重的PropertyDescriptor实现
  • 一个transfer UJO对象属性可以是集合(并非values)
  • 核心部分才两个接口(Ujo和UjoProperty),实现起来非常简单。

代码示例:

import java.util.HashMap;  
import org.ujoframework.*;  
import org.ujoframework.core.UjoManager;  
import org.ujoframework.implementation.map.*;

@SuppressWarnings("unchecked")  
public class Person implements Ujo {

  public static final MapProperty NAME = new MapProperty("Name", String.class);  
  public static final MapProperty MALE = new MapProperty("Male", Boolean.class);  
  public static final MapProperty CASH = new MapProperty("Cash", 0d);

  // --- The start of the MapUjo implementation ---  
  private HashMap map = new HashMap();    
  public Object readValue(UjoProperty property) {  
    Object result = map.get(property);  
    return result!=null ? result : property.getDefault();  
  }  
  public void writeValue(UjoProperty property, Object value) {  
    map.put(property, value);  
  }  
  public UjoProperty[] readProperties() {  
    return UjoManager.getInstance().readProperties(getClass());  
  }  
  public boolean readAuthorization(UjoAction action, UjoProperty property, Object value) {  
    return true;  
  } // --- The end of MapUjo implementation ---

  /** Add cash in the **Ujo implementation** */  
  public void addCash(double cash) {  
    double newPrice = CASH.of(this) + cash;  
    CASH.setValue(this, newPrice);  
  }  
}