1

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.

Gany
  • 13
  • 1
  • 5
  • Like a [Singleton](https://sourcemaking.com/design_patterns/singleton/java/1)? And using [Dependency Injection](http://www.vogella.com/tutorials/DependencyInjection/article.html) – Mario Santini Mar 20 '17 at 19:01
  • None of the code you show will compile unless you spell `class` correctly. – Lew Bloch Mar 20 '17 at 19:05
  • What does "No creating instance and using the instance created from interface" mean? Instances cannot be created directly from interfaces, so the phrase is meaningless on the face. – Lew Bloch Mar 20 '17 at 19:06
  • @LewBloch corrected Sir :) – Gany Mar 20 '17 at 19:18
  • @MarioSantini I'm new to java. May be I will check out the links. But I do came across of Singleton. But not sure how to use it for my requirement. Since I have multiple classes. – Gany Mar 20 '17 at 19:22
  • @LewBloch Yes interface doesn't create instance but when one of the class gets executed and it will call methods from other class as well. When methods from other class gets called by that time instance gets created. So we can use that instance for other classes as well while execution. Correct me if I'm wrong. – Gany Mar 20 '17 at 19:27
  • @Gany Its better that your independent test cases test with fresh objects. Suppose you modify the state of an object in some method then while executing another method, you would not be sure if its the mistake in the current method being tested or its due to wrong order of methods execution or is it a side effect of the mwthod executed before. – nits.kk Mar 20 '17 at 19:34
  • @nits.kk I agree, but actually there is no modification of the state involved in our case. We are just using the instance to call the methods under that class.So instead of creating instance again and again for a particular class we thought of using singe instance. – Gany Mar 20 '17 at 19:45

0 Answers0