简体   繁体   English

在UML中的某些派生类之间共享方法

[英]Sharing methods among some of the derived classes in UML

I am struggling with modeling a specific scenario. 我正在努力为特定的场景建模。 Let's say we have a super class and three subclasses. 假设我们有一个超类和三个子类。

The subclasses share a list of methods from the super class. 子类共享超类的方法列表。 However, there is one method which is needed to be shared by two out of three classes. 但是,有一种方法需要由三个类中的两个共享。 So, I thought of two scenarios: 因此,我想到了两种情况:

  1. Adding the method in the two subclasses. 在两个子类中添加方法。 If I do that though, I will have code duplication since the implementation of the method is same. 如果这样做的话,由于该方法的实现是相同的,因此我将进行代码重复。

  2. Keep the method in the super class to avoid code redundancy based on scenario one above. 将方法保留在超类中,以避免基于上述情况一的代码冗余。 However, wouldn't that allow the third class to inherit the method? 但是,这不会允许第三类继承该方法吗? Which defeats the purpose. 达不到目的。

So, any ideas on how to model such a scenario ? 那么,关于如何为这种情况建模的想法?

Neither of your two scenarios is particularly good. 您的两种情况都不是特别好。 The need to inherit a certain method in two out of three classes suggests that you are probably missing an abstract class in the middle, from which two classes inherit: 需要在三个类中的两个中继承某个方法,这表明您可能在中间缺少一个抽象类,其中两个类从该抽象类继承:

public class Top {
    public void method1() {}
    public void method2() {}
}
public abstract class AbstractTwoAndThree extends Top {
    public void method3() {}
}
public class Derived1 extends Top {
}
public class Derived2 extends AbstractTwoAndThree {
}
public class Derived3 extends AbstractTwoAndThree {
}

This way, all three Derived classes inherit method1 and method2 , while method3 is inherited only by Derived2 and Derived3 . 这样一来,所有这三个Derived类继承method1method2 ,而method3只被继承Derived2Derived3

Note: The reason I made the class in the middle abstract is to stress that it is intended to be inherited, and must not be used by itself. 注意:我在中间abstract该类的原因是为了强调该类旨在被继承,并且不能单独使用。 It is not necessary to add any abstract methods to it. 不必向其中添加任何抽象方法。

Comment: What if we need to create a sharing graph that is not a tree? 评论:如果我们需要创建不是树的共享图怎么办?

In situations like that you need multiple inheritance for implementation, which is not available in Java. 在这种情况下,您需要实现多个继承来实现,而Java中没有这种继承。 However, multiple inheritance of interfaces is available, which will let you declare the methods that you need only in the classes that must have them. 但是,可以使用接口的多个继承,这使您可以仅在必须具有它们的类中声明所需的方法。 Now you can offload the implementation into a package-private class, or put a default implementation into the interface, depending on the nature of the method: 现在,您可以将实现卸载到package-private类中,或者将默认实现放到接口中,具体取决于方法的性质:

public class Top {
    public void method1() {}
    public void method2() {}
}
public interface TwoAndThree {
    void method3() {}
}
public class Derived1 extends Top {
}
class TwoAndThreeHelper { // The class is not public
    public static void method3impl(TwoAndThree inst) {
        ... // Implement the method
    }
}
public class Derived2 extends Top implements TwoAndThree {
    public void method3() {
        // Call the shared implementation to avoid duplicating code
        TwoAndThreeHelper.method3impl(this);
    }
}
public class Derived3 extends Top implements TwoAndThree {
    public void method3() {
        // Call the shared implementation to avoid duplicating code
        TwoAndThreeHelper.method3impl(this);
    }
}

With pure UML, you are allowed to inherit from multiple classes at once. 使用纯UML,您可以一次从多个类继承。 That is, you may model a second superclass containing that method, from which only the two subclasses may inherit (in addition to the common superclass). 也就是说,您可以对包含该方法的第二个超类建模,只有这两个子类可以从中继承(除了公共超类之外)。

Reference: http://www.uml-diagrams.org/generalization.html#multiple-inheritance 参考: http : //www.uml-diagrams.org/generalization.html#multiple-inheritance

This becomes problematic when implementing your diagram in languages that disallow this, such as Java (where you have to choose either of your two solutions. But for the first solution, you could also add an interface, so that at least the method is declared in a central place). 当使用不允许这样做的语言(例如Java(必须选择两种解决方案之一))来实现图表时,这将成为问题。但是对于第一种解决方案,您还可以添加一个接口,以便至少在一个中心位置)。 But until then, on a conceptual level, you are fine :) 但是直到那时,从概念上讲,你还可以:)

This is what I'd come up with: 这是我想出的: 在此处输入图片说明

Just create a sub-class from Main and add method you need for your both classes. 只需从Main创建一个子类,然后添加两个类都需要的方法。

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

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