+2  A: 

I don't know why you think that the OO solution would involve a class for each number pattern. My OO solution would be to use a regular expression class. And if I was being procedural, I would probably use the standard library strtod() function.

anon
Sorry, I was just asking a rhetorical question using the number validator as an example. (I know that there are standard functions to convert real numbers!) I have edited my question to make just it clearer.
Andy
+2  A: 

You're asking for a parser, use one:

Also: http://en.wikipedia.org/wiki/Parser_generator

Now how do I handle complexity for this kind of problems ? Well if I can, I reformulate.

In your case, using a parser generator (or regular expression) is using a DSL (Domain Specific Language), that is a language more suited to the problem you're dealing with.

Design pattern and OOP are useful, but definitely not the best solution to each and every problem.

bltxd
Sorry, I was just asking a rhetorical question using the number validator as an example. (I know that there are standard functions to convert real numbers!) I have edited my question to make just it clearer.
Andy
A: 

Sorry but since i use vb, what i do is a base function then i combine a evaluator function so ill fake code it out the way i have done it

function getrealnumber(number as int){ return  getrealnumber(number.tostring) }
function getrealnumber(number as float){ return  getrealnumber(number.tostring) }
function getrealnumber(number as double){ return  getrealnumber(number.tostring) }
function getrealnumber(number as string){
if ishex(){ return evaluation()}
   if issigned(){ return evaluation()}
   if isdecimal(){ return evaluation()}
 }

and so forth up to you to figure out how to do binary and octal

Jim
+4  A: 

In OO design, you would normally allocate a class for each number format (e.g. in this case, we have 8 classes), and each class would have a separate validation function.

No no no no no. At most, you'd have a type for representing Numeric Input (in case String doesn't make it); another one for Real Number (in most languages you'd pick a built-in type, but anyway); and a Parser class, which has the knowledge to take a Numeric Input and transform it into a Real Number.

To be more general, one difference of behaviour in and by itself doesn't automatically map to one class. It can just be a property inside a class. Most importantly, behaviours should be treated orthogonally.

If (imagining that you write your own parser) you may have a sign or not, a decimal point or not, and hex or not, you have three independent sources of complexity and it would be ok to find three pieces of code, somewhere, that treat one of these issues each; but it would not be ok to find, anywhere, 2^3 = 8 different pieces of code that treat the different combinations in an explicit way.

Imagine that add a new choice: suddenly, you remember that numbers might have an "e" (such as 2.34e10) and want to be able to support that. With the orthogonal strategy, you'll have one more independent source of complexity, the fourth one. With your strategy, the 8 cases would suddenly become 16! Clearly a no-no.

Daniel Daranas
Thank you. So are you saying that you agree with the "procedural approach" (using 3 if...else statements to control the behaviour, assuming that they do not get intertwined)? I agree that using the class approach will soon become a nightmare as the number of independent sources of complexity increases.
Andy
(1) At some point there is a choice (for each different source of complexity). This choice may be done, of course, with an "if" statement; but yes, keep them independent. It's not that "if" is procedural and not OO - Eiffel, one of the purest object oriented languages, has an "if" statement :) Other options are, of course, a switch (which is a like powerful "if") and, _when_ the choice is essential to the identity of an object, polymorphism via inheritance. Also, in some cases templates (genericity) may serve as a choice, e.g. you may have a std::vector<double> or a std::vector<HexNumber>.
Daniel Daranas
(2) Also, of the things you mentioned, the fact that the input is in Hex would be the most relevant for me. Will you store it in Hex or in decimal? How will you recognize that it is Hex? (Noone can tell if "19" is actually Hex or not; you have to know beforehand). Consider that and take it into account.
Daniel Daranas
A: 

You don't kill a fly with a hammer.

I realy feel like using a Object-Oriented solution for your problem is an EXTREME overkill. Just because you can design Object-Oriented solution , doesn't mean you have to force such one to every problem you have.

From my experience , almost every time there is a difficulty in finding an OOD solution to a problem , It probably mean that OOD is not appropiate. OOD is just a tool , its not god itself. It should be used to solve large scale problems , and not problems such one you presented.

So to give you an actual answer (as someone mentioned above) : use regular expression , Every solution beyond that is just an overkill.

If you insist using an OOD solution.... Well , since all formats you presented are orthogonal to each other , I dont see any need to create a class for every possible combination. I would create a class for each format and pass my input through each , in that case the complexity will grow linearly.

but when you do kill a fly with a hammer it feels good!!! ;)
kenny
Thank you for your input. I agree with what you say but please note that the example I gave is - just an example. My problem is a different one which cannot be solved by regex.
Andy