0

What is the difference between a Facade and a Template method pattern? Both of them provide high level views of the subsystem and hide it from the user.

Facade Pattern

 internal class SubsystemA
    {
        internal string A1()
        {
            return "Subsystem A, Method A1\n";
        }
        internal string A2()
        {
            return "Subsystem A, Method A2\n";
        }
    }
    internal class SubsystemB
    {
        internal string B1()
        {
            return "Subsystem B, Method B1\n";
        }
    }
    internal class SubsystemC
    {
        internal string C1()
        {
            return "Subsystem C, Method C1\n";
        }
    }

    public static class Facade
    {

        static SubsystemA a = new SubsystemA();
        static SubsystemB b = new SubsystemB();
        static SubsystemC c = new SubsystemC();
        public static void Operation1()
        {
            Console.WriteLine("Operation 1\n" +
            a.A1() +
            a.A2() +
            b.B1());
        }
        public static void Operation2()
        {
            Console.WriteLine("Operation 2\n" +
            b.B1() +
            c.C1());
        }
    }

    // ============= Different compilation

    // Compile with csc /r:FacadeLib.dll Facade-Main.cs
    class Client
    {
        static void Main()
        {
            Facade.Operation1();
            Facade.Operation2();
        }
    }

Template pattern

 interface IPrimitives
    {
        string Operation1();
        string Operation2();
    }

    class Algorithm
    {
        public void TemplateMethod(IPrimitives a)
        {
            string s =
            a.Operation1() +
            a.Operation2();
            Console.WriteLine(s);
        }
    }

    class ClassA : IPrimitives
    {
        public string Operation1()
        {
            return "ClassA:Op1 ";
        }
        public string Operation2()
        {
            return "ClassA:Op2 ";
        }
    }

    class ClassB : IPrimitives
    {
        public string Operation1()
        {
            return "ClassB:Op1 ";
        }
        public string Operation2()
        {
            return "ClassB.Op2 ";
        }
    }

    class TemplateMethodPattern
    {

        static void Main()
        {
            Algorithm m = new Algorithm();

            m.TemplateMethod(new ClassA());
            m.TemplateMethod(new ClassB());
        }
    }

This example has been taken from O'Reilly Design Patterns

In the above provided example, both Facade and Template pattern Implement an interface and the uses an abstraction and defines on how the operation should be handled. I dont find any difference between the two patterns. Can anyone help me understand it.

Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
  • 1
    Facade pattern is like the API of a library(perhaps similar to System namespace an all that stems from it which is the facade for mscorlib.dll and other libraries), like the interface layer of your app while you have the business and the data layer in the hood, The template method pattern is something like heuristics - a receipt to do sth not the actual implementation. – αNerd Nov 16 '16 at 20:28
  • @αNerd i have updated the question, do you notice any difference from both the patterns? if yes then could you help me to understand it? thank you – Lijin Durairaj Nov 16 '16 at 20:50
  • related: http://stackoverflow.com/questions/1781860 – jaco0646 Nov 16 '16 at 20:55
  • @jaco0646 the link provided by you made me more confuse. for instance the first answer tells that Facade requires to implement Interface but from the code which i found in MSDN, there is not interface implemented for Facade – Lijin Durairaj Nov 16 '16 at 21:19
  • @jaco0646 reply given by #dymanoid confused me even more – Lijin Durairaj Nov 16 '16 at 21:21
  • 1
    @Lijin John. Pizza ordering app: you use some basic controls like buttons and menus and selectors. With them you choose size, adding, quantity. Also fill the address, .. at the end you make the order by clicking the order button. This is the interface for the client. On the inside you have the inner workings of the restaurant. Someone picks the order, transfer it to restaurant, restaurant transfer it to delivery guy. He/she brings the pizza at your doorstep. You do not care how and what they do to process the delivery, prepare the pizza/s, bring it/them to you. Pizza app is the facade. – αNerd Nov 16 '16 at 21:34
  • 1
    Template is what they do in basic steps , not in details, so that you can have your pizza. Facade is for the consumer, the front user, template is for the creator, builder. – αNerd Nov 16 '16 at 21:37
  • @αNerd could you help me with some refrence, maybe some videos, so that i could understand it better, thanks – Lijin Durairaj Nov 16 '16 at 21:43
  • 1
    Template: https://www.youtube.com/watch?v=kBT4xfloMAQ. Facade: https://www.youtube.com/watch?v=a2Qh10YjP6Y. There are many more. Basically any language example will do the job. – αNerd Nov 16 '16 at 21:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128307/discussion-between-lijin-john-and-nerd). – Lijin Durairaj Nov 16 '16 at 23:24

3 Answers3

6

Facade pattern will introduce new functionality by combining sub functionalities under wrapper method. Facade class in this case have different structure then sub classes.

Template pattern provide skeleton of algorithm in the base class and gives possibility for derived classes to override/implement some units of this algorithm.
In this case classes derived from template have same structure as base class.

Fabio
  • 31,528
  • 4
  • 33
  • 72
  • thank you for your help :), I have a small question by seeing your sample code. Doesn't your sample code look like a Decorator pattern. please forgive me if i am wrong. – Lijin Durairaj Nov 17 '16 at 00:36
2

The main purpose of the template method pattern is to define some generic algorithm, where some implementation details might be specified by the derived classes.

Here is an example:

abstract class Car
{
    public void Drive()
    {
        IgnitionOn();
        EngineOn();
        EngageTransmission();
    }

    protected abstract void IgnitionOn();
    protected abstract void EngineOn();      
    protected abstract void EngageTransmission();
}

Here the Drive() method is a template method that defines the generic behavior (how to drive). But every derived class can (or, in this example, have to) provide implementation details.

Example:

class DieselCarWithManualGearbox : Car
{
    protected override void IgnitionOn()
    {
        IgnitionControlModule.IgnitionState = IgnitionState.On;
    }

    protected override void EngineOn()
    {
        DieselEngine.StartFuelPump();
        DieselEngine.Run();
    }

    protected override void EngageTransmission()
    {
        ManualGearbox.Gear = 1;
    }
}

The DieselCarWithManualGearbox class provides some specific implementation, but the whole algorithm stays unchanged. Then you create some ElectroCarWithAutomaticGearbox that uses the same algorithm for driving, but needs its own ElectricEngine and AutomaticGearbox stuff to do it properly.

The facade pattern can be used to simplify the usage of some logic that is contained in multiple interfaces or modules. For example, the static class Console can be seen as a facade for console usage. It hides the implementation details and provides a couple of simple methods we can easily use. We cannot change the behavior of a facade though by providing some additional implementatons. That is the difference.

dymanoid
  • 14,771
  • 4
  • 36
  • 64
0

In simple words: The template method belongs to a base class and allows the subclasses to redefine some steps. You create an object of a class and invoke this template method to complete your job. But facades often involve multiple objects from many different classes. This time you perform a series of steps to accomplish the task involving all these objects. You do not redefine the methods in these classes, instead, you manage to call them easily.

Now to aswer your question:

In your example, in the template pattern, see that you use only one object of the Algorithm. But it is not the case for a facade. Though you have used static objects, see how many different types of objects are involved there.