简体   繁体   中英

Can this (below) be considered a facade (structural design pattern)?

function BankOperationChecker() {
    //This is the facade
    this.applyFor = function(facadeMethods) {
        for (var method in facadeMethods) {
            facadeMethods[method]();
        }
    }
}

function BankClient(name, amount) {
    this.name = name;
    this.amount = amount;
    this.bankOperations = new BankOperationChecker();
}

var client = new BankClient("Davi Vieira", 2000);
var checkMethods = {
    cleanBackground: function() {
        console.log('The background of this client is clean.');
    },
    canGetCredit: function() {
        if (client.amount > 1000) {
            console.log('Can get credit!');
        } else {
            console.log('Cannot get credit!');
        }
    }
}

client.bankOperations.applyFor(checkMethods);

What do you think? Facade for entrance is just one... but is that right? Is there any specific rules about creating a facade?

No, it can't.

Facade, in English, means 'the face of a building, especially the principal front that looks onto a street or open space'. Because of this, many people confuse Facade pattern as something that acts as an entrance to something else- that, however, is not so.

As per this , the intents behind Facade patterns are:

  • Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
  • Wrap a complicated subsystem with a simpler interface.

Your example neither has bunch of subsystems nor does it have one interface to wrap all of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM