重置可变类字段

huangapple go评论59阅读模式
英文:

Reset mutable class fields

问题

假设我有一个带有一些可变字段的类:

public class Test
{
    private boolean isCorrect;
    private String userId;

    public void run() {...}
    
    // 更多方法
}

run() 方法将修改这些字段。我的问题是,调用此类的调用者可能会随意调用 run() 任意次数,因此我应该将这些字段重新设置为 null

我的问题是,对于这种情况,最佳做法是什么?我应该在每次调用该方法时重置字段吗?

public void run()
{
    isCorrect = null;
    userId = null;
    // 进行操作
}

还是有更清晰/更智能的方法来处理这个问题?

英文:

Say I have a class with some mutable fields:

public class Test
{
    private boolean isCorrect;
    private String userId;

    public void run() {...}
    
    // more methods
}

The run() method will be modifying these fields. My issues is that the caller of this class might call run() any number of times, and for this reason I should be re-setting the fields back to null.

My question is, what is the best practice for this scenario? Should I reset the fields every time the method is called?

public void run()
{
    isCorrect = null;
    userId = null;
    // do stuff
}

Or is there a cleaner/smarter approach to this?

答案1

得分: 2

简单的答案是使用局部变量。楼主在评论中澄清了 run 方法调用了同一个实例上的其他方法,这些方法也希望使用这些变量。

这个类应该被拆分。run 方法应该创建一个包含这些字段的对象,并在其上调用方法。

public class Test {

    public void run() {
         TestImpl impl = new TestImpl();
         impl.run();
    }
    
    // 更多方法
}

class TestImpl {
    private boolean isCorrect;
    private String userId;

    public void run() {...}
    
    // 更多方法
}

你可以将新类创建为嵌套类,但这会导致过多的缩进。内部类也可以直接访问 Test 类的任何生存更长的变量。匿名内部类(或者更不常见的,本地类)会更加方便,但会导致更多的缩进。

英文:

The simple answer is use local variables. The OP has clarified in the comments that run calls other methods on the same instance that expect to use these variable too.

The class should be split. The run method should create an object containing the fields and call methods on that.

public class Test {

    public void run() {
         TestImpl impl = new TestImpl();
         impl.run();
    }
    
    // more methods
}
class TestImpl {
    private boolean isCorrect;
    private String userId;

    public void run() {...}
    
    // more methods
}

You could make the new class a nested class, though that does cause excessive indention. An inner class would also have direct access to any longer lived variables of Test. An anonymous inner class (or, more obscurely, a local class) would be even more convenient but indented.

答案2

得分: 1

我会这样做。使用异常来实现。这样,任何敢于两次调用 run() 的人都会被踢出。

package test;

import com.sun.jdi.IncompatibleThreadStateException;

public class Test{
    private boolean isRunning = false;

    public void run() throws IncompatibleThreadStateException{
        if(this.isRunning) {
            throw new IncompatibleThreadStateException();
        }
        else {
            this.isRunning = true;
        }
    }

    public static void main(String[] args) {

    }
}
英文:

I would do it this way. Using an exception. So anyone who dares to use run() twice gets kicked out.

package test;

import com.sun.jdi.IncompatibleThreadStateException;

public class Test{
	private boolean isRunning = false;
	
	public void run() throws IncompatibleThreadStateException{
		if(this.isRunning) {
			throw new IncompatibleThreadStateException();
		}
		else {
			this.isRunning = true;
		}
	}
	
	public static void main(String[] args) {
		
	}
}

huangapple
  • 本文由 发表于 2020年10月24日 11:30:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64509688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定