Skip to main content
added writer strategy
Source Link
JimmyJames
  • 30.9k
  • 3
  • 59
  • 110

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"ONLINE)) {
            facade = OnlineFacade();
        } else if (input.equals("offline"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.

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 {
    private Facade facade;

    
    FacadeStrategy(String input) {
        if(input.equals("online")) {
            facade = OnlineFacade();
        } else if (input.equals("offline")) {
            facade = OfflineFacade();
        }
    }

    void foo() {
        facade.foo();
    }

    void bar() {
        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.

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.

Source Link
JimmyJames
  • 30.9k
  • 3
  • 59
  • 110

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 {
    private Facade facade;

    
    FacadeStrategy(String input) {
        if(input.equals("online")) {
            facade = OnlineFacade();
        } else if (input.equals("offline")) {
            facade = OfflineFacade();
        }
    }

    void foo() {
        facade.foo();
    }

    void bar() {
        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.