08 Mar 2023

Structural Design Patterns

  • Facade:

    • basically a simplified API
    • jQuery is a facade for JavaScript features
class House {
    private plumbing = new Plumbing();
    private electrical = new Electrical();

    public turnOnSystems() {
        this.plumbing.on();
        this.electrical.setVoltage(120);
    }   
}
  • Proxy:

    • Replace a target object with a proxy, eg. to override functions
    • Controls access to the original object, allowing you to perform something either before or after the request gets through to the original object
    • Why? For example you have a large object that you only need occasionally.
    • Create a new proxy class with the same interface as an original object → update your app so that it passes the proxy object to all of the original object’s clients → the proxy creates a real service object and delegates all the work to it.
    • Benefit: If you need to execute something either before or after the primary logic of the class, the proxy lets you do this without changing that class.