Facade Design Pattern C#

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

FacadeDesignPattern (640x480)

Example: We have different formats(ODI, Test and T20 = Subsystems) in cricket and you have a requirement to get sum of total runs scored by a player in each format of cricket. Instead of calling each class method in main, its better to include such complex functionalities in Facade(Cricket) class.

namespace TestFacadePattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Player player = new Player("Sachin");
            Cricket cricket = new Cricket();

            int TotalRuns = cricket.GetAllFormatTotalRunsByPlayer(player);
            Console.WriteLine("Total Runs Scored by {0}: {1}", player.PlayerName, TotalRuns);

            Console.ReadKey();
        }
    }

    public class Player
    {
        private string _playerName;

        //Constructor
        public Player(string playerName)
        {
            this._playerName = playerName;
        }

        public string PlayerName { get { return _playerName; }}
    }

    /// <summary>
    /// SubSystem 1
    /// </summary>
    public class ODICricket
    {
        public int GetTotalRunsByPlayer(Player player)
        {
            //Some Code logic here to get/compute total ODI Score Of a player
            int totalRuns = 15921;
            Console.WriteLine("Total ODI Score Of {0}: {1}", player.PlayerName,totalRuns);
            return totalRuns;
        }
    }

    /// <summary>
    /// SubSystem 2
    /// </summary>
    public class TestCricket
    {
        public int GetTotalRunsByPlayer(Player player)
        {
            //Some Code logic here to get/compute total ODI Score Of a player
            int totalRuns = 18426;
            Console.WriteLine("Total Test Score Of {0}: {1}", player.PlayerName, totalRuns);
            return totalRuns;
        }
    }

    /// <summary>
    /// SubSystem 3
    /// </summary>
    public class T20Cricket
    {
        public int GetTotalRunsByPlayer(Player player)
        {
            //Some Code logic here to get/compute total T20 Score Of a player
            int totalRuns = 10;
            Console.WriteLine("Total T20 Score Of {0}: {1}", player.PlayerName, totalRuns);
            return totalRuns;
        }
    }

    /// <summary>
    /// Facade
    /// </summary>
    public class Cricket
    {
        public int GetAllFormatTotalRunsByPlayer(Player player)
        {
            ODICricket odi = new ODICricket();
            TestCricket test = new TestCricket();
            T20Cricket t20 = new T20Cricket();

            int totalRuns = test.GetTotalRunsByPlayer(player) + odi.GetTotalRunsByPlayer(player) + t20.GetTotalRunsByPlayer(player);
            return totalRuns;
        }
    }
}

Output:
FacadeDesignPattern_Output.JPG

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s