简体   繁体   English

Java:使用两个不同枚举的具有几乎相同方法的两个类; 可以避免冗余吗? (到处都是静态方法)

[英]Java: Two classes with nearly identical methods using two different enums; possible to avoid redundancy? (static methods everywhere)

I have two enums describing two UML profiles (meaning they define stereotypes that the profiles contain).我有两个枚举描述了两个 UML 配置文件(意味着它们定义了配置文件包含的原型)。

I also have two utility classes featuring nearly identical methods working on each of the profiles.我还有两个实用程序类,它们具有在每个配置文件上工作的几乎相同的方法。

Example:例子:

public static List<Element> getStereotypedElements(final InsertProfileHere stereo, final Package pkg) {
    List<Element> extendedElements = new ArrayList<Element>();      
    if (isProfileApplied(pkg)) {
        if (hasStereotype(stereo, pkg)) {
            extendedElements.add(pkg);
        }
        extendedElements.addAll(getStereotypedElements(stereo, pkg.allOwnedElements()));
    }
    return extendedElements;
}

,where InsertProfileHere can be replaced with each of the two profile enums. , 其中InsertProfileHere可以替换为两个配置文件枚举中的每一个。

If anyone is interested, this method uses the Eclipse Modeling Framework or rather the UML2 metamodel implementation in EMF.如果有人感兴趣,此方法使用 Eclipse 建模框架,或者更确切地说是 EMF 中的 UML2 元模型实现。

Anyway, I want to merge the two utility classes to avoid redundant code.无论如何,我想合并这两个实用程序类以避免冗余代码。

I've tried:我试过了:

  • a super interface for the two profiles两个配置文件的超级界面
    • didn't work because of static methods由于 static 方法而无法正常工作
  • an abstract class for the Utility classes实用程序类的抽象 class
    • didn't work because of static methods由于 static 方法而无法正常工作
  • encapsulating the profile enums in a class将配置文件枚举封装在 class 中

Each didn't work for one or another reason.由于某种原因,每种方法都不起作用。

Anyone got any ideas?有人有什么想法吗?

EDIT:编辑:

An example for another utility method:另一种实用方法的示例:

public static boolean hasStereotype(
    final InsertProfileHere stereo, final Element elem) {
    for (Stereotype appliedStereo : elem.getAppliedStereotypes()) {
        if (stereo == null) {
            if (InsertProfileHere.contains(appliedStereo)) {
                return true;
            }
        } else if (stereo.isEqual(appliedStereo)) {
            return true;
        }
    }
    return false;
}

EDIT2: And for good measure part of the implementation of the profile enum EDIT2:并且为了很好地衡量配置文件枚举的实施部分

public enum Profile1 { 

STEREOTYPE1 ("readable stereotype1 name"),
STEREOTYPE2 ("readable stereotype2 name"),
STEREOTYPE3 ("readable stereotype3 name"),

public static final String PROFILE_NAME = "NameOfProfile";

private final String readableName;

private Profile1(final String newName) {
    readableName = newName;
}

public static Profile1 getValue(final String name) {
    for (Profile1 type : Profile1.values()) {
        if (type.toString().equals(name)) {
            return type;
        }
    }
    return null;
}

public static boolean contains(Stereotype stereotype) {
    return (stereotype.getProfile().
            getDefinition().getNsURI().contains(PROFILE_NAME));
}

Let the two enums implement a common interface, and use this interface in the (now unified) utility class.让两个枚举实现一个通用接口,并在(现在统一的)实用程序 class 中使用这个接口。 Or, better yet, move the methods from the utility class to the interface.或者,更好的是,将方法从实用程序 class 移至接口。

interface Stereotype {
  public boolean hasStereotype(Package pkg);
  public List<Element> getStereotypedElementes(Package pkg);
}

 enum Enum1 implements Stereotype {

   FOO("com.foo"), BAR("com.bar");

   Enum1(String packagename) {
     this.packagename=packagename;
   }

   private String packagename;

  @Override
  public boolean hasStereotype(Package pkg) {
    return pkg.getName().equals(packagename);
  }

  @Override
  public List<Element> getStereotypedElementes(Package pkg) {
    ...
  }

}

 enum Enum2 implements Stereotype {
   ...
 }


public class Foo {
  static List<Element> getStereotypedElements(final Stereotype stereo, final Package pkg) {
    List<Element> extendedElements = new ArrayList<Element>();      
    if (isProfileApplied(pkg)) {
        if (stereo.hasStereotype(pkg)) {
            extendedElements.add(pkg);
        }
        extendedElements.addAll(stereo.getStereotypedElements(pkg.allOwnedElements()));
    }
    return extendedElements;
  }
}

Use common interface as in example below.使用通用接口,如下例所示。

import java.awt.Color;

public class EnumeInteraceTest
{
    public interface ICorolsEnum
    {
        public Color getColor();
    }
    public enum Colors1 implements ICorolsEnum
    {
        MAGENTA(Color.MAGENTA),
        PINK(Color.PINK);
        private Color color;

        private Colors1(Color color)
        {
            this.color = color;
        }

        @Override
        public Color getColor()
        {
            return color;
        }
    }
    public enum Colors2 implements ICorolsEnum
    {
        GREEN(Color.GREEN),
        BLUE(Color.BLUE);
        private Color color;

        private Colors2(Color color)
        {
            this.color = color;
        }

        @Override
        public Color getColor()
        {
            return color;
        }
    }

    public static void main(String[] args)
    {
        ICorolsEnum ice1 = Colors1.MAGENTA;
        System.out.println(ice1.getColor());
        ICorolsEnum ice2 = Colors2.GREEN;
        System.out.println(ice2.getColor());
    }
}

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

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