简体   繁体   English

这是什么? 模板方法还是什么?

[英]What is this? Template method or what?

I have a class Request.cs我有一个 class Request.cs

It has an abstract method:它有一个抽象方法:

public abstract Response CreateResponse(XmlReader reader);

and there's also a method:还有一个方法:

public Response SendRequest(string requestURI)
{
    ...

    XmlReader reader = XmlReader.Create(responseStream);

    return CreateResponse(reader);
}

The CreateResponse method is implemented in a subclass that extends Request . CreateResponse method在扩展Request的子类中实现。 And that implementation returns response.并且该实现返回响应。

I don't understand how you can return a method like this that's an abstract method but the value returned is actually in the implementation of the method from the subclass.我不明白你怎么能返回一个像这样的抽象方法,但返回的值实际上是在子类的方法的实现中。 Is this an example of the Template Method pattern or just simple polymorphism, or is this "tricky or too savvy" code or even messy?这是模板方法模式的一个例子还是只是简单的多态性,或者这个“棘手或过于精明”的代码甚至是混乱的? I'm trying to figure out how this can even be done (is this basic OOP principal or design pattern) and is this a good practice or good use of OOP?我试图弄清楚这甚至可以如何完成(这是基本的 OOP 主体或设计模式),这是 OOP 的良好实践还是良好使用?

This is a very standard approach.这是一种非常标准的方法。 In fact it's often recommended.事实上,它经常被推荐。 The CreateResponse method is like a blank that has to be filled in by the derived class. CreateResponse方法就像一个空白,必须由派生的 class 填充。

Yes, this is template method.是的,这是模板方法。 Since the Response class is abstract, you are forced to implement CreateResponse before you can call SendRequest.由于响应 class 是抽象的,因此您必须在调用 SendRequest 之前实现 CreateResponse。 This way, the common logic is encapsulated in the base class and the rest of the logic can vary as needed (by creating multiple implementations of the base class).这样,通用逻辑封装在基础 class 中,逻辑的 rest 可以根据需要进行更改(通过创建基类的多个实现)。

The main reason being that when you use the method, you never know about the details, just the interface and an abstract method has that defined.主要原因是当你使用方法时,你永远不知道细节,只是定义了接口和抽象方法。

Being as the method is abstract it has no details since they must be defined by the derived class.由于该方法是抽象的,因此没有细节,因为它们必须由派生的 class 定义。 The main point that makes this possible is that the "INs" and "OUTs" never change as the method IS abstract in the base therefore the interface is defined, just not the implementation... yet.使这成为可能的主要观点是,“INs”和“OUTs”永远不会改变,因为方法在基础中是抽象的,因此定义了接口,而不是实现......但是。

Think of it like someone selling basic airline tickets from point A to B not knowing what airline was going to be used.可以把它想象成有人出售从 A 点到 B 点的基本机票,但不知道要使用哪家航空公司。 They have no idea how you will get there yet but once they hand off the contract of the sold ticket to an airline, that airline will figure out the details of meeting the contracts terms.他们还不知道您将如何到达那里,但是一旦他们将已售机票的合同移交给航空公司,该航空公司就会弄清楚满足合同条款的细节。 All you cared about when buying the ticket was that you knew for x dollars you were going to get from A to B.买票时,您所关心的只是您知道从 A 到 B 可以花 x 美元。

This is template method, and template method is really not much more than simple polymorphism.这就是模板方法,而模板方法其实也不过是简单的多态。 This is exactly the kind of thing typical C# / Java -style OO polymorphism is intended for and is a good usage of the language.这正是典型的 C# / Java 风格的 OO 多态性旨在用于并且是该语言的良好用法。

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

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