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();
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);
}
}