Facade Design Pattern

  • Structural design pattern
  • The intent of this pattern is to provide an unified interface to a set of interfaces in a subsystem.
  • It defines higher lever interface that makes subsystems easier to use.
  • It hides the complexity of the calculation and ecourages resue.
  • Facade knows which subsystem classes are responsible for a request and delegates client request to a appropriate subsystem objects.
  • Each subsystem class implements subsystem functionality. They dont know about the facade, but they do handle work assigned by it.
  • The number of objects that client has to deal is reduced.
  • Client can still use subsystem classes if they want to.
				
					namespace decoratorpattern
{
    
    public class EmployeeValidateService
    {
        public bool isEmployeeEligible(int employeeCredit)
        {
            return employeeCredit > 100;
        }
    }

    public class EmployeeWorthinessScore
    {
        public double CalculateCustomerWorthiness(int employeeId)
        {
            //some calculation
            return 1000;
        }
    }

    public class EmployeeCreditScore
    {
        public double CalculateCustomerWorthiness(int employeeId)
        {
            //some calculation
            return 2000;
        }
    }

}

				
			
				
					namespace decoratorpattern
{
    public class EmployeeFacade
    {
        private readonly EmployeeValidateService employeeValidateService = new();
        private readonly EmployeeCreditScore employeeCreditScore = new();
        private readonly EmployeeWorthinessScore employeeWorthinessScore = new();

        public double CalculateEmployeeScore(int customerId)
        {
            if (!employeeValidateService.isEmployeeEligible(customerId))
            {
                return 0;
            }
            return employeeCreditScore.CalculateCustomerWorthiness(customerId)
                - employeeWorthinessScore.CalculateCustomerWorthiness(customerId);
        }
    }

}

				
			
				
					using decoratorpattern;

var facade = new EmployeeFacade();
Console.WriteLine(facade.CalculateEmployeeScore(12345));
				
			

1000

Leave a Comment