I want to Know about Vo and Serilization

Hi FriendsI am having one doubtWhen we carete a Value objet then we provide a deafault constructor.Also we use to serilize it.What is the use of deafult construtor even if we are not intilizing any parameters..
[252 byte] By [rajpuniaa] at [2007-10-3 3:58:31]
# 1

> Hi Friends

> I am having one doubt

> When we carete a Value objet then we provide a

> deafault constructor.

> Also we use to serilize it.

> What is the use of deafult construtor even if we

> are not intilizing any parameters..

Value Object should not have a default constructor. (Unless something like Hibernate demands it, in which case you can make it private.)

A real Value Object should be immutable, IMO.

Who says your default constructor should not initialize parameters? It should be setting them to sensible defaults.

If there are no "sensible defaults", then perhaps you need to rethink having a default constructor. There's nothing that says you have to have one.

%

duffymoa at 2007-7-14 21:57:03 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

Maybe we should agree on what we mean by "Value Object" first.

I like Eric Evans' definition: A value object is an object whose identity doesn't matter, and its state is immutable.

If I write a Person object, and that Person is persisted in a relational database with a primary key, that Person object is likely to have a primary key as an attribute. That's an identity that my application needs to track as that object is used.

If I write a Money class, it can be a Value Object because any instance that represents $10 USD can be used by any other object that needs it. The Java wrapper classes are VOs.

%

duffymoa at 2007-7-14 21:57:03 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> Maybe we should agree on what we mean by "Value

> Object" first.

>

> I like Eric Evans' definition: A value object is an

> object whose identity doesn't matter, and its state

> is immutable.

>

> If I write a Person object, and that Person is

> persisted in a relational database with a primary

> key, that Person object is likely to have a primary

> key as an attribute. That's an identity that my

> application needs to track as that object is used.

>

> If I write a Money class, it can be a Value Object

> because any instance that represents $10 USD can be

> used by any other object that needs it. The Java

> wrapper classes are VOs.

>

> %

Thanks for Ur valubable Input.

Is String Class provide any type of design Pattern?

rajpuniaa at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

> > Maybe we should agree on what we mean by "Value

> > Object" first.

> >

> > I like Eric Evans' definition: A value object is

> an

> > object whose identity doesn't matter, and its

> state

> > is immutable.

> >

> > If I write a Person object, and that Person is

> > persisted in a relational database with a primary

> > key, that Person object is likely to have a

> primary

> > key as an attribute. That's an identity that my

> > application needs to track as that object is used.

> >

> > If I write a Money class, it can be a Value Object

> > because any instance that represents $10 USD can

> be

> > used by any other object that needs it. The Java

> > wrapper classes are VOs.

> >

> > %

> Thanks for Ur valubable Input.

> Is String Class provide any type of design

> Pattern?

No. What do you mean? Why do you ask?

Strings are immutable, we know that.

What good would a pattern be here?

%

duffymoa at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

> > Maybe we should agree on what we mean by "Value

> > Object" first.

> >

> > I like Eric Evans' definition: A value object is

> an

> > object whose identity doesn't matter, and its

> state

> > is immutable.

> >

> > If I write a Person object, and that Person is

> > persisted in a relational database with a primary

> > key, that Person object is likely to have a

> primary

> > key as an attribute. That's an identity that my

> > application needs to track as that object is used.

> >

> > If I write a Money class, it can be a Value Object

> > because any instance that represents $10 USD can

> be

> > used by any other object that needs it. The Java

> > wrapper classes are VOs.

> >

> > %

> Thanks for Ur valubable Input.

> Is String Class provide any type of design

> Pattern?

In an iNterview the interviewr had asked me that u can not initilize a Valueobject.But I know that it is simple java Class with getter and setter method serilaizable Interface Imlepmented.

He said that What is the Point of Initilization when It is Serilized.

rajpuniaa at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

interview question. now I get it.

cannot initialize a value object? why not? what good is a value object without a value?

doesn't have to have setter. in my opinion it's better to NOT have the setter for VO. They're better immutable.

The question tells you as much about the interviewer as he learns from you. Could be that he's not smart enough for you to work with.

%

duffymoa at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

