接口使用和泛型的良好组合肯定会产生一个简单且可扩展的更简洁的实现。
这是我的建议 - 我实际上实现了这个并且效果很好:
基于 OP 的 cmets 进行更新
模型接口可以被“生成”以限制返回类型:
package org.example;
/**
* Created by prahaladd on 08/07/15.
*/
public interface Model<T extends Identifier>
{
T getIdentifier();
}
实现使用特定类型标识符的模型类:
package org.example;
/**
* Created by prahaladd on 08/07/15.
*/
public class Person implements Model<StringIdentifier>
{
private final String name;
private final String id;
public Person(String id, String name)
{
this.id = id;
this.name = name;
}
@Override
public StringIdentifier getIdentifier()
{
return new StringIdentifier(id);
}
public String getName()
{
return name;
}
}
ChangeSet 实现现在进行了一些更改以模仿 Map 接口,如下所示。事实上,它现在接受将作为键存储的标识符的类型:
package org.example;
import java.util.Map;
/**
* Created by prahaladd on 08/07/15.
*/
public class ChangeSet<T extends Identifier, M extends Model<T>>
{
//Refer to PECS - http://stackoverflow.com/questions/2723397/java-generics-what-is-pecs
private Map<? super Identifier, M> changeMap;
public void addChange(M element)
{
changeMap.put(element.getIdentifier(),element);
}
public M getChangedElementForId(T id)
{
return changeMap.get(id);
}
}
所有这些更改都还不错 - 您可以很容易地实例化 ChangeSet,如下所示:
package org.example;
public class Main {
public static void main(String[] args)
{
Person p1 = new Person("1", "Tom");
Person p2 = new Person("2", "Jerry");
//change set is instantiated without any redundant generic parameters
ChangeSet<StringIdentifier, Person> changes = new ChangeSet<StringIdentifier,Person>();
//assume that there were some changes and you want to add them to the changeset.
changes.addChange(p1);
changes.addChange(p2);
//retrieve element from the changeset for an id
p1= changes.getChangedElementForId(new StringIdentifier("1"));
p2 = changes.getChangedElementForId(new StringIdentifier("2"));
}
}
替代解决方案
首先 - 定义一个接口来封装您的 ID。这不是矫枉过正。假设您有不同类型的 ID,使用接口来定义标识符的合同将大大有助于使您的代码干净和可扩展:
package org.example;
/**
* Created by prahaladd on 08/07/15.
*/
public interface Identifier<T>
{
T getIdentifier();
}
现在您已经定义了一个 Identifier 接口,您可以根据您的各种 ID 类型为其定义不同的实现。例如下面我提供了 StringIdentifier 的实现,它生成字符串类型的 ID:
package org.example;
/**
* Created by prahaladd on 08/07/15.
*/
public class StringIdentifier implements Identifier<String>
{
private final String identifier;
public StringIdentifier(String id)
{
identifier = id;
}
@Override
public String getIdentifier()
{
return "someId";
}
}
现在定义模型接口。理想情况下,模型接口不应该处理任何 ID 类型,它应该只知道它必须返回一个标识符(就像您的用例一样)。
package org.example;
/**
* Created by prahaladd on 08/07/15.
*/
public interface Model
{
Identifier getIdentifier();
}
现在提供模型接口的实现。例如以下是您的查询中提到的 Person 类:
包 org.example;
/**
* Created by prahaladd on 08/07/15.
*/
public class Person implements Model
{
private final String name;
private final String id;
public Person(String id, String name)
{
this.id = id;
this.name = name;
}
@Override
public Identifier getIdentifier()
{
return new StringIdentifier(id);
}
public String getName()
{
return name;
}
}
现在定义变更集。 ChangeSet 应该只知道它存储了 ID 对象和对应的 Model 之间的映射。它并不真正了解 ID 对象的类型。这使得 ChangeSet 类非常灵活,甚至可以支持除了您想要的同质集合之外的异构集合。
package org.example;
import java.util.Map;
/**
* Created by prahaladd on 08/07/15.
*/
public class ChangeSet<M extends Model>
{
//Refer to PECS - http://stackoverflow.com/questions/2723397/java-generics-what-is-pecs
private Map<? super Identifier, M> changeMap;
private Class identifierType;
public void addChange(M element)
{
//prahaladd - update : save the identifier type for a later check.
if(identifierType != null)
{
identifierType = element.getIdentifier.getClass();
}
changeMap.put(element.getIdentifier(),element);
}
public M getChangedElementForId(Identifier id)
{
//prahaladd updated - verify that the type of the passed in id
//is the same as that of the changeset identifier type.
if(!id.getClass().equals(identifierType))
{
throw new IllegalArgumentException();
}
return changeMap.get(id);
}
}
现在,努力得到了回报。看看下面的客户端实现:
package org.example;
public class Main {
public static void main(String[] args)
{
Person p1 = new Person("1", "Tom");
Person p2 = new Person("2", "Jerry");
ChangeSet<Person> changes = new ChangeSet<Person>();
//assume that there were some changes and you want to add them to the changeset.
changes.addChange(p1);
changes.addChange(p2);
//retrieve element from the changeset for an id
p1= changes.getChangedElementForId(new StringIdentifier("1"));
p2 = changes.getChangedElementForId(new StringIdentifier("2"));
}
}
和你想象的一样!!如您所见,这里没有什么花哨的东西。简单的面向对象的概念以及接口和泛型的深思熟虑的组合。
希望这会有所帮助!