| andrewmcdonagh 2007-02-07, 6:55 pm |
| On Feb 6, 2:45 am, LX-i <lxi0...@netscape.net> wrote:
snippetty snip
>
> Ah... I shied away from that because I thought, "I don't need a
> key-value pair - just the value." Would it still be acceptably
> efficient to do the Add()s off the array defined in the constructor? I
> could convert it to hard-coded Add()s, that would be easy enough.
Yes, given that the contents are only loaded once, it might be worth
doing purely from the 'readability point of view.
One thing though.... I would have expected an 'addAll(Object[]) '
method like the Java collections have, so to save you from having to
code it.
>From a Design point of view, this population loop is a Responsability
that does not below with the KeyWordDictionary and so would be best
moved into its own class, with a choice of designs being:
1) Using Inheritence -
class ExtendedDictionary : Dictionary
{
public void addAll(....)
{
foreach (entry)
add( entry, entry);
}
}
2 Use Delegation
class DictionaryFactory
{
public Dicitionary createFrom(Object[] elements)
{
Dicitionary dictionary = new Dictionary();
for (int i = 0; i < keywords.Length; i++)
{
dictionaryAdd(keywords[i], keywords[i]);
}
}
}
}
>
> Also, regarding the above - would there be anything wrong with making
> isKeyword() a static method as well? Then, you wouldn't actually have
> to create an object - you could just say something like
>
> if (FastCobolKeywordDictionary.isKeyword(words[i])) {
> // do something really 
>
> }
>
Wrong... such a subjective term....
So, as I see it (YMMV) at the very highest level of our design this is
a Procedural approach and we are aiming for an OO one.
With the procedural approach, I can't use polymorphism to determine
which keyworddictionary to use, whereas I can with the OO approach.
Procedural example...
public void makeAnimalSound(Animal animal)
{
if(animal.getType() == Dog)
{
DogNoiseGenerator.bark();
}
else if (animal.,getType() == Cat)
{
CatNoiseGenerator.meeow();
}
}
OO Example....(where the noisegenerators are used in a polymorphic
way)
public void makeAnimalSound(Animal animal)
{
AnimalNoiseGenerator noiseGenerator = dictionary.get
(animal.getType() );
noiseGenerator.makeNoise();
}
> I did that today with the NormalizeSpace method - I created a class
> called CSCSFuncs (as plain Funcs was already taken), and made
> NormalizeSpace() a class method.
>
Personally I always favour Instance Methods over (static) Class
Methods because of this 'subsitutability' benefit. There is nothing to
be gained from worrying about the memory or cpu usage from creating
one of this objects vs calling a static method.
Andrew
|