Search code examples
javadesign-patternswrapperanti-patternsfacade

Using Facade Like a wrapper


I usually see some people used facade like this.

public class FooFacade {
   Foo foo;

   public boolean isFunny(param1, param2) {
       IsFunnyInput isFunnyInput = new IsFunnyInput(param1, param2);
       return foo.isFunny(isFunnyInput);
   }
}

Is this a right approach? In my opinion, it is just adding one more class to the package. When you can also directly use Foo to do the same.


Solution

  • For me, it is an example of the decorator pattern and it makes sense only if Foo and FooFacade share the same interface.

    interface Foo { boolean isFunny(p1, p2); }
    class FooFacade implements Foo { ... } // FooDecorator, actually
    

    In contrast, the Facade pattern is used to perform more complex interactions where a few different classes involved (and to hide this complexity).

    The Wrapper pattern (aka Adapter) implies several interfaces.