Java – Design Pattern For Util Classes and General Static Variables

design-patternsjava

I am developing a Mojo(think it as just a usual class) for my custom Maven Plugin. There is an Abstract Mojo at top. I have some other custom Mojos in example which makes server side includes. I have some static variables and some util methods in example get operating system name, fix file separator in order to used operating system etc. I have created an abstract Base Mojo that extends Abstract Mojo and put every common things inside it. When I create a Mojo (i.e. server side includer) I extend it from my Base Mojo. However should I think that I should separate variables and util methods or not and what kind of pattern I should follow.

To make it clear there is:

  • AbstractMojo at top by default (I can not change it)
  • BaseMojo an abstract class that extends AbstractMojo that has util methods and static system variables
  • CustomMojo extends BaseMojo and does its responsibility.

Any ideas how to implement a design for my needs?

Best Answer

I'm wondering, why this was migrated. But anyway:

You shouldn't put everything in a base mojo. That practically makes reusing that code impossible. Just think of the mojo growing and you need to split that mojo up into separate classes. You can't do that if that code has a dependency on the base class of the mojo.

You should create services that provide the functionality you currently have in your base mojo. They are reusable and therefore accessible from other places.

As a general rule of thumb:
YAGNI - You aint gonna need a member declaration in a class that is only intendend to be used by deriving classes. How do you know that it is really used?