-3

I have classes PageA and PageB.
Methods and variable are absolutely different.
But what is common for both of them, from PageA and PageB it should be possible to call methods of class Popup.

The problem is that after executing method "closePopup" of the class Popup, it should return instance of the class, which called him (PageA or PageB).

I have done following:

class PageA{

    public PageA dosomethingA(){
        //some code
        return this;
    }

    public Popup openPopup(){
        $("button.media").click();
        return new Popup(PageA.class);
    }
}

class PageB{

    public PageB dosomethingB(){
        //some code
        return this;
    }

    public Popup openPopup(){
        $("p.images").click();
        return new Popup(PageB.class);
    }
}


class Popup<T>{

    private Class<T> callerClass;

    public Popup(Class<T> callerClass) {
        this.callerClass = callerClass;
    }

    public Popup doSomething1(){
        //some code
        return this;
    }

    public Popup doSomething2(){
        //some code
        return this;
    }   

    public Popup doSomething3(){
        //some code
        return this;
    }   


    public Class<T> closePopup(){
        //some code
        return callerClass;
    }   
}

Run:

pageA.dosomethingA()
    .openPopup()
    .doSomething1()
    .closePopup()

    //Error Object class is returned, PageA class should be returned
    .dosomethingA();

pageB.dosomethingB()
    .openPopup()
    .doSomething2()
    .doSomething3()         
    .closePopup()   


    //Error Object class is returned, PageB class should be returned
    .dosomethingB();

The problem is that closePopup method returns an Object. How to fix it?

Eran
  • 387,369
  • 54
  • 702
  • 768
Artur
  • 661
  • 1
  • 6
  • 23

1 Answers1

2

You have several issues:

  1. Some of your methods return a raw Popup instead of Popup<T>.
  2. You are storing a Class<T> instance instead of T instance inside your Popup<T>, so you cannot retrieve T.

Instead of storing Class<T>, store T:

class PageA {

    public PageA dosomethingA(){
        //some code
        return this;
    }

    public Popup<PageA> openPopup(){

        return new Popup<PageA>(this);
    }
}

class Popup<T> {

    private T caller;

    public Popup(T caller) {
        this.caller = caller;
    }

    public Popup<T> doSomething1(){
        //some code
        return this;
    }

    public Popup<T> doSomething2(){
        //some code
        return this;
    }   

    public Popup<T> doSomething3(){
        //some code
        return this;
    }   


    public T closePopup(){
        //some code
        return caller;
    }   
}

Now the following will work:

PageA pageA = new PageA ();
pageA.dosomethingA()
     .openPopup()
     .doSomething1()
     .closePopup()
     .dosomethingA();
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Could you please help. Following case: PageA and PageB classes has inside them method check() with different realization. It's needed to execute method check() inside doSomething1(). How to do it without doing - if (caller instanceof PageA)? PageA and PageB could implement common interface, but what about variable caller? – Artur Sep 28 '19 at 14:06