Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Architecture question

Name: Anonymous 2009-05-02 17:38

Hello /prog/

I have developed a noteworthy Java program with over 20,000 lines of code. In the first version I threw things together the best I could think of at the time. Now that I've learned much about how it should be designed, I am refactoring it into something cleaner. However I am stuck with a situation that I can't figure out, and no amount of OOP design patterns seem to result in a good design.

I am reading several different types of tokens from a stream. Depending on the token, a different polymorphic object is created, but they all share the same superclass. These instantiated objects are then sent on their way for various purposes. There are two types of tokens I am struggling with in particular: I will call them token X and token Y. These two tokens are similar in many ways, for instance they both can be converted into binary data of a particular type. However, while it only takes 1 X token to create the data, it takes 2 Y tokens to create similar data. Additionally, the way they are converted into the end data differs somewhat, but they both require maintaining a state depending on what tokens came before.

So I have an X class, a Y class, a converter class for X objects, and a converter class for Y objects.

Since the converters have to persist and maintain state (depending on what came before), I can't really put them inside the X and Y classes because those will come and go. I could give the X and Y objects the converter class to briefly use and convert themselves to the binary data, but then the X and Y objects have to check that they are getting the correct converter class. On the flip side, I could pass the X and Y objects to the converters, but then the converters have to make sure they're getting the correct object type. All the while, the binary data will be produced immediately upon an X conversion, but will only happen on every other Y conversion (since it takes 2 Y objects to create the data).

So either way I'm regularly checking that these objects and converters are compatible, but there's got to be a better way. Any suggestions /prog/?

Name: Anonymous 2009-05-03 14:47

>>1
So the problem is given a Token (which may be X or Y), and a sink object (which knows about an XConverter and a YConverter), how to match them up, and you don't want to do

if (token instanceof X) XConverter(token);
else if (token instanceof Y) YConverter(token);
...


If there is only 1 instance of each converter (or the destination converter is known at token construction time) and the converter type share some common subtype (interface, probably), then Token can expose a BaseConverter getConverter() which returns a reference to the appropriate converter instance (as set at construction time).

Other than that, polymorphism isn't really going to help you.

Name: Anonymous 2009-05-05 14:37

>>1
I don't understand what your problem is. So you read a stream in which different types of data can be uniquely identified, i.e. you know that an X data blob is in there next as opposed to a Y one. And then based on that detection you just pass it off to the relevant converter, which does something to it based on the data.

Where is the actual problem here?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List