简体   繁体   中英

Java design pattern / transformation pattern

I have the below use case where I get events containing JsonString1, I have have to do some processing/transformation to get to Object1 through Object 4. As of now I only have one such case and its likely that in future there there might more such hierarchies (atmost 2-3).

I am unable to decide on what would an elegant way to code this.

        JsonString1
            |
        JsonString2
          /   \
JsonString3  JsonString4
    |            |
 Object1      Object2
                 |
              Object3

I could just have an Abstract class for processing JsonStrings 1 to 4 and concrete implementation for each type of the event. Something like

public abstract class AbstractEventProcessor {
    public AbstractEventProcessor(String jsonString1) {
         // Do processing to get JsonString2, JsonString3 and JsonString4
    }
}

public class Event1Processor extends AbstractEventProcessor {
    Event1Processor(String str) {
         super(str);
    }
    Object1 getObject1() { 
    }
    Object2 getObject2() { 
    }
    Object3 getObject3() { 
    }
}

And similar implementations more events as they come along.

Is there a better way to do this ?

Also for now two things are constant, but in a rare case might change.

  1. All events will have JsonString1 .. JsonString4 but the number of Objects at the end will vary. But in future this might change.
  2. Although its very unlikely (but not impossible) that the format of the strings might change (say from json to xml)

Do I accomodate for such changes as well by providing interfaces for string transformations, or it this an overkill ?

Usually I am stuck at such places where I am trying to figure out the most elegant way to do this and end up spending a lot of time ? Is there any general advice as well for this ? :)

Thanks

It's not very clear what you exactly want. However, even without it, when I see your hierarchy, it smells. Usually , during my code reviews, whenever I see too fancy hierarchy like yours, there is something wrong in the design.

Try considering using decorators to avoid the inheritance hell. Thus you may create any combinations you may need in the near and far future. Get some inspiration in the standard java class java.io.Reader and its subclasses.

For your case it would mean something like this (at least how I understand your description):

public interface EventProcessor {
    public BaseObject processJsonString(String jsonString);
}

public abstract class AbstractEventProcessor implements EventProcessor {
    final private EventProcessor processor;
    public AbstractEventProcessor(EventProcessor processor) {
        this.processor = processor;
    }
}

public class SpecialObject1 extends/implements BaseObject { ... }
public class SpecialObject2 extends/implements BaseObject { ... }
public class SpecialObject3 extends/implements BaseObject { ... }

// Each your future processor will look like this
public class Event1Processor extends AbstractEventProcessor implements EventProcessor {
    public Event1Processor(EventProcessor processor) {
        super(processor);
    }
    public SpecialObject1 processJsonString(String jsonString) {
        final SpecialObject1 result = (SpecialObject1) super.processJsonString(jsonString);
        // here you add this Event processor specific stuff
        ...
        return result;
    }
    // Maybe more methods here
}

public class Client {
    public void useEventProcessor() {
        final EventProcessor processor1 = new Event1Processor(new Event2Processor(new Event3Processor(null)));
        final SpecialObjectX object1 = processor.processJsonString(jsonString);

        final EventProcessor processor2 = new Event51Processor(new Event20Processor(new Event2Processor(null)));
        final SpecialObjectY object2 = processor2.processJsonString(jsonString);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM