Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Static/Constant' business objects

Tags:

oop

I don't quite know how to ask this question, so I'll phrase it as an example instead:

Imagine in an application you have a Country object. There are two properties of this object: Name, and a 'Bordering Countries' collection. More properties might be added later, but it will be the kind of information that would change very rarely (e.g. changes of country names/borders)

Lets say this application needs to know about all of the countries in the world. Where would you store these object's state? How would you new them up? It seems silly to store all this state in the DB, since it won't change very often.

One option might be to have an abstract 'country' base object, and have a class for each country inheriting from this with the details of each country. But this doesn't seem quite right to me.

What is the proper way of dealing with these kinds of objects?

UPDATES:

Someone asked about language: C#

Also, I'm coming at this from a web application perspective, so there wouldn't be multiple client installations where I'd have to worry about updating hard coded values.

Most people have suggested not hardcoding the data, but using the DB or XML files to store the data. Could anyone provide an example of how this kind of object would be 'newed up' (from e.g. an XML file)? Would you use some kind of helper or factory method to obtain instance of a particular country?

like image 811
UpTheCreek Avatar asked May 25 '10 08:05

UpTheCreek


3 Answers

Definatly in the DB. Load them up once (refresh periodically) and use them from there. I say definatly DB, as you will most likely be extracting data for reporting purposes, and if structured correctly, you can reuse the country data for other applications too.

like image 186
MaLio Avatar answered Oct 23 '22 06:10

MaLio


enumerations are quite useful for that kind of thing. you don't mention the language you're using, but in java, they're stored quite efficiently as an ordinal index (integer) and you can always add new values to the end of the enumeration list.

like image 44
chris Avatar answered Oct 23 '22 06:10

chris


I don't think that create a class hierachy would be a good design for your problem. Instead I'd store the values at the database and have a generic Country class what retrieve the country state from database.

like image 1
CARLOS LOTH Avatar answered Oct 23 '22 07:10

CARLOS LOTH