Facade Design Pattern

Facade Pattern: A single class that represents the entire system. It is needed when one simple action needs many other action behind the scenes.

 

Example: A withdrawal from bank includes following actions

  1. Check if account is valid
  2. Check if Pin is valid
  3. Check if Amount is sufficient
  4. Make changes accordingly

Folling is thr code: Code is also available on GitHub. Click here to go to git hub repository

namespace FacadeFDesignPattern

{

class Program

{

static void Main(string[] args)

{

var facade = new BankFacade(12345678,1234);

 

facade.WithdrawCash(50);

facade.WithdrawCash(100);

facade.DepositCash(50);

Console.ReadLine();

}

}

internal class BankFacade

{

private int _accountNumber;

private int _pin;

AccountNumberCheck accountNumberCheck;

PinCheck pinCheck;

FundCheck fundCheck;

public BankFacade(int accountNumber, int pin)

{

this._accountNumber = accountNumber;

this._pin = pin;

accountNumberCheck = new AccountNumberCheck();

pinCheck=new PinCheck();

fundCheck=new FundCheck();

}

internal void DepositCash(int amount)

{

if (accountNumberCheck.IsAccountValid(_accountNumber) && pinCheck.IsPinValid(_pin))

{

fundCheck.MakeDeposit(amount);

Console.WriteLine($”Transaction Complete”);

}

}

internal void WithdrawCash(int amount)

{

if(accountNumberCheck.IsAccountValid(_accountNumber)&&pinCheck.IsPinValid(_pin)&&fundCheck.HasEnoughFund(amount))

Console.WriteLine($”Transaction Complete”);

}

}

public class AccountNumberCheck

{

public bool IsAccountValid(int accountNumber)

{

if (accountNumber == 12345678)

return true;

return false;

}

}

public class PinCheck

{

public bool IsPinValid(int pinNumber)

{

if (pinNumber == 1234)

return true;

return false;

}

}

public class FundCheck

{

private double _totalAmount = 1000;

public double GetTotakAmount()

{

return _totalAmount;

}

public void IncreaseFund(double amountDeposited)

{

_totalAmount += amountDeposited;

}

public void DecreaseFund(double amountWithdrawn)

{

_totalAmount -= amountWithdrawn;

}

public bool HasEnoughFund(int amountToWithDraw)

{

if (_totalAmount < amountToWithDraw)

{

Console.WriteLine($”Withdraw Fail.Insufficient amount. Current balance is {_totalAmount}”);

return false;

}

else

{

DecreaseFund(amountToWithDraw);

Console.WriteLine($”Withdraw complete. Current balance is {_totalAmount}”);

return true;

}

}

public void MakeDeposit(int amountToDeposit)

{

IncreaseFund(amountToDeposit);

Console.WriteLine($”Deposit complete. Current balance is {_totalAmount}”);

}

}

}

Author: Rupesh

Hi! I'm Rupesh, a funophile and technophile. I'm an Application Developer, Solution Architect and IT-Consultant, and an author in the works. I am a Microsoft certified Professional and Solution Developer (MCP and MCSD).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s