我应该使用抽象类还是Util类?

时间:2022-11-25 19:34:03

I had a scenario like below

我有一个像下面这样的场景

interace A {

}

class B implements A {
}

Now during making changes, I realized that I have to extract out a repeating piece of code into a method.

现在在进行更改时,我意识到我必须将一段重复的代码提取到一个方法中。

say there is a method like below in class B:

说B类中有如下方法:

private void domSomething() {
 //sequence of steps
}

Now, the issue is that in future we expect other implementations of interface A to use method doSomething().

现在,问题在于,将来我们希望接口A的其他实现使用方法doSomething()。

So the dilemma here is should method doSomething() be moved to a util class or we should create an abstract class with a protected doSomething() method. Something like below.

所以这里的困境是应该将doSomething()方法移动到util类,还是应该使用受保护的doSomething()方法创建一个抽象类。像下面的东西。

abstract class C implements A {
 protected void doSomething();
}

class B extends C {

}

Or create a Utils class and let B to still implement A.

或者创建一个Utils类,让B仍然实现A.

Generally, I like to refrain from using abstract and look for ways to avoid them. So that leads to some questions here:

一般来说,我喜欢不使用抽象并寻找避免它们的方法。所以这会引出一些问题:

  1. Is that right way to think about abstract classes?
  2. 这是考虑抽象类的正确方法吗?

  3. Which approach should be more preferred here and why?
  4. 这里应该更倾向于哪种方法,为什么?

  5. Any other suggestion behind the thought process is always welcomed.
  6. 思想过程背后的任何其他建议总是受到欢迎。

3 个解决方案

#1


1  

I would preffer going for "util class" (but not static! Just another class defining a common behaviour). In another words preffer "composition over inheritance".

我会考虑使用“util class”(但不是静态的!只是另一个定义常见行为的类)。换句话说,就是“继承权的构成”。

More info why here:

更多信息为什么这里:

Prefer composition over inheritance?

喜欢构成而不是继承?

#2


0  

private void domSomething() does not return value, that mean method update some state, it can be global state (database or external resources) or it can be internal state of class this method exists in.

private void domSomething()不返回值,即表示方法更新某些状态,它可以是全局状态(数据库或外部资源),也可以是此方法存在于类的内部状态。

If method updates global state based on internal members of class, then it should be moved to another class and all required data passed in as parameter

如果方法基于类的内部成员更新全局状态,则应将其移动到另一个类,并将所有必需的数据作为参数传入

public class Util
{
    public void DoSomething(value1, value2) {}
}

If method updates internal state of class then it can be placed to abstract class, so all derived classes have access to it.

如果方法更新了类的内部状态,那么它可以放在抽象类中,因此所有派生类都可以访问它。

public abstract class A
{
    protected void DoSomething() {}
}

Make method virtual/overridable if you want give derived classes possibility to change it's behaviour.

如果您希望为派生类提供更改其行为的可能性,请将方法设置为virtual / overridable。

#3


0  

Use abstract classes when you are modeling some hierarchy in your classes, move common logic to parents, more over try to prefer composition over inheritance

在为类中的某些层次结构建模时使用抽象类,将常用逻辑移动到父级,更多地尝试使用组合而不是继承

And use static util/helper classes when logic is very generic and can be used by multiple classes, e.g FileUtils.createTmpFile

当逻辑非常通用并且可以被多个类使用时,使用静态util / helper类,例如FileUtils.createTmpFile

#1


1  

I would preffer going for "util class" (but not static! Just another class defining a common behaviour). In another words preffer "composition over inheritance".

我会考虑使用“util class”(但不是静态的!只是另一个定义常见行为的类)。换句话说,就是“继承权的构成”。

More info why here:

更多信息为什么这里:

Prefer composition over inheritance?

喜欢构成而不是继承?

#2


0  

private void domSomething() does not return value, that mean method update some state, it can be global state (database or external resources) or it can be internal state of class this method exists in.

private void domSomething()不返回值,即表示方法更新某些状态,它可以是全局状态(数据库或外部资源),也可以是此方法存在于类的内部状态。

If method updates global state based on internal members of class, then it should be moved to another class and all required data passed in as parameter

如果方法基于类的内部成员更新全局状态,则应将其移动到另一个类,并将所有必需的数据作为参数传入

public class Util
{
    public void DoSomething(value1, value2) {}
}

If method updates internal state of class then it can be placed to abstract class, so all derived classes have access to it.

如果方法更新了类的内部状态,那么它可以放在抽象类中,因此所有派生类都可以访问它。

public abstract class A
{
    protected void DoSomething() {}
}

Make method virtual/overridable if you want give derived classes possibility to change it's behaviour.

如果您希望为派生类提供更改其行为的可能性,请将方法设置为virtual / overridable。

#3


0  

Use abstract classes when you are modeling some hierarchy in your classes, move common logic to parents, more over try to prefer composition over inheritance

在为类中的某些层次结构建模时使用抽象类,将常用逻辑移动到父级,更多地尝试使用组合而不是继承

And use static util/helper classes when logic is very generic and can be used by multiple classes, e.g FileUtils.createTmpFile

当逻辑非常通用并且可以被多个类使用时,使用静态util / helper类,例如FileUtils.createTmpFile