Wednesday, January 2, 2008

Facade Design Pattern

The Facade pattern is probably one of the most frequently used design patterns. When a class requires the functionality derived from different classes, it is often useful to use a façade class that encapsulates the calls to the various classes. This class can then be effectively used instead of making calls to the individual classes and improves code reuse. Additionally, if we decide to make changes to the encapsulated classes, all we need to alter are the method invocations in the façade class.
Consider a business application for a mortgage company. The Mortgage Company uses multiple services that are both internal to the company as well as require interaction with other parties to determine if a loan can be sanctioned. Using the Facade pattern here is easy, create a class say LoanHelper. We can then create shared method unless you prefer to create an instance of the LoanHelper. Add the method calls to the different internal and external services. This LoanHelper method can then be used anywhere within your code to validate a prospect before allotting a loan and determining the rate of interest.

LoanHelper Class Listing

Public Class LoanHelper

 

  Public Const DEFAULT_LOAN_PERCENT As Double = 10.0

 

  Public Shared Function GetEligbleLoanCriteria(ByVal SSN As String, ByVal RequiredLoanAmount As Double, _

  ByRef EligibleLoanAmount As Double, ByRef EligibleLoanPercent As Double) As Boolean

    'Add the method calls to various systems - internal and external to determine

    'the values of EligibleLoanAmount and the EligibleLoanAmount

    EligibleLoanAmount = 100000

    EligibleLoanPercent = 8.25

    Console.WriteLine("Façade class LoanHelper was invoked to determine Eligible Loan Criteria")

    Return True  'Return false if SSN failed to match

  End Function

End Class



modMain Module Listing

Module modMain

  Sub Main()

    Dim SSN As String = "SampleSSN"

    Dim RequiredLoanAmount As Double = 100000

    Dim EligibleLoanAmount As Double = 0

    Dim EligibleLoanPercent As Double = LoanHelper.DEFAULT_LOAN_PERCENT

    Dim IsSSNMatch As Boolean = LoanHelper.GetEligbleLoanCriteria(SSN, RequiredLoanAmount, _

    EligibleLoanAmount, EligibleLoanPercent)

    Console.ReadLine()

  End Sub

End Module



A look at the classes


And the results

No comments: