简体   繁体   中英

Which design pattern should I use?

I have the following code which executes in sequence, method after another.

I load the request, perform a couple of checks like checking if a response already exists for this request, if not, I call the service and receive the response which I save to the DB.

I was looking for a design pattern I can use in such a case, I thought of posting this here and get some ideas.

public class Manager
    {

        public void PutRequest()
        {
            //Do Something

            if (loadRequest())
            {
                  callService(); 

                  //Do Something
                  saveResponse();
            }

        }

        private bool loadRequest()
        {            
             bool isExist = checkIfResponseExists();

             if (!isExist)
             {
              // If false, load request from DB
             } 

             return !isExist;

        }

        private bool checkIfDataExists()
        {
            //Check if a response already exists in the DB for this request
        }

        private void callService()
        {
            //Call the service and receive the response
        }    

        private void saveResponse()
        {
            //Store the response in the DB
        }

    }

Patterns are used for solving some problems. What problem your current code have? I don't see any duplicated code, beside names of methods. There is no pattern, which fixes method naming problem.

Yes, your code need some refactoring, but not to patterns. Better class and method naming is a first step. Also, I'd removed field isExist .

public class Manager
{
   public void PutRequest()
   {
       //Do Something
       if (!CheckIfResponseExists()) // returns boolean value
            LoadRequestFromDB()

        CallService(); 
        //Do Something
        SaveResponse();
   }
}

Check the design pattern called Strategy, it defines an interface common to all supported algorithms and each concrete strategy implements an algorithm

http://www.oodesign.com/strategy-pattern.html

It seems like it'd be more useful for several of these methods to be functions. So instead of having a method who's responsibility is to both check for a condition and do some other actions, you have a function that checks for a condition then the method that called it does some action depending on the result. (Kind of the SRP applied to methods...)

public void DoAllTheThings!() // Oops, Ruby syntax creeping in :P
{
   if(!WeCanDoIt())
   {
     MakeItSo(); // So that we can do it...
   }

   NowWeCanDoAllTheThings();
}

private bool WeCanDoIt() {}
private void MakeItSo() {}
private void NowWeCanDoAllTheThings() {}

Command + Composite.

Some people consider the use of an if/then Command - in your case that would be in putRequest - in a Composite a kind of Chain Of Responsibility.

While selecting a pattern you should consider scalability of the application

One of the pattern you can apply is pattern 模式

There will be two states.

  1. Response is already there
  2. Need to process the new response

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