简体   繁体   English

如何避免不得不依赖另一个类的对象来使不同类中的方法工作?

[英]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.

相关问题 我可以使用单个对象访问具有diff属性的不同类的方法吗? - Can I access methods of different class having diff properties using single object? 我如何使子 class 的字段和方法可用于 class,它仅通过另一个 class 使用超级 class - How can i make fields and methods of a sub class usable to a class which only uses the super class through another class 如何用不同的类对象制作2D数组? - How Can I make 2D array with different object of class? 如何避免激活类中的所有方法? - How can I avoid activating all the methods on a class? 为什么要为 Main 类创建一个对象,以使方法起作用? - Why make an object for Main class, for the methods to work? 如何告诉一个类使用另一个类的方法? - How can I tell a class to use methods of another class? 如何在 class 中使用另一个 class 的方法? - How can I use methods from another class in a class? 我不能从另一个班级做这个班级的对象 - I can't make an object of this class from another class 如何使一个类方法与数组中的另一个类方法一起使用? - How can I make a class method work with another class method in an array? 在另一个包中委托接口的方法时,如何使委托类非公开? - How can I make delegate class non-public when delegating methods of an interface in another package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM