2

I am thinking what will be the best pattern (if possible from the gang of four book) to implement the following validation scenario:

Let say there is an object that is call TLV (Type Length Value). That looks like that:

public class TLV implements Serializable, TLVInterface {

    private Type type;
    private Length length;
    private Value value;   // the buffer may include sub-TLVs
…
getters/setters + other methods
…
}   

This object can include sub objects of the same type i.e. sub-TLV. I.e. the Value field has ByteBuffer that may include sub-TLVs.
Now what needs to be done, is to make sure that only particular sub-TLVs could be a part of an existent TLV. i.e. a check need to be implemented that the main object type can include only particular subtypes i.e. sub-TLVs.

The TLV object is created using the Builder pattern using fluid interface. After that the idea was to use the Composite pattern in order to build something like a tree i.e. tlv within tlv. However that still leave the question how to best validate such structure and if that is the optimal design for it.

This question is more about how to implement proper validation techniques. I was thinking about to create statistical association for example -> where each “Type” (Integer) number has the subType (Integer) number statically typed in a java structure such as List . Then based on a quick search of that List it will be possible to find if the dissected sub-TLVs types match the one that are in the statically pre-typed List. This will work but I was curios if there is a better/smarter validation design pattern / techniques that I can rely on when using TLV's and Java. Maybe there is a pattern that others already used exactly for that purpose.

Another way that I first thought it could work was to rely on Java Interfaces and use them in order to tag the classes as some sort of a tag, and then iterate over the sub TLV's types build them and find out if they implement particular interface. Now this would mean that I would need to manually create at least one class representing each different TLV type that I would support. That is not feasible in my case since those could be many thousands, and to code that will be a nightmare.

Any ideas/ techniques are greatly appreciated.

Tito
  • 2,234
  • 6
  • 31
  • 65
  • Design Patterns are not something you sprinkle over your code like sugar to make it "nicer". If you do not know what design pattern to use, either no pattern is applicable or you do not understand the patterns well enough. – Raedwald Mar 21 '16 at 09:36
  • well i do agree with the part about sprinkling the sugar, however a comment like "If you do not know what design pattern to use, either no pattern is applicable or you do not understand the patterns well enough" is a flame such as the sky is blue and the the moon is white. If you do not have anything to add to this question, sometimes is best to be silent. If you have something to add to that matter in a more constructive way such as in my experience ... , i have done it that way ... etc. then be my guest, but if you try to show how well you pointed that the sky is blue then "no comment". – Tito Mar 21 '16 at 13:22
  • "If you do not have anything to add to this question" my point is, this question is therefore a bad question for this site, because it would amount to a tutorial about design patterns. – Raedwald Mar 21 '16 at 13:41
  • "brilliant" response, now the sun is hot. I do not wish to continue this discussion. – Tito Mar 21 '16 at 14:39

0 Answers0