C# Design Pattern–Facade

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Facade
{
    //Main Entry to calculate Cost of Transportation - Car, Bike and Public
    class MainEntry
    {
        static void Main()
        {
            // Create an instance of Facade Class
            Facade _facade = new Facade();

            // Call Facade Class Method which is a wrapper to different classes
            // Car, Bike and PublicTransport.
            Console.WriteLine(_facade.TotalCost(2,4,20));

            // Hold the console without disappearing
            Console.ReadLine();
        }
    }

    // Class - Car 
    class Car
    {
        public Decimal GetExpense(int vehicleCount)
        {
            return vehicleCount * 10000;
        }
    }

    // Class - Bike
    class Bike
    {
        public Decimal GetExpense(int vehicleCount)
        {
            return vehicleCount * 1000;
        }
    }

    // Class - PublicTransport
    class PublicTransport
    {
        public Decimal GetExpense(int numberOfDays)
        {
            return numberOfDays * 100;
        }
    }

    // Class - Facade, which wraps all individual classes and perform
    // Operation. In this case, this calculate the total expense of 
    // Transportation.
    class Facade
    {
        private Car _car;
        private Bike _bike;
        private PublicTransport _publicTransport;

        public Facade()
        {
            _car = new Car();
            _bike = new Bike();
            _publicTransport = new PublicTransport();
        }

        // Method to Calculate total sum of expenses on transportation
        public Decimal TotalCost(int carCount, int bikeCount, int numberOfDays)
        {
            return _car.GetExpense(carCount) + _bike.GetExpense(bikeCount) + _publicTransport.GetExpense(numberOfDays);
        }
    }
}

 

 

Output -

Facade

You may also like...