An interviewer might ask a question in a way that implies an incorrect answer. This puts you in a difficult position. You can contradict the interviewer or you can go along with it. If you go along with it, you are either spineless or you don't know anything. If you contradict the interviewer, the interviewer knows you have knowledge and the confidence to use it. If an organizaton is looking for truly effective people, they want the latter.

This may not be the situation you were in but I've learned that if you want to work at a place where your perspective is repsected, you should be assertive in the interview. Otherwise you will only be hired at a place that is looking for a passive person. I'm not saying to be a jerk, just be assertive.

dubwaia at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 8

> An interviewer might ask a question in a way that

> implies an incorrect answer. This puts you in a

> difficult position. You can contradict the

> interviewer or you can go along with it. If you go

> along with it, you are either spineless or you don't

> know anything. If you contradict the interviewer,

> the interviewer knows you have knowledge and the

> confidence to use it. If an organizaton is looking

> for truly effective people, they want the latter.

Errr....what if dafei is the interviewer?

jschella at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 9

>

> In an iNterview the interviewr had asked me that u

> can not initilize a Valueobject.But I know that it is

> simple java Class with getter and setter method

> serilaizable Interface Imlepmented.

> He said that What is the Point of Initilization

> when It is Serilized.

A Value Object as originally defined by the java blueprints are immutable. This means....

1. It has at least one constructor which takes arguments for all the values that the VO contains. It might have others.

2. It does not have setters.

The above is not the case for a Data Transfer Object which is the current blueprints container (VO is no longer used.)

Initialization and serialization have nothing to do with each other. As a guess some one 'clever' decided on this obtuse question because they were trying to get you to answer that the serialization process when it builds an object doesn't use constructors.

jschella at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 10

> >

> > In an iNterview the interviewr had asked me that u

> > can not initilize a Valueobject.But I know that it

> is

> > simple java Class with getter and setter method

> > serilaizable Interface Imlepmented.

> > He said that What is the Point of Initilization

> > when It is Serilized.

>

> A Value Object as originally defined by the java

> blueprints are immutable. This means....

> 1. It has at least one constructor which takes

> arguments for all the values that the VO contains.

> It might have others.

> . It does not have setters.

>

> The above is not the case for a Data Transfer Object

> which is the current blueprints container (VO is no

> longer used.)

>

> Initialization and serialization have nothing to do

> with each other. As a guess some one 'clever'

> decided on this obtuse question because they were

> trying to get you to answer that the serialization

> process when it builds an object doesn't use

> constructors.

Does it mean that It can exist without any constructor.As u said That serilized obeject are not required to be initilized.

rajpuniaa at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 11
> Errr....what if dafei is the interviewer?Been there (well, probably not with daffy).Stand up, pleasantly thank the interviewer for his or her time, and get yourself gone.
kdgregorya at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 12

> Does it mean that It can exist without any

> constructor.As u said That serilized obeject are not

> required to be initilized.

When you restore a serialized object, it is not an intialization, so doesn't matter if you have a constructor, JVM is not going to invoke it. Invoking a constructor always gives a new object, which we don't want here.

Instead JVM calls readObject(ObjectInputStream) OR writeObject(ObjectOutputStream), if you have them implemented. OR it may call readExternal or writeExternal, if you implement Externalizable interface.

Most of the time we don't need to implement these objects, but consider if an object wants to access some resource as soon as its deserailized then you can provide that implementation in the method provided.

Pawan_Shrivastavaa at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 13

> > Does it mean that It can exist without any

> > constructor.

What do you mean by without any constructor? Every class will have a constructor, if you don't provide any, there will be a default one created in the class file. There is no point having a class without any constructor.

So you initialize an Object (call its constructor), set some values, serialize it, deserialize it (this time JVM is not going to invoke constructor).

Pawan_Shrivastavaa at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 14

> > Errr....what if dafei is the interviewer?

>

> Been there (well, probably not with daffy).

>

> Stand up, pleasantly thank the interviewer for his or

> her time, and get yourself gone.

Finally I got the answer

It is Flyweight pattern...

rajpuniaa at 2007-7-14 21:57:04 > top of Java-index,Other Topics,Patterns & OO Design...
# 15
> Invoking> a constructor always gives a new object, which we> don't want here.The constructor itself does not create anything.The new expression creates a new object. The constructor is invoked as part of that expression but it is not itself the
jschella at 2007-7-21 10:20:33 > top of Java-index,Other Topics,Patterns & OO Design...