简体   繁体   English

C#中的外观和模板方法模式有什么区别?

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

What is the difference between a Facade and a Template method pattern? Facade 和 Template 方法模式有什么区别? 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这个例子取自O'Reilly 设计模式

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).这里的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. DieselCarWithManualGearbox类提供了一些特定的实现,但整个算法保持不变。 Then you create some ElectroCarWithAutomaticGearbox that uses the same algorithm for driving, but needs its own ElectricEngine and AutomaticGearbox stuff to do it properly.然后你创建了一些使用相同算法进行驾驶的ElectroCarWithAutomaticGearbox ,但需要它自己的ElectricEngineAutomaticGearbox东西才能正确执行。

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.例如,静态类Console可以被视为控制台使用的门面。 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.尽管您使用了静态对象,但请查看其中涉及多少不同类型的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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