13. marts 2002 - 11:37
Der er
7 kommentarer og
1 løsning
Value Object kode!
Jeg har forsøgt at lave et Value Object mønster, med noget kode som jeg har fundet.
Her er AuctionValueObject:
package valueobject;
import java.rmi.RemoteException;
import javax.ejb.*;
import java.util.*;
import java.text.NumberFormat;
/**
* A sample class shows the Value Object construction
*/
public class AuctionValueObject implements java.io.Serializable {
public String seller;
public String description;
public double startPrice;
public String summary;
public AuctionValueObject() {}
// constructor accepts all values
public AuctionValueObject(String seller, String description,
double startPrice, String summary) {
init(seller, description, startPrice, summary);
}
// new constructor based on previous data
public AuctionValueObject(AuctionValueObject avo) {
init(avo.seller, avo.description, avo.startPrice, avo.summary);
}
// method to set the values.
public void init(String seller, String description, double startPrice,
String summary) {
this.seller = seller;
this.description = description;
this.startPrice = startPrice;
this.summary = summary;
}
// method creates a new AuctionValueObject
public AuctionValueObject getData() {
return new AuctionValueObject(this);
}
}
Og her er bønnen, som nedarver fra AuctionValueObject:
package valueobject;
import java.rmi.*;
import javax.ejb.*;
public class AuctionItemBean extends AuctionValueObject implements EntityBean
{
EntityContext entityContext;
public String ejbCreate() throws CreateException, RemoteException
{
// @todo: Implement this method
return null;
}
public void ejbPostCreate() throws CreateException, RemoteException
{
}
public void ejbLoad() throws RemoteException
{
}
public void ejbStore() throws RemoteException
{
}
public void ejbRemove() throws RemoveException, RemoteException
{
}
public void ejbActivate() throws RemoteException
{
}
public void ejbPassivate() throws RemoteException
{
}
public void setEntityContext(EntityContext entityContext) throws RemoteException
{
this.entityContext = entityContext;
}
public void unsetEntityContext() throws RemoteException
{
entityContext = null;
}
public String ejbFindByPrimaryKey(String primKey) throws ObjectNotFoundException
{
//@todo: Implement this method
return null;
}
}
Det er jo så meningen at en klient skal kalde getData()
på AuctionValueObject, som så indeholde al data fra bønnen. Herefter skal AuctionValueObject så retunerer et object af sig selv.
Men hvordan får AuctionValueObject fat i data fra bønnen?
Og hvordan skal klienten se ud?
13. marts 2002 - 15:06
#2
Jeg har lavet lidt om i koden på bønnen og Value-objektet:
Min spørgsmål er stadig hvordan Value-objektet får data fra bønnen, blot ved at kalde getData() på Value-objektet?
package valueobject;
import java.rmi.RemoteException;
import javax.ejb.*;
import java.util.*;
import java.text.NumberFormat;
import java.rmi.*;
import javax.ejb.*;
import java.sql.*;
/**
* A sample class shows the Value Object construction
*/
public class AuctionValueObject implements java.io.Serializable {
public int id;
public String password;
public String username;
public String displayname;
public Integer role;
public Timestamp created;
public String realname;
public Integer active;
public String seller;
public String description;
public double startPrice;
public String summary;
public AuctionValueObject() {}
// constructor accepts all values
public AuctionValueObject(int id, String password, String username, Integer role, Timestamp created, String realname, Integer active)
{
init(id, password, username, role, created, realname, active);
}
// new constructor based on previous data
public AuctionValueObject(AuctionValueObject avo) {
init(avo.id, avo.password, avo.username, avo.role, avo.created, avo.realname, avo.active);
}
// method to set the values.
public void init (int id, String password, String username, Integer role, Timestamp created, String realname, Integer active)
{
this.id = id;
this.password = password;
this.username = username;
this.role = role;
this.created = created;
this.realname = realname;
this.active = active;
}
// method creates a new AuctionValueObject
public AuctionValueObject getData() {
return new AuctionValueObject(this);
}
}
package valueobject;
import java.rmi.*;
import javax.ejb.*;
import java.sql.*;
public class UserBean extends AuctionValueObject implements EntityBean
{
EntityContext entityContext;
public int id;
public String password;
public String username;
public String displayname;
public Integer role;
public Timestamp created;
public String realname;
public Integer active;
public String ejbCreate(int id, String password, String username, String displayname, Integer role, Timestamp created, String realname, Integer active) throws CreateException, RemoteException
{
this.id = id;
this.password = password;
this.username = username;
this.displayname = displayname;
this.role = role;
this.created = created;
this.realname = realname;
this.active = active;
return null;
}
public String ejbCreate(String username) throws RemoteException, CreateException, RemoteException
{
return ejbCreate(0, null, username, null, null, null, null, null);
}
public void ejbPostCreate(int id, String password, String username, String displayname, Integer role, Timestamp created, String realname, Integer active) throws CreateException, RemoteException
{
}
public void ejbPostCreate(String username) throws CreateException, RemoteException
{
ejbPostCreate(0, null, username, null, null, null, null, null);
}
public void ejbLoad() throws RemoteException
{
}
public void ejbStore() throws RemoteException
{
}
public void ejbRemove() throws RemoveException, RemoteException
{
}
public void ejbActivate() throws RemoteException
{
}
public void ejbPassivate() throws RemoteException
{
}
public void setEntityContext(EntityContext entityContext) throws RemoteException
{
this.entityContext = entityContext;
}
public void unsetEntityContext() throws RemoteException
{
entityContext = null;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getUsername()
{
return username;
}
public String getDisplayname()
{
return displayname;
}
public void setDisplayname(String displayname)
{
this.displayname = displayname;
}
public Integer getRole()
{
return role;
}
public void setRole(Integer role)
{
this.role = role;
}
public Timestamp getCreated()
{
return created;
}
public void setCreated(Timestamp created)
{
this.created = created;
}
public String getRealname()
{
return realname;
}
public void setRealname(String realname)
{
this.realname = realname;
}
public Integer getActive()
{
return active;
}
public void setActive(Integer active)
{
this.active = active;
}
}
17. april 2002 - 22:25
#7
Din EJB skal bare have en metode getData, ala
public class UserBean implements EntityBean {
public int id;
public String str1, str2, str3;
...
public UserVO getData() {
return new UserVO(this.id, this.str1, this.str2, ...);
}
public void setData(UserVO newData) {
this.id = newData.getId();
this.str1 = newData.getStr1();
this.str2 = newData.getStr2();
}
}
Og dit Value Object er bare
public class UserVO implements Serializable {
private int id;
private String str1, str2, str3;
...
public UserVO(int id, String str1, ...) {
this.id = id;
this.str1 = str1;
...
}
public int getId() {
return id;
}
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
}
Så er din klient lige efter bogen:
UserHome h = ..... retrieve home ...
User u = h.findByPrimaryKey(new Integer(12));
UserVO = u.getData(); // Off-load VO from bean
u = null; // Drop the bean
// Reverse, when setting data again.
Klienten er typisk en session bean, der fungerer som facade for entity beans.