简体   繁体   中英

Which design Pattern to use in case of Rest WebService Client?

I have a webservice performing following operations:

  • Login
  • Logout
  • Registration

Each webservice differs only in terms of parameters passed in case of createRequest() method and processResponse() method.

public Map<String, String> SubscriberLogin(String username ,String password ,   String addInfoData)
{

        Map<String, String> returnMap = new HashMap<String, String>();
        try {
            Map<String,String> paramMap = new HashMap<String, String>();
            paramMap.put("userIdentity", username);
            paramMap.put("password", password);

// Every webservice varies in value of Rest_WsConstants(webservice path) and paramMap-key/value mapping 
            HttpPost postReq = createRequest(Rest_WsConstants.LOGIN_WS.path, paramMap);

            StringEntity entity = new StringEntity(addInfoData);
            postReq.setEntity(entity);

            HttpResponse wsResponse = wsClient.execute(postReq);

            returnMap = processResponse(wsResponse);

        }catch (Exception e) {
            Logger.logError(MODULE,"Error while login, reason:"+e.getMessage());
        }
        return returnMap;
}

But currently there is a single Singleton class and all operations are defined as separate static methods.

Please suggest which design pattern to implement in such scenarios.

I would use Factory Pattern for implementing this scenario. I would have following structure.

I would follow selector approach where selector can be login , logout , registration and so on. I would define three classes to achieve separation of concern as I would not want to change and test whole code if I combine all the logic related to these operation in single class and if I need to do a simple change in any of the mentioned operation. So having said I have three class LoginHelper , LogoutHelper and RegistrationHelper .

The factory class would have instances of all three classes and based on selector I would delegate the operation to related class.

Also the factory that I am talking about would contain handlers to you DAO layer and would provide the same to your helper if they require.

Have factory instance in your class from where you want to delegate the task of login and logout etc. factory.processRequest("login", "passAdditionInformationIfYouWantLikeRequestEtc.");


interface UserActivity{
    void processResponse(); 
}

class LoginHelper implements UserActivity{
    public void processResponse() { /*write logic*/ }   
}

class LogoutHelper implements UserActivity{
    public void processResponse() { /*write logic*/ }   
}    

class Factory{
    private UserActivity login = new LoginHelper();
    private UserActivity logout = new LogoutHelper();

    //Call this method by passing selector
    public UserActivity processRequest(String selector) {
        UserActivity activityHandler = null;
        if(selector.equals("login")){
            activityHandler = this.login;
        }else if(selector.equals("logout")){
            activityHandler = this.logout;
        }
        return activityHandler;
    }
}

If you are implementing a tree-based structure (/users/USERNAME/subscriptions to display subscription information for a user named USERNAME) you may want to look at chain-of-responsibility. The suggestion to use Factory for creating the individual actions is a good one, so stick with that, except you will want to be able to chain the actions.

By "chain" I mean that the "users" action requires the USERNAME, and should be executed first (get user data), then invoke the "subscriptions" action while passing itself as a parameter and/or the user data that it (presumably) retrieves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM