在設(shè)計(jì)模式2中我們看到,在entity bean和struct之間有很多重復(fù)的代碼比如同樣的字段聲明(對(duì)應(yīng)數(shù)據(jù)庫中的表列)。如果讓entity bean從結(jié)構(gòu)繼承下來就可以避免冗余的代碼。但是這種設(shè)計(jì),仍然不能顯示beans之間的聯(lián)系。
Code snippet for Company Entity Bean
public class CompanyBean extends CompanyStruct
?。?BR> implements EntityBean
EntityContext entityContext;
//all fields in CompanyStruct are available for CMP
public Integer ejbCreate(CompanyStruct Struct)
throws CreateException
{
this.comId = struct.comId; //set the primary key
setData(struct);//this removes some redundant code
return null;
}
}
其余的代碼比如getdata()和setdata()方法的實(shí)現(xiàn)和設(shè)計(jì)模式2中是完全一樣的。
Code snippet for Company Entity Bean
public class CompanyBean extends CompanyStruct
?。?BR> implements EntityBean
EntityContext entityContext;
//all fields in CompanyStruct are available for CMP
public Integer ejbCreate(CompanyStruct Struct)
throws CreateException
{
this.comId = struct.comId; //set the primary key
setData(struct);//this removes some redundant code
return null;
}
}
其余的代碼比如getdata()和setdata()方法的實(shí)現(xiàn)和設(shè)計(jì)模式2中是完全一樣的。

