简体   繁体   English

使用匿名类返回Java中的值

[英]Using an anonymous class to return values in Java

Consider the following code: 考虑以下代码:

public interface MyClass {
    public final String getMyObject1();
    public final String getMyObject2();
}

public class MyClass1 implements MyClass {
    private String myObject1;
    private String myObject2;
    public MyClass1(String myObject1, String myObject2) {
        this.myObject1 = myObject1;
        this.myObject2 = myObject2;
    }
    public String getMyObject1() {
        return myObject1;
    }
    public String getMyObject2() {
        return myObject2;
    }
}

public interface MyClass2 extends MyClass {
    public static MyClass2 newInstance(String myObject1, String myObject2) {
        return new MyClass2() {
            public String getMyObject1() {
                return myObject1;
            }
            public String getMyObject2() {
                return myObject2;
            }
        };
    }
}

And I use them like 我像这样使用它们

public static void func(MyClass m) {
    m.getMyObject1();
    m.getMyObject2();
}
func(new MyClass1(o1, o2));
func(MyClass2.newInstance(o1, o2));

I wonder how they differ and if I only need to read from the values (ie to use MyClass as a "struct" to pass values), using the anonymous class can it be a simpler approach? 我想知道它们之间有什么区别,是否只需要读取值(即使用MyClass作为传递值的“结构”),使用匿名类是否可以是一种更简单的方法?

Otherwise, what are the draw backs? 否则,退缩是什么?

One core rule of programming: try to not surprise your readers. 编程的一个核心规则:尽量不要让读者感到惊讶。

Your approach here to use a static class within an interface as "factory" method is very surprising (and believe me: I have seen a lot of Java code). 您在此处将接口内的静态类用作“工厂”方法的方法非常令人惊讶(相信我:我已经看过很多 Java代码)。

If at all, the more "common" way of handling such things: create a static class with a slightly similar name, you know, like there is java.lang.Object and java.lang.Objects that carries some useful static helper methods. 如果有的话,处理这种事情的更“常见”的方法是:创建一个名称稍微相似的静态类,就像java.lang.Object和java.lang.Objects一样,它们带有一些有用的静态帮助器方法。

And beyond that, there is already a class in Java that helps with arbitrary numbers of "named" values; 除此之外,Java中已经有一个类可以帮助处理任意数量的“命名”值。 and that is called a Map ! 这就是所谓的Map

Finally: there are some good arguments for "DTO"s ( data transfer objects ) but esp. 最后:对于“ DTO”( 数据传输对象 )有一些很好的论据,但尤其是。 for "beginners", you should rather look into "real" OO designs; 对于“初学者”,您应该研究“真正的” OO设计。 based on the SOLID principles. 基于SOLID原则。 In that sense: design real classes that exactly model your problem domain; 从这个意义上说:设计能够精确建模您的问题领域的真实类; and that provide helpful abstractions. 并且提供有用的抽象。 A struct with an arbitrary number of members ... doesn't fall into either category. 具有任意数量成员的结构...不在任何类别中。

The problem here is not the code necessarily but the design. 这里的问题不是代码,而是设计。 I would be interested to know the real world use case you are trying to design here. 我很想知道您要在此处设计的实际用例。

Surely there are limitations in the second approach like you cannot update the value of your objects at all once your class is created as you just have a way to get the value of the passed objects back. 当然,第二种方法存在局限性,例如创建类后就无法再更新对象的值,因为您只有一种方法来获取传递的对象的值。

Coming back to Design: 回到设计:

An interface is supposed to be an action which your class can perform if it implements that interface. 如果接口实现了该接口,则它应该是您的类可以执行的操作。 In your case you are trying to return the value of two instance variables using the two methods in your interface which is a kind of action but it ignores the basic principle of encapsulation. 在您的情况下,您尝试使用接口中的两个方法返回两个实例变量的值,这是一种操作,但它忽略了封装的基本原理。

If your class defines/owns those instance variables it should have the getters and setters for that. 如果您的类定义/拥有那些实例变量,则它应该具有该方法的getter和setter方法。 You should not require an interface to do that. 您不需要接口即可执行此操作。 So ideally your interface should not be required. 因此,理想情况下,不需要您的界面。 Any other class which uses MyClass1 object should directly use the getters and setters of the MyClass1. 使用MyClass1对象的任何其他类都应直接使用MyClass1的getter和setter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM