另一种可能的嵌入式、内置 AFAIK 解决方案。
来自Adapter pattern:
...允许从另一个类使用现有类的接口
界面。它通常用于使现有的类与其他类一起工作
无需修改其源代码。
此示例仅供参考,并非已确认的解决方案,但需要编写结构良好且灵活应对变化的代码。
所以...
示例:
如果我们有类似的 JPAEntity
@Entity
@Table(name="EntityClass", uniqueConstraints = {
@UniqueConstraint(columnNames = {"ID"})})
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "EntityClass.findAll", query = "SELECT a FROM EntityClass a"),
@NamedQuery(name = "EntityClass.findById", query = "SELECT a FROM EntityClass a WHERE a.id = :id"),
@NamedQuery(name = "EntityClass.findByYear", query = "SELECT a FROM EntityClass a WHERE a.year = :year")})
public class EntityClass implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(nullable = false)
private Integer id;
@Basic(optional = false)
@Column(nullable = false, length = 4)
private String year;
//...and others
private static final long serialVersionUID = 1L;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "fKYear")
private Collection<Some1> some1Collection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "fKYear")
private Collection<Some2> some2Collection;
public EntityClass() {
}
public EntityClass(Integer id) {
this.id = id;
}
public EntityClass(Integer id, String year) {
this.id = id;
this.year = year;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
@XmlTransient
public Collection<Some1> getSome1Collection() {
return some1Collection;
}
public void setSome1Collection(Collection<Some1> some1Collection) {
this.some1Collection = some1Collection;
}
@XmlTransient
public Collection<Some2> getSome2Collection() {
return some2Collection;
}
public void setSome2Collection(Collection<Some2> some2Collection) {
this.some2Collection = some2Collection;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof EntityClass)) {
return false;
}
EntityClass other = (EntityClass) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return this.year;
}
}
我们创建了一个这样的接口
public interface IFXEntityClass{
void setYear(String year);
String getYear();
//...
void setSome1Collection(Collection<Some1> some1);
Collection<Some1> getSome1Collection();
//...
}
我们可以像这样创建我们的 FXClass
public class FXEntityClass implements IFXEntityClass{
private final StringProperty yearProperty=new SimpleStringProperty();
public StringProperty getYearProperty(){ return this.yearProperty;}
public void setYear(String year){ this.year.set(year); }
public String getYear(){ return this.year.get(); }
//...
void setSome1Collection(Collection<Some1> some1)( //do something)
Collection<Some1> getSome1Collection(){ return null;}
//...
}
现在我们拥有了所需的一切。让我们创建适配器。
public class EntityClassToFXEntityClassAdaptor implements IFXEntityClass{
private EntityClass entity;
private final StringProperty yearProperty=new SimpleStringProperty();
public EntityClassToFXEntityClassAdaptor(EntityClass e){
this.entity=e;
//adapt it as you want
this.yearProperty.set(e.getYear());
//...
}
public StringProperty getYearProperty(){ return this.yearProperty;}
public void setYear(String year){ this.year.set(year); }
public String getYear(){ return this.year.get(); }
//...
void setSome1Collection(Collection<Some1> some1)( //do something)
Collection<Some1> getSome1Collection(){ return null;}
//...
}
我们终于可以使用它了
EntityClass ec=something; //get an instance of JPAEntity
EntityClassToFXEntityClassAdaptor adaptor=new EntityClassToFXEntityClassAdaptor(ec);
adaptor.getYearProperty();
还有一个更清晰的示例说明如何应用您在
中找到的这种模式
import java.awt.*;
public class CheckboxAdapter extends Checkbox{
public CheckboxAdapter(String n){
super(n);
}
public boolean isSelected(){
return getState();
}
//... continue
}