简体   繁体   中英

What is the difference between Facade and Template Method Pattern in C#?

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM