Search code examples
javadesign-patternssolid-principlesfacade

Applying design patterns whilst collaborating with Facade pattern


At first, I had an input which is either "online" or "offline"

If the input is online, it gets data from online resources, and if it's offline, it gets data just from a mock text file that I personally made. Then I displayed the result on GUI using swing.

So at first, my main method was like this:

public static void main(String[] args) {

    if(args[0].equalsIgnoreCase("online")) {
        JFrame frame = new JFrame("Online");
        JTextField...
        ...
    } else if(args[0].equalsIgnoreCase("offline")) {
        JFrame frame = new JFrame("Offline");
        JTextField...
        ...
    }
}

And I used a design pattern in order to hide the complexity of the system.

Applying the facade

public static void main(String[] args) {

    Facade facade = new Facade();

    if(args[0].equalsIgnoreCase("online")) {
        facade.online();
    } else if(args[0].equalsIgnoreCase("offline")) {
        facade.offline();
    }
}

But the problem is, I now have one more input which is either "pdf" or "txt" which will decide in which file extension should the output of the data be stored.

So what I want to achieve here is, I want to apply a design pattern to cover up the second input as well whilst keeping the facade pattern.

The main method I desire to have is like:

public static void main(String[] args) {
    SomeRandomDesignPattern pattern = new SomeRandomDesignPattern();
    pattern.execute(args);
}

I am thinking of using the Strategy pattern here, but when I searched up google, people don't get used to using if/else statement with the Strategy pattern and that makes me confusing about how I should apply it to the system.

Can anyone tell me what kind of design pattern I can apply for it and how?

If applying the design pattern is not the best way, is there any better way for it? (I personally want to avoid using nesty if/else statements...)

Thanks in advance!


Solution

  • Consider your hopeful example:

    public static void main(String[] args) {
        SomeRandomDesignPattern pattern = new SomeRandomDesignPattern();
        pattern.execute(args);
    }
    

    Instead of new'ing up the pattern object, delegate that responsibility to a helper method:

    public static void main(String[] args) {
        SomeRandomDesignPattern pattern = new create();
        pattern.execute(args);
    }
    

    where create can be something as simple as a private helper method.

    Now move args from execute to the helper method:

    public static void main(String[] args) {
        SomeRandomDesignPattern pattern = new create(args);
        pattern.execute();
    }
    

    Now you have a well-known pattern: Command. All you have to do is to rename things:

    public static void main(String[] args) {
        Command command = new create(args);
        command.execute();
    }
    

    The create helper method can look at args to choose the appropriate subtype of Command to return.