Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Value objects and encapsulation  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been reading about primitive obsession(online),and value
objects(eric's DDD).I want to understand how they are important with
regard to encapsulation?
In my scenario,the data entry guy is using a web form,to record
air/train fare.So,I have class called TicketFare,in BusinessDomain..

Now,how should I interact with this class,from Presenter class(which
takes values from a form,based on MVP pattern).
What I have done is,create object of each
type(TravelSector,TravelFareAmount etc) which is required by
TicketFare,and then created TicketFare object,and passed all the
values(value and entity objects) in constructor.
public TicketFare(TravelClass tc,TravelFareAmount amount etc)

Is this a good way of doing things?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch!

I have seen this recommended in articles - create a type for everything, avoid primitives, etc - but never tried it. It can surely eliminate a lot of issues, but it looks like an awful lot of classes for the very few that might need to do more than a dumb wrapper over time. Do you have a smallish project you can try it on and see how it works out?

Or was the question about passing all those things in the constructor?

And what's Eric's DDD? ( OK, Google got that one )

More questions than answers!

[ September 07, 2006: Message edited by: Stan James ]
[ September 07, 2006: Message edited by: Stan James ]
 
ravi chaturvedi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stan,
I am trying using value objects on a project I am doing(but not for everything,but where it fits domain).Yes,my question was related to constructor.
In my code,I create all these objects,and then pass it to the constructor(of TicketFare class).Is this a good way?
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can see where you are going with this. Personally I would not use the "m_" warts, but if that's a house style then I guess you have to use it.

As for the types I generally agree with them. The only bit I'm less sure about is:



To me this looks as if it should be some sort of more generic "Money" class (still with currency and amount). A more generic class would be more reusable in other applications. Is there anything which distinguishes a "TravelFareAmount" from a more generic Money object?

Also I'm puzzled by the use of "Dollar" in the variable name. Your class seems capable of representing money in any currency, yet your member field name implies dollars. Should your application work if the fare is specified in Pounds or Euros, or whatever? This is guaranteed to cnfuse anyone trying to maintain the code.

My version of this line would probably be more like:



Does that make sense?
 
ravi chaturvedi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,In my scenario..A single route..can have both dollar as well as euro rate.so I have dollarrate and eurorate.
 
ravi chaturvedi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Personally I would not use the "m_" warts, but if that's a house style then I guess you have to use it



Whats your style,Frank?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,In my scenario..A single route..can have both dollar as well as euro rate.so I have dollarrate and eurorate.

OK. In which case you still have a danegerous duplication of information. It's still possible for a coding or data error to result in a non-dollar money amount assigned to the dollar variable.

It seems you have two choices. One chioce is to remove the currency information from the money object, and rely on the variable name to be correct. Unfortunately, this is still vulnerable to errors (which, presumably is why you didn't want a raw primitive in the first place).

Another (and, IMHO more flexible) choice is to remove the currency information from the variable name, and rely on the value in the object. In this case you could store the alternative rates in an anonymous array, map or list, and when you nedd a particular currency, fetch the object which is tagged with that currency. For example:



In practice, all those value-object classes seem to be a bit lacking in behaviour, so I would be looking very hard at the rest of the system to see if they should have any behaviour rather than just data. But as this is a thread about how to use value objects I'll leave it at that.

Can you see where I am going with this?

In answer to your following question, I have re-coded it in something more like my own current style, which in turn is very like Sun's style.
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by ravi chaturvedi:
Quote: Personally I would not use the "m_" warts, but if that's a house style then I guess you have to use it
Whats your style



The "m_member" style was heavily used during the early days of C++ back in the early nineties; practioners using other languages also adopted that style back then (unless they where already using the now "abandoned" Hungarian Notation by Charles Simonyi) and stuck to it even though a significant portion of the C++ community moved on. Eventually "m_member" was replaced by "_member" which then in more recent years changed to �member_" � so the trailing underscore identifies an instance variable. Many stick with this notation to avoid having to use the "this" pointer to distinguish between an instance variable and a parameter/local variable with the "same" name; i.e. they preferred to simply write "myValue_ = myValue;". It also seeks to avoid the potential confusion that can arise in circumstances when a local variable with the same name shadows an instance variable.

Java never really adopted an instance variable notation and many seem quite content to use "this.myValue = myValue;" when a parameter/local variable of the same name is in scope.

Naming Conventions in C++ and Java :Can They Be Reconciled ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!