0

i'm currently working on a project and something came up on the design. I have a class named Key which is composed of several Fields. This Field class it's a mother class and their sons like Age, Name, etc implement Field. Inside the Key class there's an attribute which is an array of Fields, to hold different kinds of Fields.

class Key {
private:
Field * fieldList;

}

I'm working on a team and a design choice came up that i couldn't defend cause i didn't knew how to answer to the following problem... or maybe the lack of it? I trust that you'll be able to open my mind on this.

The purpose of this Key class is to hold several fields. The existence of this class is because i'm going to handle data of this kind.

(Name, Age....)

This is how i thought it would look already implemented:

Key myKey = Key();
Age newAge = Age(50);
myKey.add(newAge);

This is what the prototype of the add method of the Key class would look like:

void Key::add(Field);

As you may have assumed, since the Key class has an array of Field's this method receives a Field and since Age is also Field, cause of inheritance, then this works like a charm. Same can be said of the Name class and other classes that could come up in the future.

This is the same idea as in a database where you have rows with data and the columns belong to the attributes, so a same column has the same type of attribute.

We would also like to compare 2 Key's only by one of the Fields, for example:

Let's say i have 2 Key's with this data:

(John, 50) <- myKey1

(Paul, 60) <- myKey2

My method to do this would look like this:

myKey1.compareTo(myKey2, 2)

This would answer if the 2nd attribute of the first myKey1 is bigger, equal or less than the one on the second myKey2.

There's a problem with this. When i used the add method, i randomly added Field's of different types, say Age first, then Name second, etc to the Key object. My Key object added them to it's internal array by order of appearance. So when i use the compareTo method, nothing is assuring me that inside both objects, the 2nd elements of their arrays will have a Field say, the Name Field, and therefore if that were not to be true, it could be comparing a Name with Age, cause inside it only holds an array of Field's, that are equal type as long the Key class knows.

This was my approach to my solution, but what i couldn't answer is, how to fix this problem.

Another member of my team proposed, that we implement a method for the key class for each of the existing fields, that is:

myKey.addAge(newAge);
myKey.addName(newName);

Inside it would still have the Field array but this time, the class can assure you that Age will go in the 1st place of the array, and that Name would go in the 2nd position of the array, cause each method would make sure of it.

The obvious problem with this, is that i would have to add a method for each type of Field that exists. That means that if in the future i wish to add say "born date" and so creating the new Date class, i'll have to add a method addDate, and so on and so on... Another reason my team member gave me is that, "we can't trust an exterior user that he will add the Fields the way they're supposed to be ordered" when pointing why my approach was bad.

So to conclude:

  • On the first approach, the Key class depends on the programmer that added Fields, to make sure they have the order they should, but as a benefit no need to add a method for each type of field.

  • On the second approach, the Key class makes sure the order is the right one, by implementing a method for each type Field that exists, but then, by each type of new Field created, the class would grow bigger and bigger.

Any ideas with this? is there a workaround for this?

Thanks in advance, and i apologize if i wasn't clear with it, i'll add new details if needed.

4

1 回答 1

0

扩展 @tp1 的 Field 类中的 ID 字段和枚举的绝妙想法,您实际上可以使其非常灵活。如果您愿意将字段类型的数量限制为 32,您甚至可以将一组标志作为CompareTo. 然后您可以同时比较多个字段。这种方法有意义吗?

于 2012-05-26T02:56:44.047 回答