Java 使用带有通配符的泛型进行合适的设计
我试图确定泛型是否能够帮助我设计更好、可扩展的解决方案。在我的应用程序中,有一个模型类负责从数据源加载数据,我使用ModelProxy类公开模型类中的一些方法Java 使用带有通配符的泛型进行合适的设计,java,generics,polymorphism,java-6,Java,Generics,Polymorphism,Java 6,我试图确定泛型是否能够帮助我设计更好、可扩展的解决方案。在我的应用程序中,有一个模型类负责从数据源加载数据,我使用ModelProxy类公开模型类中的一些方法 public interface ModelProxy { public int getOrderCount(); public int getCustomerCount(); } public abstract class AbstractModel { public abstract ModelProx
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);
}
}
到目前为止,一切看起来都很好,但我想看看泛型(带有通配符)是否可以帮助我设计更好的解决方案,从而允许扩展ModelProxy接口或配置类。例如,在另一个具体的模型类中,我想使用ExtendedConfiguration类和ExtendedModelProxy
public ExtendedModelProxy extends ModelProxy {
// Additional methods
public int getTotalCount();
}
public class ConcereteModel2 extends AbstractModel {
public ExtendedModelProxy loadDate(ExtendedConfiguration configuration) {
return new ConcreteExtendedModelProxy(this);
}
}
Java泛型能否帮助我实现上述目标?
或者我的设计有缺陷,需要重新设计。任何建议都会很有帮助
谢谢
客户机代码示例:
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());
}
}
我希望没有太多的混淆。在ExtendedServiceImpl中,您可以看到我需要将ModelProxy强制转换为ExtendedModelProxy,以便能够访问方法getTotalCount。我的想法是也许我可以使用泛型来避免类型转换。差不多
public abstract <M extends ModelProxy, C extends Configuration> M loadData(C configuration);
公共抽象M loadData(C配置);
也许我把事情复杂化了,我现在的设计就是我所需要的。不确定…这种东西怎么样
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;
}
}
jj包;
导入java.lang.reflect.InvocationHandler;
导入java.lang.reflect.Proxy;
导入java.util.*;
接口配置{
}
界面模型{
}
接口OrderModel扩展模型{
public int getOrderCount();
public int getCustomerCount();
}
接口CustomerModel扩展模型{
public int getName();
public int getAddress();
}
抽象类抽象模型{
@抑制警告(“未选中”)
公共M loadData(配置){
//连接到内容
对象连接=null;
loadInternal(配置、连接);
//做些别的事情
return(M)Proxy.newProxyInstance(null,新类[]{getModelClass()},null);
}
受保护的抽象void loadInternal(配置,
对象连接);
受保护的抽象调用处理程序getInvocationHandler(对象连接);
受保护的抽象类getModelClass();
}
类ConcreteOrderModel扩展了AbstractModel{
public void loadInternal(配置,
对象连接){
}
受保护的调用处理程序getInvocationHandler(对象连接){
返回null;
}
受保护的类getModelClass(){
returnordermodel.class;
}
}
类ConcreteCustomerModel扩展了AbstractModel{
public void loadInternal(配置,
对象连接){
}
受保护的调用处理程序getInvocationHandler(对象连接){
返回null;
}
受保护的类getModelClass(){
返回CustomerModel.class;
}
}
我不清楚如何以及为什么要使用泛型。您可以发布一个包含您希望使用的最少代码的类吗?这些扩展类是否必须向客户端公开非扩展类中不存在的方法?从我现在看到的情况来看,使用接口似乎更好。@Bohemian。只是添加了一些示例代码来说明我打算如何使用它。@trutheality我不打算公开我的模型类,但需要公开ModelProxy对象。我添加了一些代码来说明我打算如何使用它。ServiceImpl和ExtendedServiceImpl类。