[英]How can I avoid having to rely on an object of another class to make the methods in a different class work?
Maybe the title was confusing, so here's a snippet of what I'm trying to avoid:也许标题令人困惑,所以这里是我试图避免的片段:
public class Generator{
private static GUI userInterface;
public static boolean specialValidator(String specialEntryText)
{
if(entryValidator(specialEntryText))
{
int specialChars = Integer.parseInt(specialEntryText);
int maxPossible = Integer.parseInt(userInterface.getLength())-3;
if(specialChars < 1 || specialChars > maxPossible)
{
return false;
}
return true;
}
return false;
}
public static void main(String[] args) {
userInterface = new GUI();
}
}
My program runs and functions as intended (keep in mind there is more to it than this), but I don't know if what I've done here is considered bad practice or what the downsides of doing it this way are.我的程序按预期运行和运行(请记住,还有比这更多的功能),但我不知道我在这里所做的是否被认为是不好的做法,或者这样做的缺点是什么。 If my main
method was not in the Generator
class, this would not work, which seems like a problem to me.如果我的main
方法不在Generator
类中,这将不起作用,这对我来说似乎是一个问题。
Also, is there a specific name for what I did here, too?另外,我在这里所做的事情也有一个特定的名称吗?
The main method is the entry point of the program, and it needs to be in a class. main方法是程序的入口点,需要在一个类中。 It does not need to be in the Generator
class.它不需要在Generator
类中。
As long as there is access to the class that you want to use, you can call it from another class.只要可以访问要使用的类,就可以从另一个类调用它。 In you case it is public
so it should be OK.在你的情况下,它是public
所以应该没问题。
If it is in another class it could be something like如果它在另一个班级,它可能是这样的
package yourPackage;
public class Main {
public static void main (String[] args) {
Generator gen = new Generator ();
//
gen.specialValidator(..);
}
}
Many things jump out at me.很多事情都让我眼前一亮。
There seems to be a dependency on GUI
in specialValidator
which is producing a "tight coupling" - you can't use the method without GUI
. specialValidator
似乎对GUI
有依赖性,这会产生“紧耦合”——没有GUI
就不能使用该方法。
This doesn't seem to make sense to me.这对我来说似乎没有意义。 You want to focus on reducing this coupling/dependency by passing all the required information into the method directly, for example...您希望通过将所有必需的信息直接传递到方法中来专注于减少这种耦合/依赖性,例如...
public class Generator {
public static boolean specialValidator(String specialEntryText, int length) {
if (entryValidator(specialEntryText)) {
int specialChars = Integer.parseInt(specialEntryText);
// Any resason we're not using the specialEntryText length?
int maxPossible = length - 3;
if (specialChars < 1 || specialChars > maxPossible) {
return false;
}
return true;
}
return false;
}
}
Now specialValidator
doesn't care "how" the information is generated, only that the information is made available to it.现在specialValidator
不关心“如何”生成信息,只关心信息是否可用。 This "decouples" the method and makes it more independent, meaning you can call it any way you like (it also supports "dependence injection" making it more testable 😝)这“解耦”了方法并使其更加独立,这意味着您可以随心所欲地调用它(它还支持“依赖注入”使其更易于测试😝)
And now you can call it anyway you like, for example...现在你可以随心所欲地称呼它,例如......
public class Main {
public static void main(String[] args) {
Generator.specialValidator("some text", 8);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.