Avatar billede fear Nybegynder
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?
Avatar billede disky Nybegynder
13. marts 2002 - 12:02 #1
Brug get og set metoder til at hente data med.
Avatar billede fear Nybegynder
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;
  }
}
Avatar billede disky Nybegynder
15. marts 2002 - 23:40 #3
når du spørger hvordan, ønsker du så at vide hvordan det foregår på bytekode niveau, eller bare om den gør det ?

For det gør den :)
Avatar billede disky Nybegynder
15. marts 2002 - 23:41 #4
forresten pæn kode du laver :)
Avatar billede fear Nybegynder
16. marts 2002 - 16:33 #5
Jeg er interesseret i hvordan min test-klient skal se ud. Altså i alm. java kode :-)
Avatar billede disky Nybegynder
16. marts 2002 - 20:33 #6
sorry det har jeg ikke lige tid til at lave for dig. Men jeg er gerne behjælpelig med ideer.
Avatar billede logical Nybegynder
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.
Avatar billede fear Nybegynder
23. april 2002 - 01:21 #8
tak tak
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester

Seneste spørgsmål Seneste aktivitet
I går 21:00 Libre Office Impress Af Frank i Andre styresystemer
I går 11:47 VB script Af Jenshentze i Word
I går 11:21 Popup ved opstart Af mort1 i Windows
04/0918:50 Slet lokal konto Af ErikHg i Windows
04/0916:05 Ændre tal i en celle Af xvid i Excel