ATech Guide

Facade Pattern

Published: 2018-02-04 • Updated: 2019-09-09

Characteristics of Facade

  • To abstract out existing implementaion of a Class (e.g. API)
  • Internally facade uses composition
  • Facade doesn't require any interface
  • Useful while refactoring existing class

Examples of Facade

  • FacesContext (javax.faces.context)
  • ExternalContext (javax.faces.context)

Java Implementaion

Phone.class

public interface Phone {

    String build();
}

IPhone.class

public class IPhone implements Phone {

    @Override
    public java.lang.String build() {
        return "IOS 10";
    }
}

FacadeExample.class

public class FacadeExample {

    public static void main(String[] args) {

        // Cleaner Client Code
        PhoneFacade phoneFacade = new PhoneFacade();

        // We are creating phone using facade without going through complex logic
        System.out.println(phoneFacade.buildAndroidPhone());
        System.out.println(phoneFacade.buildApplePhone());
    }
}

Disadvantages of Facade

  • Overuse of the facade pattern under which a lot of legacy code is present

To access the full working code sample, click here

References

Design Pattern
Share this Post