Design to request bucket information on customer

I am trying to find the best way to create a "bucket" client store. To explain, the server sends information like:

{
    'buckets': {
        'some_cool_bucket' : 'val1',
        'another_bucket' : 'name'
    }
}

      

If values ​​can be

'some_cool_bucket' : 'val1' |  'val2' | 'val3'
'another_bucket' : 'name' | 'cool' | 'other'

      

basically all of these buckets are enumerations with all possible values ​​known to both the client and the server, although there are no general restrictions, for example, buckets can have any number of possible values ​​(no size limit) and any naming scheme.

I am struggling against Java to find a design that I am happy with. Forget parsing JSON, blah blah blah, from a design standpoint I want to know that I can store this data that meets these requirements:

  • Each bucket should be able to keep the default if the server doesn't send one snapshot
  • bucket.isBucket (...) must be type safe, i.e. if we use an enum here, you won't be able to go into a bucket that doesn't belong without getting an error.
  • Simple, easy access. Buckets.some_cool_bucket.is (val1) would be perfect.
  • Minimum boiler plate when adding a new ladle
  • Don't confuse the design

Ignoring these requirements, we can implement this as follows:

enum Bucket {
    some_cool_bucket('val1'),
    another_bucket('name');

    Bucket(String default) {
        [...]
    }

    // Assume this retrieves the stored value sent down from the server.
    String getVal() {
        [...]
    }

    boolean is(String val) {
        return getVal().equals(val);
    }
}

      

Using Bucket.some_cool_bucket.is('val1')

. Naturally, we would like to expand on this by changing the is () type signature to accept some val enum defined by some_cool_bucket. Since the values ​​that a bucket can take are not uniform, we need to define this inside the some_cool_bucket enum. But you cannot have enums inside enums in Java.

Ok, reset. Try again:

public abstract class BaseBucket<E extends Enum<E>> {

    private final Class<E> mClazz;
    private final E mDefaultBucket;

    public BaseBucket(Class<E> clazz, E defaultBucket) {
        mClazz = clazz;
        mDefaultBucket = defaultBucket;
    }

    // Assume this retrieves the stored value sent down from the server, uses
    // getName() to match with the data.
    protected E getVal() {
        [...]
    }

    protected abstract String getName();

    public boolean is(E test) {
        return getVal() == test;
    }
}

public class SomeCoolBucket extends BaseBucket<Val> {
    public SomeCoolBucket() {
        super(Val.class, Val.val1);
    }

    @Override
    public String getName() {
        return "some_cool_bucket";
    }

    public enum Val {
        val1, val2, val3;
    }
}

public Buckets {
    public static final SomeCoolBucket some_cool_bucket = new SomeCoolBucket();
}

      

Ok, it works! Fine. It meets all the functional requirements, but it is cumbersome and I don't like creating a new class for each bucket. I find it hard to justify all the code, although I find the requirements sound.

Does anyone have a better solution for this?

+3


source to share


1 answer


What you can do in this case is to have an abstract BaseBucket class, but remove the generics bits. The base class can have an instance variable of type Enum. To do this without generics, you can use the Strategy design pattern . Using this, you can force all Enum types to implement a common interface. For example, you can call this IBucketEnum. Thus, your BaseBucket class will have an instance variable of type IBucketEnum and will look something like this:

public abstract class BaseBucket{
    private IBucketEnum enum;
    //other instance vars

    public String getName(){
        enum.getName();
    }
}

      



From there, you can have different versions of BaseBucket, and they will have implementations and instance variables inherited from the base one, but can be extended with additional things you need. Hope this helps.

0


source







All Articles