简体   繁体   English

接口继承层次结构

[英]Interface inheritance hierarchy

As part of code refactoring, I have found some code duplicates that I'm trying to remove 作为代码重构的一部分,我发现了一些我要删除的重复代码

I have an interface like one below, in an assembly I cannot modify. 我有一个类似于下面的界面,在我无法修改的程序集中。

public interface IArtifact
{
    void Accept(IArtifactVisitor visitor);
}

public  interface IArtifactVisitor
{
    void Visit(Topic topic);
}

In the references assembly, which I want to reuse existing interfaces there is the same function signature 在引用程序集中,我想重用现有接口,具有相同的函数签名

public interface IArtifact
{
    void Accept(IArtifactVisitor visitor);
}

public  interface IArtifactVisitor
{
     void Visit(Topic topic);
     void Visit(NewTopic topic);
}

and the Accept looks something like this 和接受看起来像这样

public void Accept(IArtifactVisitor visitor)
{
    visitor.Visit(this);
}

In order to removed the code reuse, I have tried the following: 为了删除代码重用,我尝试了以下方法:

public interface MyIArtifact : IArtifact
{
    void Accept(MyIArtifactVisitor visitor);
}

public interface MyIArtifactVisitor : IArtifactVisitor
{
    void Visit(NewTopic topic);
}

but what this does is, it forces me in each implementation class to implement both Accept(MyIArtifactVisitor visitor) and Accept(IArtifactVisitor visitor) 但这是什么,它迫使我在每个实现类中实现Accept(MyIArtifactVisitor visitor)和Accept(IArtifactVisitor visitor)

Is there a better way to do this? 有一个更好的方法吗?

If I understand your question correctly, you only need to specify one method signature in your interface 如果我正确理解了您的问题,则只需在界面中指定一个方法签名

public interface MyIArtifactVisitor : IArtifactVisitor
{
     void Visit(NewTopic topic);
}

And that's essentially it. 基本上就是这样。 So, if you want to impliment that, and the external interface, then you do something like the following 因此,如果您想隐含该信息以及外部接口,则可以执行以下操作

public class MyImplimentingClass : MyIArtifactVisitor, IArtifactVisitor 
{
     public void Visit(NewTopic topic)
     {}

     public void Visit(Topic topic)
     {}
}

The visitor pattern is screwed up, and IMHO it is non-sense to implement it through interfaces. 访问者模式搞砸了,恕我直言,通过接口实现它是毫无意义的。 The way you implement visitor pattern is as follows: 实现访问者模式的方式如下:

abstract class Artifact{
     internal abstract void Visit(ArtifactVisitor visitor);
}


class Topic : Artifact{
     internal override void Visit(ArtifactVisitor visitor)
     {
        visitor.Visit(this);
     }
}

class ArtifactVisitor{
    internal virtual void Visit(Artifact artifact)
    {
        artifact.Visit(this);
    }
    protected virtual void Visit(Topic topic)
    {
    }
}

class SomeSpecificTopicVisitor : ArtifactVisitor
{
     protected override void Visit(Topic topic)
     {
         //do something with topics
     }     
}

From this state, you can start inheriting your own visitors from ArtifactVisitor. 在此状态下,您可以开始从ArtifactVisitor继承您自己的访问者。 You will only override the methods which you actually need. 您将只覆盖您实际需要的方法。

The visitor pattern is not very extensible. 访客模式不是很可扩展。 It is only useful if the set of inheritors is fixed and will not change significantly. 仅当继承者集是固定的并且不会发生重大变化时,此选项才有用。 Nevertheless, if you use it properly, it will nicely solve your double dispatching problems. 但是,如果正确使用它,它将很好地解决您的双重调度问题。

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

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