samedi 29 juillet 2017

Should I use abstract class or Util class?

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:

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

Now, the issue is that in future we expect other implementations of interface A to use method 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.

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

class B extends C {

}

Or create a Utils class and let B to still implement 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. Which approach should be more preferred here and why?
3. Any other suggestion behind the thought process is always welcomed.

Thank for your time.

Aucun commentaire:

Enregistrer un commentaire