1

I have a bunch of classes (a few 10s), that got generated from the .proto files I have. These classes are configuration items. So, let's say I have:

class config_item_1 {
    string Id;
    ...
    ...
}

class config_item_2 {
    string Id;
    ...
    ...
}

I have instances of these classes which represent my system configuration. I'm building logic in my code to keep them in-memory, performing some transformations, and at the end, I need to figure out which objects changed. That is, some objects might stay untouched, some might go away, some might change.

So, for all these classes, I need to add a "status" without changing the actual class definitions. Something like a wrapper.

What is the best way to achieve this?

EDIT:

Another "hack" I'm considering is, since my config item classes are generated classes from proto file, like I mentioned, I'm thinking of creating a wrapper proto, like this:

message ConfigCacheItem {
    enum ItemState {
        UNTOUCHED = 0;
        CREATED = 1;
        UPDATED = 2;
        DELETED = 3;
    }
    ItemState item_state = 1;
    String id = 2; /* ID stashed from one of the items below for lookup */
    oneof config_item {
        ConfigItem1 config_item_1 = 3;
        ConfigItem2 config_item_2 = 4;
        ....
    }
}

3 Answers 3

1

The initial problem is that generated code is almost always the wrong way to go--yet some people insist so let's make the best out of it.

You can extend the class and add functionality. This is probably a bad idea in your case:

class ConfigItem1Wrapper extends config_item_1 {
    public Boolean getStatus()
    {…}
    ...
}

The main problem here is that you will have ~10 of these classes so a lot of duplicated code or forwarding. It's also going to give you problems if whatever system you are using to create these classes also wants to instantiate them... if it does you will get instances of "config_item_1" instead of the more awesomely named "ConfigItem1Wrapper"

You can encapsulate the class in another class as a delegate. This isn't as bad as it seems. Some editors can automatically create classes like this. Each method is re-created and the call is forwarded to the copy you store internally.

class ConfigItem1Wrapper {
    config_item_1 delegate;

    String some_method1(var) {
        return delegate.some_method1(var);
    }
}

If you can use delegation like this (Hopefully using some IDEs code generation) it might be the best way, it gives you complete control.

Were you to use Groovy (A superset of Java) you'd find it has an @Delegate annotation that does this all for you. Just this:

class ConfigItem1Wrapper {
    @Delegate config_item_1 delegate;

    public Boolean getStatus() {
        return status;
    }
}

would generate all the methods from config_item_1 onto ConfigItem1Wrapper with your additional code. I'm guessing that won't work for you. Pity.

Honestly nearly any other solution I could give you is some type of delegation. I recommend you just find some way to make delegation work (without having to forward each method manually)

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

Comments

0

Straight forward way is to create wrapper class and extend/ override setter and use them for your status tracking

Comments

0

I believe that you need a Decorator Pattern. Something like the example bellow.

  public class Decorator_config_item_1 {
    private config_item_1 item_1;
    private Boolean status;

    public Decorator_config_item_1(config_item_1 item_1) {
        this.config_item_1 = item_1;
    }

    public Boolean getStatus() {
        return status;
    }

    public void setStatus(Boolean status) {
        this.status = status;
    }

}

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.