Facade

Facade

Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.

Vocabulary:

  • Client: makes calls to the facade for particular services
  • Facade: serves the client by making calls to subsystems for specific services
  • Subsystem: implements the services called on by the facade

Advantages

  • Isolation: We can easily isolate our code from the complexity of a subsystem.
  • Testing Process: Using Facade Method makes the process of testing comparatively easy since it has convenient methods for common testing tasks.
  • Loose Coupling: Availability of loose coupling between the clients and the Subsystems.

Disadvantages

  • Changes in Methods: As we know that in Facade method, subsequent methods are attached to Facade layer and any change in subsequent method may brings change in Facade layer which is not favorable.
  • Costly process: It is not cheap to establish the Facade method in out application for the system’s reliability.
  • Violation of rules: There is always the fear of violation of the construction of the facade layer.

Python implementation of facade patterm

Let's consider the customer care center. customers calls the customer care center and gets the account information and delivery package information.

Let's define the classes AccountInfo, DeliveryInfo, CustomerRepresentative

# service classes
class AccountInfo:
    def get_account_info(self, acc_id):
        # get account id
        info = {"account_id": "AC123", "name": "Anji"}
        return info


class DeliveryInfo:
    def get_package_info(self, package_id):
        info = {
            "name": "Cricket KIT",
            "address": "Hyderabad, Telangana, India"
        }
        return info

# facade class
class CustomerRepresentative:
    def __init__(self):
        self.account = AccountInfo()
        self.delivery = DeliveryInfo()

    def get_info(self, acc_id, package_id):
        acc = self.account.get_account_info(acc_id)
        pckg = self.delivery.get_package_info(package_id)
        print("account:", acc)
        print("package:", pckg)

if __name__ == '__main__':
    represntative = CustomerRepresentative()
    represntative.get_info("AC123", "PG5577")

When we run above code it gives the below output

account: {'account_id': 'AC123', 'name': 'Anji'}
package: {'name': 'Cricket KIT', 'address': 'Hyderabad, Telangana, India'}

References: