We have some 100 classes in our project. Each class defines a webpage of our application. Ex, we have separate class for Login page, Logout, Home Screen, etc., So like this we have many webpages in our application and we have separate class file for each page. We are doing automation testing, so each class has its own methods according to the pages. So we create instance of a class with new() keyword.
class A{
public static void main(String args[]){
B objb = new B();
C objc = new C();
method1();
B.method1();
C.method2();
}
public void method1();
public void method2();
}
class B{
public static void main(String args[]){
A objb = new A();
C objc = new C();
method1();
A.method1();
C.method2();
}
public void method1();
public void method2();
}
class C{
public static void main(String args[]){
B objb = new B();
A objc = new A();
method1();
B.method1();
A.method2();
}
public void method1();
public void method2();
}
So in the sample code above, i'm creating instance of class A or B or C repeatedly in other class files. So we have some 100 classes. Is there a way we can create single instance of a class A and use it again for other Class files instead of creating new instance again ?
Is it good to create a interface and create object of all the classes and implement it in all the classes. So by this way when i try to run Class A or B or C only one time interface will executed and the instance of the classes created once and can be reused.
public interface TestInterface {
A objc = new A();
B objb = new B();
C objc = new C();
// Adding all the classes - say 100 or more
}
class A implements TestInterface{
public static void main(String args[]){
method1();
// Not creating instance and using the reference from interface which creates a instance when it executes the method.
B.method1();
C.method2();
}
public void method1();
public void method2();
}
Note: When I execute script can run multiple classes. Like first it can execute class A then C then B and goes on.