0

I am on new on java. what I am trying to do is trying to create reusable generic class. Here is the my codes.

public interface Operation {
Boolean IsConnected();
Boolean ConnectionOpen();
Boolean ConnectionClose();
}

my main class

public class MyConnectionManager extends MyWifi{

    private MyWifi _wf;

    public MyConnectionManager(MyWifi wf) {
        // TODO Auto-generated constructor stub
        _wf= wf;
    }

    public Boolean IsConnected() {
        // TODO Auto-generated method stub
         return _wf.IsConnected();
    }


    public Boolean ConnectionOpen() {
        // TODO Auto-generated method stub
        return  _wf.ConnectionOpen();
    }


    public Boolean ConnectionClose() {
        // TODO Auto-generated method stub
        return _wf.ConnectionClose();
    }
}




public class MyWifi implements Operation {

    public Context _context =null;


    @Override
    public Boolean IsConnected() {
        // TODO Auto-generated method stub

        ConnectivityManager connManager = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (mWifi.isConnected()) {

    return true;
        }
        return false;
    }

but I want it to be generic and reusable,as the type should be changable .For example ,instead of MyWifi,it could be MyBlueTooth (which implement same interface) and so on.

Here is the what trying to achive.

MyWifi wf = new MyWifi();
//MyBlueTooth bl= new MyBlueTooth ();
MyConnectionManager<MyWifi> mn=  new MyConnectionManager<MyWifi>(wf);
mn.IsConnected();
0

2 Answers 2

1

You mean somethin like this?

public class MyConnectionManager<E extends Operation>{

private E _wf;

public MyConnectionManager(E wf) {
    // TODO Auto-generated constructor stub
    _wf= wf;
}

public Boolean IsConnected() {
    // TODO Auto-generated method stub
     return _wf.IsConnected();
}


public Boolean ConnectionOpen() {
    // TODO Auto-generated method stub
    return  _wf.ConnectionOpen();
}


public Boolean ConnectionClose() {
    // TODO Auto-generated method stub
    return _wf.ConnectionClose();
}

}

public class Starter {
public static void main(String[] args) {
    MyBlueTooth bt = new MyBlueTooth();
    MyWifi wf = new MyWifi();
    MyConnectionManager<MyBlueTooth> test = new MyConnectionManager<MyBlueTooth>(bt);
    MyConnectionManager<MyWifi> test2 = new MyConnectionManager<MyWifi>(wf);

}

}

Sign up to request clarification or add additional context in comments.

Comments

0

Change your MyConnectionManager as follows:

public class MyConnectionManager<T extends Operation> implements Operation {

    private T _op;

    public MyConnectionManager(T op) {
        // TODO Auto-generated constructor stub
        _op = op;
    }

    public Boolean isConnected() {
        // TODO Auto-generated method stub
         return _op.isConnected();
    }


    public Boolean connectionOpen() {
        // TODO Auto-generated method stub
        return  _op.connectionOpen();
    }


    public Boolean connectionClose() {
        // TODO Auto-generated method stub
        return _op.connectionClose();
    }

    public T getOperation() {
    return _op;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.