jeudi 27 mai 2021

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!

Aucun commentaire:

Enregistrer un commentaire