简体   繁体   English

深层嵌套继承 - 糟糕或良好的实践?

[英]Deeply Nested Inheritance - Bad or Good Practice?

I am in the process of making a PHP web application. 我正在制作PHP Web应用程序。 I have a situation that I believe would foster a good time for nested inheritance. 我有一种情况, 我认为这将为嵌套继承创造一个美好时光。 Anyway, here is my situation: 无论如何,这是我的情况:

public class RecurringWeeklyEvent extends RecurringEvent { }

public class RecurringEvent extends Event { }

It does not seem to me that this would be a bad design practice; 在我看来,这不是一个糟糕的设计实践; however, I am not an advanced Object-Oriented programmer by any means. 但是,无论如何,我不是一个先进的面向对象程序员。 With that said, before I venture off using this kind of code in my application, I would like to know if this is a good or bad practice from more experienced/qualified programmers. 话虽如此,在我冒昧地在我的应用程序中使用这种代码之前,我想知道这是来自更有经验/合格的程序员的好或坏做法。

NOTE: I changed the title from multiple inheritance to nested inheritance after being corrected of using the wrong term. 注意:在使用错误的术语进行更正后,我将标题从多重继承更改为嵌套继承。

Thanks 谢谢

Steve 史蒂夫

To quote David West : 引用David West

The definition of inheritance then becomes, "A superordinate-subordinate relationship between classes in which the subordinate (child) has the same behaviours (responsibilities) as the superordinate (parent) plus at least one additional." 然后,继承的定义变为“类别之间的上级 - 从属关系,其中下级(子)与上级(父级)具有相同的行为(责任)以及至少一个附加。”

So what you're doing seems fine, so long as you're adding behaviour to your derived classes (ie, inheritance is not a tool for adding in extra members), and (preferably) not altering the defined behaviour of the bases classes. 所以你所做的似乎很好,只要你在派生类中添加行为 (即,继承不是添加额外成员的工具),并且(最好)不改变基类的定义行为。

Elaboration

What duffymo says is correct. duffymo说的是正确的。 I want to add my 2p from my interpretation of what David West says in his book. 我想从我对David West在他的书中所说的内容的解释中加入我的2p。

Overriding methods should be avoided where possible. 应尽可能避免使用覆盖方法。 Changing the behaviour of a method in a child class could lead to confusion for implementers. 更改子类中方法的行为可能会导致实现者的混淆。

(I'm still getting my head around OOP and Object Thinking.) (我仍然围绕着OOP和对象思维。)

The best way, would be to have the RecurringEvent, and each object has information on when it recurs, probably every n days (or even hours, minutes). 最好的方法是使用RecurringEvent,每个对象都有关于它何时复发的信息,可能每n天(甚至几小时,几分钟)。

From these two lines, I'm guessing you're making a calendar? 从这两行来看,我猜你正在制作日历? You will probably want to make a different class (sub class of recurring event) for events that recur in non standard ways (for example, third monday of the week, first monday in October, and the like). 您可能希望为以非标准方式重复的事件(例如,一周中的第三个星期一,十月的第一个星期一等)创建不同的类(重复事件的子类)。

And just so you know, this isn't multiple inheritance, multiple inheritance is when a single class has more than one direct parent class. 而且你知道,这不是多重继承,多重继承就是当一个类有多个直接父类时。 In this case, RecurringWeeklyEvent extends RecurringEvent which extends Event. 在这种情况下,RecurringWeeklyEvent扩展了RecurringEvent,它扩展了Event。 With multiple inheritance, it would be RecurrungWeeklyEvent extends RecurringEvent and Event (I'm not sure the syntax to do this in php or java). 有了多重继承,它将是RecurrungWeeklyEvent扩展RecurringEvent和Event(我不确定在php或java中执行此操作的语法)。

multiple inheritance is actually something a bit different. 多重继承实际上有点不同。 Multiple inheritance is where your class inherits from multiple interfaces / superclasses. 多继承是您的类从多个接口/超类继承的地方。 What you have is a regular run-of-them-mill class hierarchy which I believe is perfectly fine. 你所拥有的是一个常规的磨合级别的层次结构,我相信这是非常好的。 I find that sometimes one can get carried away with creating overly complex object hierarchies, so just be aware of that. 我发现有时人们会因为创建过于复杂的对象层次结构而感到厌烦,所以请注意这一点。 Your design looks acceptable to me though. 不过,你的设计看起来很可以接受。

I wouldn't call it nested inheritance, just 'Deep inheritance tree'. 我不会把它称为嵌套继承,只是'深度继承树'。

Looking at your example I wouldn't create a WeeklyRecurringEvent . 看看你的例子,我不会创建WeeklyRecurringEvent It is inflexible for extension if you add more time steps (you then would create classes TwoWeeklyRecurringEvent , MonthlyRecurringEvent ), which gets confusing and end up with a lot of dedicated classes. 如果你添加更多的时间步骤(然后你会创建类TwoWeeklyRecurringEventMonthlyRecurringEvent ),它对于扩展是不灵活的,这会让人感到困惑并最终得到很多专用类。 For that I would skip inheritance and a more composed approach: 为此我会跳过继承和更复杂的方法:

The last time I wrote PHP is a while ago, I just present the idea in Java, should be enough to get the idea: 我上一次写PHP是不久前,我只是用Java来表达这个想法,应该足以得到这个想法:


class RecurringEvent extends Event{
   RecurringStep step;

   RecurringEvent(RecurringStep step){
     this.step=step;
   }
}

enum RecurringStep{
  MONTHLY,WEEKLY,DAILY,YEARLY;
}

For making it easier to create recurring events inside code I maybe would also offer factory methods: 为了更容易在代码中创建重复事件,我可能还会提供工厂方法:


class RecurringEventFactory{
...
  static createDailyEvent(){
     return new RecurringEvent(DAILY);
  }
...
}

Of course you could encapsulate factory methods directly to the RecurringEvent class. 当然,您可以将工厂方法直接封装到RecurringEvent类。

I would call it nested inheritance maybe. 我可以称之为嵌套继承。 Either way don't try to find a problem here. 无论哪种方式都不要试图在这里找到问题。 You just stop nesting inheritance when you don't feel comfortable with it anymore. 当你感觉不舒服时,你就停止嵌套继承。

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

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