• No se han encontrado resultados

Disseny de la persistència Serialització

N/A
N/A
Protected

Academic year: 2021

Share "Disseny de la persistència Serialització"

Copied!
9
0
0

Texto completo

(1)

eria del SW II: Disseny de la persistència

Toni Navarrete

(2)

eria del SW II: Disseny de la persistència

• És la forma que té Java per fer permanents els

objectes (per guardar-los a disc)

• Serialització s’usa típicament en RMI per enviar

objectes. També com a “persistència light”

• Les classes dels objectes que es vulguin fer

persistents han d’implementar la interfície

Serializable

• Aquesta interfície és buida

package java.io;

public interface Serializable {

// no hi ha res aquí!!

(3)

eria del SW II: Disseny de la persistència

• Què es guarda d’un objecte quan es serialitza?

– Tots els atributs que no siguin

• static (perquè són atributs de la classe i no de la instància)

• transient (indica que no s’ha de serialitzar). Per exemple,

atributs que es poden calcular a partir d’altres atributs,

atributs confidencials ...

– Encara que sigui private se serialitza (compte!)

– Referències a altres objectes...

• Es guarden els objectes referenciats i la seva referència

• Per poder-los recuperar cal que també siguin serialitzables

(4)

eria del SW II: Disseny de la persistència

• Escriure un objecte en un Object Stream.

Example:

FileOutputStream out = new FileOutputStream(“nomFitxer");

ObjectOutputStream s = new ObjectOutputStream(out);

s.writeObject(objecte);

s.flush();

(5)

eria del SW II: Disseny de la persistència

• Recuperar un objecte des d’un Object

Stream. Exemple

FileInputStream in = new FileInputStream("nomFitxer");

ObjectInputStream s = new ObjectInputStream(in);

try {

Color c = (Color)s.readObject();

System.out.println("R: " + c.R);

System.out.println("G: " + c.G);

System.out.println("B: " + c.B);

} catch(ClassNotFoundException cnfe) {

(6)

eria del SW II: Disseny de la persistència

}

void guardaObjecte() throws FileNotFoundException, IOException { FileOutputStream out = new FileOutputStream("ObjecteColor"); ObjectOutputStream s = new ObjectOutputStream(out);

s.writeObject(this); s.flush();

}

void recuperaObjecte() throws FileNotFoundException, IOException { FileInputStream in = new FileInputStream("ObjecteColor");

ObjectInputStream s = new ObjectInputStream(in); try {

ColorRGB c = (ColorRGB)s.readObject();

System.out.println("R: " + c.R); System.out.println("G: " + c.G); System.out.println("B: " + c.B); } catch(ClassNotFoundException cnfe) {

System.out.println("Excepció classe no definida"); }

}

public static void main(String[] args) {

ColorRGB color = new ColorRGB(0,0,255); try {

color.guardaObjecte(); color.recuperaObjecte(); } catch(Exception e) {

System.out.println("Error d'E/S"); System.exit(0); }

(7)

eria del SW II: Disseny de la persistència

major control de què se serialitza: Externalizable

• 2 mètodes per personalitzar el comportament

quan se serialitza:

– public void writeExternal(ObjectOutput out) throws

IOException

• Només es guarden els atributs que aquí s’especifiquin

– public void readExternal(ObjectInput in) throws

IOException

• Llegeix la tira de bits emmagatzemats amb writeExternal i

recupera l’objecte

• Crida al constructor per defecte i recupera els atributs

especificats aquí

(8)

eria del SW II: Disseny de la persistència

this.G = G; this.B = B; }

public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(R);

out.writeInt(B);

//si fos un tipus no primitiu (per exemple String o una classe pròpia de l’aplicació) es faria amb writeObject

}

public void readExternal(ObjectInput in) throws IOException { R = in.readInt();

B = in.readInt();

//si fos un tipus no primitiu (per exemple String o una class pròpia de l’aplicació) es faria amb readObject

}

public static void main(String[] args) throws IOException, FileNotFoundException, ClassNotFoundException{ ColorRGB1 color = new ColorRGB1(100,200,255);

FileOutputStream out = new FileOutputStream("ObjecteColor"); ObjectOutputStream sout = new ObjectOutputStream(out); sout.writeObject(color);

sout.close();

FileInputStream in = new FileInputStream("ObjecteColor"); ObjectInputStream sin = new ObjectInputStream(in); ColorRGB1 c = (ColorRGB1)sin.readObject();

System.out.println("R: "+ c.R); System.out.println("G: "+ c.G); System.out.println("B: "+ c.B);

(9)

eria del SW II: Disseny de la persistència

• Avantantges:

– Estàndard en qualsevol ambient Java

– Fàcil d’utilitzar

– El model de dades és directament el de les

classes (aprofita OO)

• Persistència transitiva

• Inconvenients:

– No té les característiques d’un gestor de BD

robust

Referencias

Documento similar

transacting in weighed silver bullion was widespread (cf. This monetary use of silver bullion did not begin in the Iberian Peninsula during the years of the Second Punic War,

__________ [2000a]: Preface to Statements of International Public Sector Accounting Standards, IFAC, New York.. IFAC,

I core of tenths of Earth masses, heavy element fraction 0.52.. I see talk

After interviewing the hospital Human Resources manager, the training manager and the managing director, a meeting was arranged in order to demonstrate and discuss of the

In addition, given the widespread use of prescription benzodiazepines in society, and their diversion to the illicit drug market, the increase in new benzodiazepines might also

public static void main(String[] args) { String counterFile = args[0];. int initialValue

This will serve as a helpful navigating tool for researchers to map out the direction that cultural theory was taking at the beginning of the twenty- first century towards Jürgen

The technology in the new Liceu included the subtitling system Figaro that allows every seat in the theatre to follow the libretto in Cat- alan, English or Spanish. Surtitles