I am learning design patterns from Head First Design Patterns and the first pattern i came across is to "Code to interface".Now i want to use that pattern in my android app.My app includes lots of api calls.I am using Retrofit for for network calls.So i have used that design pattern considering some scenario something like this:
1. Suppose in future i need to shift from Retrofit to volley or any other networking library.
2. So i created an interface with a method to getData from server and created a class which implements that interface.The implemented method includes the Retrofit code to getData from server.
3. Now in future if i want to use volley i'll create a new class implementing the interface and with volley code to retrieve data from server.
The code goes like this
Interface
public interface NetworkCallsApi {
String getDataFromServer(Activity activity,String url,String Callback);
}
Retrofit Implementation
public class NetworkCallsRetrofitImpl implements NetworkCallsApi{
private Retrofit retrofit;
@Override
public String getDataFromServer(Activity activity, String url, String Callback) {
retrofit = new Retrofit.Builder()
.baseUrl(StringConstants.ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitApi apiCalls = retrofit.create(RetrofitApi.class);
Call<ResponseBody> call = apiCalls.getDataFromServer(url);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
return null;
}
}
ServiceCalls.java
public class ServiceCalls {
public static ServiceCalls serviceCalls;
public NetworkCallsApi networkCallsimpl;
public static ServiceCalls getInstance(){
if(serviceCalls == null){
serviceCalls = new ServiceCalls();
}
return serviceCalls;
}
public void setNetworkCallsimpl(NetworkCallsApi networkCallsimpl) {
this.networkCallsimpl = networkCallsimpl;
}
public String getDataFromServer(Activity activity, String url, String callback){
networkCallsimpl.getDataFromServer(activity,url,callback);
return null;
}
}
MainFragment Code:
public class StatusFragment extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Setting the implementation
ServiceCalls.getInstance().setNetworkCallsimpl(new NetworkCallsRetrofitImpl());
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_pnr_status,container,false);
try{
init();
}catch(Exception e){
}
return view;
}
private void init() {
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setUrlKeysAndValues();
ServiceCalls.getInstance().getDataFromServer(getActivity()
,Utils.getInstance().buildUrl(urlKeys,urlValues),"onResponse");
}
});
}
}
Now my question is "have i correctly implemented that design pattern".If not,what changes i need to do.In onCreate of MainFragment Code i am hardCoding the implementation.How Code to interface design pattern could be better applied in this scenario.Thankyou.
Aucun commentaire:
Enregistrer un commentaire