1

I have the following question.

Assume we have third party software that send us transaction (atm cards) information in following style:

atm card 4**5048 debited 42$. 231$ Left.

So, I have debited sum, sum left and card no in each transaction.

So I create class

class Transaction {
    private String mCardNo;
    private Double mAmount; // Actually. I dont store money as double, but let it be:)
    private Double mSumLeft;
}

One day second software appears and starts to send the info:

atm card 4**5048 debited 42$: purchase at Apple Store. 231$ Left.

And I think that its awesome to have info about place, where user made a purchase. There are two options: extend Transaction class or add a new attribute "place".

One day new software is added and now I have to support 3 types of message!

atm card Visa Classic 4**5048 debited 42$: purchase at Apple Store. 231$ Left.

Oh, God! and Im pretty sure that it will be more than 100 types of messages containing unique numbers of attributes (because I have about 50 now!)

So what is the best way to store additional attributes?

6
  • None of the properties of Transaction are in fact attributes, they're properties. Other than that, you might consider a more loosely typed approach, where types can be extended without the class being extended per se by means of a 'property bag'-like implementation. Commented Nov 2, 2012 at 15:58
  • @GrantThomas that's not entirely correct. It depends on the programming language, in Java attributes and properties are synonyms. Commented Nov 2, 2012 at 16:03
  • @ÓscarLópez To be honest, don't even know how I wound up amongst language agnostic tags. My bias to .NET and C# surely show. Commented Nov 2, 2012 at 16:07
  • @GrantThomas I can't be 100% sure (there's too little code in the question), but my bet is that the language used by the OP is Java - because of the capital "S" in the String data type Commented Nov 2, 2012 at 16:13
  • @ÓscarLópez Well at least the capital "D" of Double indicates that it might not be C# - unless they're into avoiding language-specific aliases for 'native' types. Still a possibility, not that it matters that much. Commented Nov 2, 2012 at 16:15

3 Answers 3

1

If there's so much variability in the number of attributes, maybe you should store them in a single class (to avoid an explosion of subclasses) as values in a series of Map attributes and identify them with a String key (say, Map<String, Double>, Map<String, Integer>, etc.)

Sign up to request clarification or add additional context in comments.

Comments

0

You can go several routes with this.

  1. Keep your transaction class as is and use it as a base class, inherit from it and build new classes from it for each message type.
  2. Add all the properties that you will need to the one trasaction class - you don't have to populate all of the properties when you use it.

I have personally used the second approach more often, it's easier to keep track of.

Comments

0

It depends on many other factors, such as how do you store data, is data being normalized or not make any difference or not, will there by millions of transactions or just hundreds, etc.

But, judging only by what you have provided, if you have tens of variations of transaction, then it is probably not practical to have separate class for each type. A generic Transaction class is a better approach. But, choosing one of the approaches is only half the work. The other half is, no matter which one you chose, to make sure that you design your class(s) properly right from the beginning. This requires that you spend some time, before doing actual design, to study transactions and various types that you have, or might get in the future. After having a comprehensive of the problem (business domain) itself, you will be able to devise a much better solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.