Software Development Tricky

Friday 15 December 2017

Facade Pattern

  • Facade pattern hides the complexities of the system and provide flexible interface to the client.
  • This pattern involves a single wrapper class which contains a set of members which are required by client. These members access the system on behalf of the facade client and hide the implementation details.
  • It’s used to help client application to easily interact with the system
  • The Facade pattern is particularly used when a system is very complex or difficult to understand because system has a large number of independent classes or it source code is unavailable
  • Facade as the name suggests means the front of the building. The people walking past the road can only see this glass front of the building. They do not know anything about it, the wiring, pipes and other complexities. The front hides all the complexities of the building and display a friendly look.

What is the difference between factory pattern & facade pattern?

 Every one strike out this question If you learn design pattern. Here is the answer for you.

S.No
Factory Pattern
Facade Pattern
1
It encapsulate a group of factories which are used for creating object
It is a classes or group of classes hiding     internal implementation / service from the user

  It can be used to provide abstraction to all kinds of operation(select, insert, update, delete), not just creation

Examples:


1.     Consider online stock management application. The client want purchase some things in online. The client not have enough knowledge of ordering, cards & place order internal functioning. Once the order is added to card or placed, the facade class layer calls the methods of the subsystems like stock for stock check, card for add & remove items & payment for processing the payment. After processing it returns the control to the client class with the confirmation about the order being processed. 

2.     Consider your computer start up process. When a computer starts up, it involves the work of CPU, memory & hard drive etc…. but end user not know their internal operation of starts up process of computer. The main purpose of make it easy to use for users. It hide complexity to end user.

3.     Imaging your using remote control to operate TV channel, volume & power button. Actually end user not know the internal functionality of each action. Just they are clicking button action, based on action the respective functionality work internal. It hide internal complexity to end user. Just show as user friendly display button.

Sample Demo Diagram:




UML Diagram:




Drawbacks:

In Facade design pattern the subsystem methods are connected to Facade Layer. In future development if structure of the sub systems changes then it will require subsequent change to the Facade layer and client methods.


Sample Demo Program:

The following participant are involved in given program

1.       Complex System – A ordering of subsystems.

2.       Subsystem 1, Subsystem 2, Subsystem 3: These are classes within complex system and offer detailed operations.

3.       Facade – This is wrapper class which wrapper class which contains a set of members which are required by client

4.       Client – This is a class which calls the high-level operations in the Facade

Entire Code Structure:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
 * Author: Manikandan. M
 * Description: Sample Program for Facade Pattern
 */
using System;
namespace FacadePattern
{
    /// <summary>
    /// sub system 1
    /// </summary>
    public class Stock
    {
        public string checkStock(string orderId)
        {
            return (orderId != "") ? "available" : "not avilable";
        }
    }

    /// <summary>
    /// sub system 2
    /// </summary>
    public class Card
    {
        public string addOrder(string orderId)
        {
            return "order added successfully";
        }
        public string removeOrder(string orderId)
        {
            return "order removed successfully";
        }

        public string checkOutOrder(string orderId)
        {
            return "order checkout successfully";
        }
    }

    /// <summary>
    /// sub system 3
    /// </summary>
    public class Payment
    {
        public string checkPayment(string paymentDetails)
        {
            return (paymentDetails != "") ? "valid payment" : "not valid payment";
        }

        public string deductPayment(string orderId)
        {
            return (orderId != "") ? "payment deducted successfully" : "payment not deducted. check from your order";
        }
    }

    /// <summary>
    /// order facade
    /// </summary>
    public class OrderFacade
    {
        private Stock stock;
        private Card card;
        private Payment payment;

        public OrderFacade()
        {
            stock = new Stock();
            card = new Card();
            payment = new Payment();
        }

        public string checkStockAvailability(string orderId)
        {
            string checkStockAvailability = stock.checkStock(orderId);
            return checkStockAvailability;
        }

        public string addToCard(string orderId)
        {
            return card.addOrder(orderId);
        }

        public string removeFromCard(string orderId)
        {
            return card.removeOrder(orderId);
        }

        public string checkoutOrderFromCard(string orderId)
        {
            return card.checkOutOrder(orderId);
        }

        public void placeOrder(string orderId)
        {
            string paymentValidationStatus = payment.checkPayment(orderId);
            Console.WriteLine(string.Format("\npayment validation status: {0}", paymentValidationStatus));
            string deductPaymentStatus = payment.deductPayment(orderId);
            Console.WriteLine(string.Format("\npayment deduct status: {0}", deductPaymentStatus));
        }

    }

    /// <summary>
    /// client
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("-----------------------------------Facade Pattern-------------------------------------");
            OrderFacade order = new OrderFacade();
            string orderId = "radio123"; //sample order id for this example
            string orderStatus = order.checkStockAvailability(orderId);
            if (orderStatus == "available")
            {
                Console.WriteLine(string.Format("\nstock status: {0}", orderStatus));
                Console.WriteLine(string.Format("\nadd card status: {0}", order.addToCard(orderId)));
                Console.WriteLine(string.Format("\ncheckout order from card status: {0}", order.checkoutOrderFromCard(orderId)));
                order.placeOrder(orderId);
            } else
            {
                Console.WriteLine(string.Format("\nstock status: {0}", orderStatus));
            }
            Console.Write("\nPress any key to exist...");
            Console.ReadKey();
        }
    }
}

Final Output:



Click here to download sample program

If you like this post, Kindly share your valuable comments.

No comments:

Post a Comment

Followers