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

Pages: 1-

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-02 17:45

noteworthy
Java

Name: Anonymous 2009-05-02 17:55

I have now read you're post twice and I am starting to get a grasp of the problem at hand. My suggestion is: your fucked. I am glad I can use The C Programming Language, void pointers and, in general, fuck the abstractions.

Name: Anonymous 2009-05-02 18:02

Use a factory

Name: Anonymous 2009-05-02 18:08

>>4
You helped him!

Name: Anonymous 2009-05-02 18:12

>>1
Shit about design patterns
Obvious first-year problem

This troll is so obvious it apparently took five posts for someone to notice. What's up, /prog/?

Name: Anonymous 2009-05-02 18:16

>>6
This troll is so obvious it apparently took five posts for someone to notice. What's up, /prog/?
This troll is so obvious it apparently took one posts for someone to notice. What's up, /prog/?

Name: Anonymous 2009-05-02 18:17

>>6
see
>>2 dude.

Name: Anonymous 2009-05-02 20:03

>>2
Srsly. The vast majority of the internets would never use such a shitty language to do something as important as query their databases.

Name: Anonymous 2009-05-02 20:10

>>3
What about my fucked?

Name: Anonymous 2009-05-02 20:16

>>10
Its fucked.

Name: Anonymous 2009-05-02 20:17

>>1
It sounds like you've overengineered your solution. You might find it easier if you removed some superfluous abstraction layers.

Name: Anonymous 2009-05-02 20:22

>>1
I don't understand what's your problem. I never used Java, but wouldn't the type system take care of checking if the functions are passed the correct data type?

Name: Anonymous 2009-05-02 20:26

>>13
I should proofread my posts before clicking Reply.
s/your/you're/

Name: Anonymous 2009-05-02 20:38

>>11
What about my fucked's fucked?

Name: Anonymous 2009-05-02 21:40

Too many classes.

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?

Name: Anonymous 2009-05-05 18:04

>>17,18
Same person.

Name: Anonymous 2009-05-05 21:59

>>17
Thank you very much for another perspective. I wasn't sure if I was missing some polymorphism concept, or if there really is no good OOP design pattern for the situation.
>>18
It's true that the token identifier already does the work to figure out the token type, so it could technically just send it to the necessary converter right there. Unfortunately the program design doesn't really lend itself to having the token identifier being aware of what converters are currently instantiated. For example, depending on user preferences, the converting might all be done in another thread.

Name: Anonymous 2009-05-05 22:44

>>20
good
design pattern
YHBTE

Name: OP 2009-06-10 2:51

>>21
It seems the Visitor pattern is actually exactly what is needed in this case. However I don't really care for its problems.

Name: Anonymous 2010-12-17 1:22

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

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