0

First of all, I had task 1 and it was about rest API, requesting calls using httpclient in order to use the functions of an API. We get an input from a user, either "Online" or "Offline". If it is an "Online", we request calls such as generating tokens, and we display the result on GUI using javafx or swing. If the input is "Offline", we just request calls from a mocked API that I personally made so that the program can be run even if the server is down or the internet is cut off. I used a facade pattern here in order to flexibly and simply change in between online and offline.

In the main method, it goes like this:

if(input.equals("online")) {
    facade.online();
} else if (input.equals("offline")) {
    facade.offline();
}

The second one is the extension of task 1. They are similar to each other but the thing is, we get two inputs in which they are, the first one is the same as the one in task 1, but the second input is for the output of the result. For example, saying that we have A and B APIs and we get information/data from A by requesting calls, then we output the result through the B API (B can be an api like a pastebin, which we can save the result by creating a note on the pastebin site). If the second input is "Offline", we just save the result on a notepad.

I need to keep the facade pattern here, but what kind of design pattern should I apply in order to cover the second input as well?

I am thinking of using Factory or Strategy pattern for it, and am doing some researches for that, but still not getting ideas of how to collaborate facade with factory or strategy pattern...

Thanks.

2

1 Answer 1

0

The Strategy pattern is definitely a solution for this and it can easily be meshed with the façade pattern. The primary work will be creating offline and online versions of each of the create offline and online versions of the classes that sit behind the façade.

You could just use a simple switch as you show and check it in the façade routing whenever you are calling out to the underlying implementation classes. That's a little messy, especially if there are many different classes behind the façade. A cleaner approach is to implement the strategy pattern in your façade:

class FacadeStrategy {
    public static final ONLINE = "online";
    public static final OFFLINE = "offline"

    private Facade facade;
    private Writer writer;

    
    FacadeStrategy(String input, String output) {
        if (input.equals(ONLINE)) {
            facade = OnlineFacade();
        } else if (input.equals(OFFLINE)) {
            facade = OfflineFacade();
        }

        if (output.equals(ONLINE)) {
            writer = PastebinWriter();
        } else if (output.equals(OFFLINE)) {
            writer = TextfileWriter();
        }
    }

    public void foo() {
        writer.write(facade.foo());
    }

    public void bar() {
        writer.write(facade.bar());
    }
}

This simplifies things because your OnlineFacade and OfflineFacade are completely separate and you don't have to worry about the online/offline state within those classes.

Additionally, the code above allows you to flip from one state to another while the application is running. If that isn't desirable or possible, you can make the facade variable final.

4
  • Thank you so much for your reply Jimmy, but how should I apply the Strategy pattern for two inputs?
    – CompSydBoi
    May 27, 2021 at 8:34
  • @CompSydBoi I added a writer to address the second part of your question. I think I understand what you are asking.
    – JimmyJames
    May 27, 2021 at 15:39
  • But are we allowed to use if/else for Strategy pattern?
    – CompSydBoi
    May 28, 2021 at 7:36
  • @CompSydBoi It's often stated that design patterns are 'descriptive' not 'prescriptive'. There are no rules saying you can or can't use if statements to choose the strategy object. Frankly anyone who tries to tell you that you should never use if statements is a ridiculous someone you should stop listening to. Having said that, you don't necessary need an if statement, you could use a Map keyed off the input values just as well. It seems like overkill to 2 options, though.
    – JimmyJames
    May 28, 2021 at 16:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.