英文:
How to Handle two different Objects for same Function
问题
我有以下两个对象之一,ObjOne
和 ObjTwo
,它们都共享相似的 getter 和 setter 方法,它们将传递给我的函数。
目前我有一个中间件,一个映射器,在内部方法之间使用,但可能有一种更清晰的方法来做到这一点,而不需要映射器,但缺乏具体的语法。
public String mapper(Object obj){
Map<String, String> map = new HashMap<>();
if(obj instanceof ObjOne){
ObjOne obj1 = (ObjOne)obj;
map.put("firstKey", obj1.getFirstValue());
}
else if(obj instanceof ObjTwo){
ObjTwo obj2 = (ObjTwo)obj;
map.put("firstKey", obj2.getFirstValue());
}
return secondFunction(map);
}
private String secondFunction(Map<String, String> map){
return thirdFunction(map.get("firstKey"));
}
在此是否有类似于 (ObjOne || ObjTwo)obj).getFirstValue()
的语法可以传递给 thirdFunction
?
编辑:我导入了这些对象,所以我不能为它们声明一个父类,它们共享的 getter 和 setter 方法对我的情况很方便。
英文:
I have either one of following Objects, ObjOne
and ObjTwo,
going into my function, both sharing similar getters/setters.
Currently I have an intermediary, a mapper, used across internal methods, but there might be a cleaner way to do this without a mapper but lacking specific syntax.
public String mapper(Object obj){
Map<String, String> map = new HashMap<>();
if(obj instanceof ObjOne){
ObjOne obj1 = (ObjOne)obj;
map.put("firstKey", obj1.getFirstValue());
}
else if(obj instanceof ObjTwo){
ObjTwo obj2 = (ObjTwo)obj
map.put("firstKey", obj1.getFirstValue());
}
return secondFunction(map);
}
private String secondFunction(Map<String, String> map){
return thirdFunction(map.get("firstKey"));
}
Is there such syntax for (ObjOne || ObjTwo)obj).getFirstValue()
to feed into thirdFunction
herein?
Edit: I imported these Objects, so I can't declare a parent class for them, they do share getters/setters that are convenient for my scenario.
答案1
得分: 1
更面向对象的方法是将那些你无法控制的对象组合到一个你可以控制的新对象中,然后根据你控制的对象编写你的API。
final class ObjOne {
String getFirstValue() {
return "foo";
}
}
final class ObjTwo {
String getFirstValue() {
return "bar";
}
}
class MyAdapter {
final Map<String, String> map = new HashMap<>();
MyAdapter(ObjOne o1) {
this(o1.getFirstValue());
}
MyAdapter(ObjTwo o2) {
this(o2.getFirstValue());
}
MyAdapter(String firstKey) {
map.put("firstKey", firstKey);
}
}
public String secondFunction(MyAdapter adapter) {
return thirdFunction(adapter.map.get("firstKey"));
}
英文:
A more OO approach is to compose the objects you don't control within a new object that you do control. Then write your API in terms of the object you control.
final class ObjOne {
String getFirstValue() {
return "foo";
}
}
final class ObjTwo {
String getFirstValue() {
return "bar";
}
}
class MyAdapter {
final Map<String, String> map = new HashMap<>();
MyAdapter(ObjOne o1) {
this(o1.getFirstValue());
}
MyAdapter(ObjTwo o2) {
this(o2.getFirstValue());
}
MyAdapter(String firstKey) {
map.put("firstKey", firstKey);
}
}
public String secondFunction(MyAdapter adapter) {
return thirdFunction(adapter.map.get("firstKey"));
}
答案2
得分: 0
建议不要传递对象,而是像这样创建一个基本模型并使用多态性。例如。
abstract class BaseObj {
abstract public String getFirstValue();
}
class ObjOne extends BaseObj {
@Override
public String getFirstValue() {
return "something useful";
}
}
class ObjTwo extends BaseObj {
@Override
public String getFirstValue() {
return "something useful";
}
}
不确定这里的用例是什么,但您总是可以相应地进行调整。
public String mapper(BaseObj obj) {
Map<String, String> map = new HashMap<>();
map.put("firstKey", obj.getFirstValue()); //常见的函数调用
return secondFunction(map);
}
private String secondFunction(Map<String, String> map) {
return thirdFunction(map.get("firstKey"));
}
private String thirdFunction(String firstKey) {
return null;
}
英文:
One suggestion don't pass Object instead do something like this create a base model use here polymorphism. for example.
abstract class BaseObj {
abstract public String getFirstValue();
}
class ObjOne extends BaseObj{
@Override
public String getFirstValue() {
return "something useful";
}
}
class ObjTwo extends BaseObj{
@Override
public String getFirstValue() {
return "something useful";
}
}
not sure what is the use case here but you can always mold accordingly.
public String mapper(BaseObj obj){
Map<String, String> map = new HashMap<>();
map.put("firstKey", obj.getFirstValue()); //common function call
return secondFunction(map);
}
private String secondFunction(Map<String, String> map){
return thirdFunction(map.get("firstKey"));
}
private String thirdFunction(String firstKey) {
return null;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论