简体   繁体   English

在这种情况下如何避免重复代码?

[英]How to avoid the duplication of code in this case?

My project is about implementing an hyperGraph in java 我的项目是关于在java中实现hyperGraph

My hyperGraph contain various type of hyperEdge depending on vertex type that I have 我的hyperGraph包含各种类型的hyperEdge,具体取决于我所拥有的顶点类型

Vertex Type:Image , tags ... 顶点类型:图像,标签......

HyperEdge =Homogeneous(relate vertex of same type )/Heterogeneous(relate vertex of different type) HyperEdge =同质(关联相同类型的顶点)/异构(关联不同类型的顶点)

Homogeneous HyperEdge= Image-image HyperEdge/Tag-tag hyperEdge 同构HyperEdge =图像图像HyperEdge / Tag-tag hyperEdge

This is a quickly draw UML diagram 这是一个快速绘制的UML图

在此输入图像描述

在此输入图像描述 this is my code 这是我的代码

public interface HomogenousHyperedge< T extends Vertex<L>, L> extends Hyperedge {

   public abstract List<T> searchNearstNeighborsVertex(
      Hypergraph hypergraph, T vertex);
}


public class ImageImageHyperedge implements
   HomogenousHyperedge<ImageVertex, Map<String,Instance>> {

   @Override
   public List<ImageVertex> searchNearstNeighborsVertex(Hypergraph hypergraph,
        ImageVertex vertex) {
      return null;
   }
}

the problem is in the ImageImageHyperEdge class I should to know what is the type of feature based on it I will search the ImageVertex nearest neighbors I can't pass it into the abstract method of the super Interface because TagTagHyperEdge class don't need it 问题出在ImageImageHyperEdge类中我应该知道基于它的特征是什么类型我将搜索ImageVertex最近邻居我不能将它传递给超级接口的抽象方法因为TagTagHyperEdge类不需要它

and if I replace ImageImageHyperEdge class by {featureOneHyperEdge class ,...featureFiveHyperEdge class } (in which I know the feature type) it will be a duplication of code as it is the same nearest neighbors search algorithm 如果我用{featureOneHyperEdge类,... featureFiveHyperEdge类}替换ImageImageHyperEdge类(我知道它的特征类型)它将是一个重复的代码,因为它是相同的最近邻搜索算法


feature= low level feature of an image (color histogram for example) feature =图像的低级特征(例如颜色直方图)
I have 5 type of low level feature 我有5种低级功能
I will use each one to search the nearest neighbors of my current image 我将使用每一个来搜索当前图像的最近邻居
All feature are stocked in a simple textual file 所有功能都存储在一个简单的文本文件中
the same algorithm is used to search the nearest neighbors 相同的算法用于搜索最近的邻居
only the file is changed every time 每次只更改文件

Your UML design's not good enough. 你的UML设计不够好。 Skip the ugly & difficult-to-read 'styling', show us 'Vertex' as well as 'Edge', and an association diagram; 略过难看且难以阅读的'造型',向我们展示'Vertex'和'Edge',以及关联图; not your (potentially over-complicated) idea of inheritance. 不是你的(可能过于复杂)继承的想法。

Your APIs, design & fundamental question are not really clear. 您的API,设计和基本问题并不十分清楚。 'Hyperedge' classes could represent a single instance of an Edge, and associate the two ends of that; 'Hyperedge'类可以代表Edge的单个实例,并将其两端相关联; or they could (if better named) represent an Edge Type, and search the graph globally from a specified Endpoint parameter. 或者他们可以(如果更好地命名)表示边缘类型,并从指定的端点参数全局搜索图形。

These are complelely different designs, your question is meaningless until you figure the above out. 这些是完全不同的设计,在你弄清楚以上之前,你的问题毫无意义。

Either way, Edge.search() doesn't have the right signature. 无论哪种方式,Edge.search()都没有正确的签名。 Where VS and VE are start and end vertex-types, and TE is edge-types, it should either be: VS和VE是起始和结束顶点类型,而TE是边缘类型,它应该是:

public class EdgeType {
    public List<EV> getEndpoints (SV startVertex);
}

or 要么

public class Vertex {
    public List<TE> Vertex.getEdges();
}
public class Edge {
    public EV Edge.getEndpoint();
}

The nearest-neighbour algorithm should be implemented using generic types, and then called as required (with exact type-signatures) by the concrete classes. 应使用泛型类型实现最近邻居算法,然后由具体类根据需要调用(具有精确类型签名)。

