基类定义了许多受保护的方法:这是一个好的OOP设计吗?

发布于 2024-09-15 23:48:08 字数 738 浏览 2 评论 0原文

我编写了一个基类,它定义了许多受保护的方法。这些方法在其子类中调用。 这些方法定义了其子类的基本操作。 例如:

class Base{
   protected void foo(){}
   protected void bar(){}
}

class Sub1 extends Base{//The sub class only needs Base.foo()
   public void po(){
     ...
     foo();
     ...
   }
}

class Sub2 extends Base{//The sub class only needs Base.bar()
   public void ko(){
     ...
     bar();
     ...
   }
}

class Sub3 extends Base{//The sub class needs both Base.bar() and Base.foo()
   public void lo(){
     ...
     bar();
     ...
     foo();
   }
}

我只是想知道这是否是一个好的OOP设计?阅读源代码,我们知道 Sub1 根本不需要 Base.bar()Sub2 不需要 Base。 foo() 根本就没有。我认为这有点多余。但我不知道更好的解决方案,有人可以给一些建议吗?谢谢!

I wrote a base class which defined many protected methods. Those methods are called in its sub classes.
The methods define basic operations for its sub classes.
For instance:

class Base{
   protected void foo(){}
   protected void bar(){}
}

class Sub1 extends Base{//The sub class only needs Base.foo()
   public void po(){
     ...
     foo();
     ...
   }
}

class Sub2 extends Base{//The sub class only needs Base.bar()
   public void ko(){
     ...
     bar();
     ...
   }
}

class Sub3 extends Base{//The sub class needs both Base.bar() and Base.foo()
   public void lo(){
     ...
     bar();
     ...
     foo();
   }
}

I am just wondering if it is a good OOP design? Read the source, we know Sub1 doesn't need Base.bar() at all, Sub2 doesn't need Base.foo() at all. It's sort of redundant I think. But I don't know better solution, anyone could give some advice? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉栀 2024-09-22 23:48:08

一般来说,您应该在设计中避免此类对象依赖性。如果 foo() 和 bar() 的功能在派生类中没有改变,您可能希望将其放入外部类中并使用该外部类

class Base{

}

class Helper1  {
   public void foo(){}
}

class Helper2  {
   public void bar(){}
}

class Sub1 extends Base{
   private Helper1 a = new Helper1();
   private Helper2 b = new Helper2();

   public void po(){
     ...
     a.foo();
     ...
     b.bar();
   }
}

class Sub2 extends Base{
   private Helper2 b = new Helper2();

   public void ko(){
     ...
     b.bar();
     ...
   }
}

:酒吧的例子看起来不太好。您的问题可能是对对象的责任分配不当或继承的误用。发布真实的代码将有助于编写更好的答案。

Generally you should avoid these kind of object dependencies in your design. If the functionality of foo() and bar() doesn't change in derived classes, you might want to put it in an outer class and use that one instead:

class Base{

}

class Helper1  {
   public void foo(){}
}

class Helper2  {
   public void bar(){}
}

class Sub1 extends Base{
   private Helper1 a = new Helper1();
   private Helper2 b = new Helper2();

   public void po(){
     ...
     a.foo();
     ...
     b.bar();
   }
}

class Sub2 extends Base{
   private Helper2 b = new Helper2();

   public void ko(){
     ...
     b.bar();
     ...
   }
}

This foo & bar example does not look well. Your problem might be a bad assignment of responsibility to the objects or misuse of inheritance. Posting the real code would help to write better answers.

浅紫色的梦幻 2024-09-22 23:48:08

抱歉,但你思考这个问题的方式是错误的。问题不是“sub2 应该继承 base,它不需要那个方法”

问题应该是“sub2 是 Base 吗”,例如青蛙是动物吗?是的,青蛙可以继承 Animal,但青蛙不应该继承 Mammal。

如果 sub2 是一个基础,那么你就走在正确的轨道上,如果基础只是可能有用的函数的集合,那么就有问题了。

Apologies, but you're thinking about this the wrong way. The question isn't "Should sub2 inherit base, it doesn't need that method"

The question should be "Is sub2 a Base" e.g Is frog an Animal? Yes, frog can inherit Animal, but frog should not inherit Mammal.

If sub2 is a base, you're on the right track, if base is just a collection of functions that may be useful, then something is wrong.

许一世地老天荒 2024-09-22 23:48:08

我的想法

1-如果您是第一次设计,请尝试遵循依赖注入原则,因为我可以清楚地看到您正在不同的子类中创建对象 Helper1 、 Helper2 并且也可能重复代码。

2-如果您不需要 helper1 和 helper 2 的不同实例,我建议在您的基类中创建 Helper1 和 Helper 2 作为属性,或者可能将它们设为虚拟,以便您可以在需要时覆盖它们。

3-您直接编写实现,并且您的客户端代码直接依赖于使用 Interfaces 的具体类,这将使您的类更具可测试性。

4-这是黄金法则:如果你正在处理的问题不是那么复杂,并且你看不到在不久的将来改变它们的理由,而不是继续使用你正在做的事情,那就不要让你的生活变得复杂。

所有好的 oop 编程都是好的,但是如果你看到一个比实现更简单的解决方案更好、更简单的解决方案,因为使用抽象,你就付出了复杂性的代价。

还有一个更重要的规则:看看您是否可以在不更改设计的情况下轻松测试您的类设计,而不是让您的设计步入正轨。

My thoughts

1- If your designing for the first time, try to follow Principle of Dependecy injection as i can clearly see that you are creating objects Helper1 , Helper2 in different sub classes and may be duplicating code also.

2- I would suggest that Create Helper1 and Helper 2 as properties in your base class if you don't need different instances of helper1 and helper 2 or posibly make them Virtual so that you can overrite them if needed.

3- you are writing directly to the implementation and your client code is directly depends upon the concrete classes use Interfaces , it will make your classes more testable.

4- AND HERE IS THE GOLDEN RULE : If the problem you are dealing is not so much complicated and you don't see a reason for changing them in near future than coninue using what you are doing , DON'T MAKE YOUR LIFE COMPLICATED.

ALL good oop progrmming is good but if you see a better and simpler solution than implement simpler one becoz for using ABSTRACTION you pay the price of complexity.

one more important rule : See if you eaisly test your class design without making changes in your design than your design is on right track.

拧巴小姐 2024-09-22 23:48:08

我发现这是 SOLID 规则之一接口隔离原则问题。

如果您的子类不需要所有基类函数,您可以将基类拆分为更具体的基类或接口

I see that's a Interface segregation Principle issue one of SOLID rules.

if your subClasses dosen't need all base class function you may split the base class to more specific base classes or interfaces

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文