简体   繁体   中英

Suitable design using Generics with Wildcard

I am trying to determine if generics would be able to help me with designing a better and scalable solution. In my application, there is a model class which is responsible for loading data from data sources and I use a ModelProxy class to expose some of the methods in the Model class.

public interface ModelProxy {
     public int getOrderCount();
     public int getCustomerCount();
}

public abstract class AbstractModel {
     public abstract ModelProxy loadData(Configuration configuration);
}

public class ConcreteModel extends AbstractModel {
    public ModelProxy loadData(Configuration configuration) {
         loadInternal();
         return new ConcereteModelProxy(this);
    }
}

Everything looks good so far, but I am looking to see if generics (with wildcards) can help me design a better solution that would allow to be extend the ModelProxy interface or the Configuration class. For example, in another Concrete Model class I woukd like to use a ExtendedConfiguration class and ExtendedModelProxy.

public ExtendedModelProxy extends ModelProxy {
   // Additional methods
   public int getTotalCount();
}

public class ConcereteModel2 extends AbstractModel {
   public ExtendedModelProxy loadDate(ExtendedConfiguration configuration) {
      return new ConcreteExtendedModelProxy(this);
   }
}

Will Java Generics help me to achieve something like above? Or Maybe my design is flawed that I need to re-design it. Any suggestions would be very helpful.

Thanks,

Example Client Code:

public abstract class Service {
   public ModelProxy load(Configuration configuration) {
       return getModel().loadData(configuration);
   }

   protected abstract AbstractModel getModel();
}

public class ServiceImpl extends Service {
   protected AbstractModel getModel() {
      return new ConcreteModel();
   }

   public static void main() {
      Service service = new ServiceImpl();
      ModelProxy proxy = service.load(configuration);
      System.out.println(proxy.getOrderCount());
   }
}

public class ExtendedServiceImpl extends Service {
   protected AbstractModel getModel() {
      return new ConcreteModel2();
   }

   public static void main() {
      Service service = new ExtendedServiceImpl();
      ExtendedModelProxy proxy = (ExtendedModelProxy) service.load(configuration);
      System.out.println(proxy.getTotalCount());
   }
}

I hope to not have confused with too much. In the ExtendedServiceImpl, you can see I need to cast ModelProxy to ExtendedModelProxy to be able to access the method getTotalCount. My thinking was maybe I can use generics to avoid the casts. Something like

public abstract <M extends ModelProxy, C extends Configuration> M loadData(C configuration);

Maybe I am overcomplicating things and really my current design is all I need. Not sure...

How about this kind of thing

package jj;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.*;

interface Configuration {
}

interface Model {
}

interface OrderModel extends Model {
    public int getOrderCount();
    public int getCustomerCount();
}

interface CustomerModel extends Model {
    public int getName();
    public int getAddress();
}

abstract class AbstractModel<M extends Model> {
    @SuppressWarnings("unchecked")
    public M loadData(Configuration configuration) {
        // connect to stuff
        Object connection = null;
        loadInternal(configuration, connection);
        // do some other stuff
        return (M) Proxy.newProxyInstance(null, new Class<?>[]{getModelClass()}, null);
    }

    protected abstract void loadInternal(Configuration configuration,
            Object connection);

    protected abstract InvocationHandler getInvocationHandler(Object connection);
    protected abstract Class<M> getModelClass();
}

class ConcreteOrderModel extends AbstractModel<OrderModel> {
    public void loadInternal(Configuration configuration,
            Object connection) {
    }

    protected InvocationHandler getInvocationHandler(Object connection) {
        return null;
    }

    protected Class<OrderModel> getModelClass() {
        return OrderModel.class;
    }
}

class ConcreteCustomerModel extends AbstractModel<CustomerModel> {
    public void loadInternal(Configuration configuration,
            Object connection) {
    }

    protected InvocationHandler getInvocationHandler(Object connection) {
        return null;
    }

    protected Class<CustomerModel> getModelClass() {
        return CustomerModel.class;
    }
}

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