BTW, when you mention "nearest neighbor"; 顺便说一下,当你提到“最近邻居”时; Neither is it clear if "nearest neighbour" means directly connected vertex , which is trivial, or to find the nearest distant (how is distance measured? you don't specify) vertex of a specified type. 也不清楚“最近邻居”是指直接连接的顶点 ,这是微不足道的,或者找到指定类型的最近的距离(如何测量距离?你没有指定)。

Either way, the utility & correctness/ need to implement subtypes of 'Edge' seems unclear. 无论哪种方式,实现'Edge'子类型的实用性和正确性/需要似乎都不清楚。 Many graph algorithms find Vertices/Nodes to be interesting and subtype these, but I am less than aware of subtyping (or the utility of subtyping) the edges leading to these. 许多图算法发现顶点/节点是有趣的并且这些是子类型,但我不太了解通向这些的边缘的子类型(或子类型的效用)。

Last tip: ditch the complex naming, KISS. 最后提示:放弃复杂的命名,KISS。 'Vertex' and 'Edge' will help you get a clear, simple, comprehensible & correct design. 'Vertex'和'Edge'将帮助您获得清晰,简单,易于理解和正确的设计。 Save the extra wordage for after you've got that. 在得到之后保存额外的字数。


In response to further info from Nawara: 回应Nawara的进一步信息:

Then it's an EdgeType you've modelled, and when asking 'nearest neighbor' you should take a Start Vertex & return either Edge(s) -- if you need the distance metrics -- or Vertices. 然后它是你已经建模的EdgeType,当你问'最近邻居'时,你应该选择一个起始顶点并返回边缘 - 如果你需要距离度量 - 或顶点。

The reference to 'Graph' should be probably implicit from the Vertex parameter. “图形”的引用应该可能隐含在Vertex参数中。

As far as your EdgeType inheritance heirarchy: subtypes & inheritance should be defined to follow behavioural characteristics, not the generic types (Vertex types) they reference. 至于你的EdgeType继承heirarchy:子类型和继承应该被定义为遵循行为特征,而不是它们引用的泛型类型(顶点类型)。 The principle for OO class heirarchy design is to model doing , not being . OO类层次设计的原则是模拟 ,而不是

In this regard you might have a KnnDistanceEdgeType & FlickrDistanceEdgeType classes, either as ancestors, or, if no other method behaviour needs to be different, as the actual implementing classes. 在这方面,您可能有一个KnnDistanceEdgeType和FlickrDistanceEdgeType类,作为祖先,或者,如果没有其他方法行为需要不同,则作为实际的实现类。 The feature types/classes they're searching for can be set as properties -- with properties & genericized differently, to answer different Vertex types. 他们搜索的要素类型/类可以设置为属性 - 具有不同的属性和通用化,以回答不同的顶点类型。

eg. 例如。

IMAGE_IMAGE_EDGES = new KnnDistanceEdgeType<ImageVertex,ImageVertex>( ImageVertex.class, ImageVertex.class);
TAG_TAG_EDGES = new FlickrDistanceEdgeType<TagVertex,TagVertex>( TagVertex.class, TagVertex.class);
ANY_EDGES = new KnnDistanceEdgeType<Vertex,Vertex>( Vertex.class, Vertex.class);

If there is much other behaviour (we haven't defined any, and can't imagine much) in EdgeType, you could move the KNN- and Flickr-distance algorithms off to separate classes. 如果在EdgeType中还有许多其他行为(我们没有定义任何行为,也无法想象),您可以将KNN和Flickr距离算法移到单独的类中。 Probably no need. 可能没必要。

Remember: in OO, subclass for behaviour, not for existence. 记住:在OO中,行为的子类,而不是存在。 And give me a +1 vote! 并给我+1票!

Based on what you wrote about "feature", I would say that would be appropriate as a field in your ImageImageHyperEdge class. 根据你所写的关于“功能”的内容,我认为这适合作为ImageImageHyperEdge类中的一个字段。 You can create a FeatureType class with whatever various members that will define it. 您可以使用将定义它的各种成员创建FeatureType类。

public class ImageImageHyperedge implements
   HomogenousHyperedge<ImageVertex, Map<String,Instance>> {

   private FeatureType featureType;

   @Override
   public List<ImageVertex> searchNearstNeighborsVertex(Hypergraph hypergraph,
        ImageVertex vertex) {
      return null;
   }
}

Try something like this? 尝试这样的事情?

public abstract class BaseFeatureHyperEdge {

    @Override
   public List<ImageVertex> searchNearstNeighborsVertex(Hypergraph hypergraph,
        ImageVertex vertex) {   
      // implement nearest neighbor search algorithm
      // and call getFeatureType where you need it.
      // This will allow you to have the search algorithm only in one place. 
      //But make the search based on the feature type;
   }

   protected FeatureType getFeatureType();
}

public class FeatureOneHyperEdge extends BaseFeatureHyperEdge{

    @Override
    protected FeatureType getFeatureType() {
        return new FeatureTypeOne();
    }
}


public class FeatureTwoHyperEdge extends BaseFeatureHyperEdge{

    @Override
    protected FeatureType getFeatureType() {
        return new FeatureTypeTwo();
    }
}

Maybe you can try the Template Method Pattern ? 也许你可以尝试模板方法模式

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

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