开发者

Non-AOP Best Practices for Application-Wide Singletons

[Java]

I have several utility classes that will appear all over my code (LogHelpers, Exception Handlers, CacheHelpers, etc.) that all represent "cross-cutting concerns" and would be ideal candidates for AOP except... I don't have the time to learn AOP frameworks like AspectJ or AOP Alliance or Google Guice.

Having said that, I'd still like my code to be as neat and clean and in keeping with best-practices (non-AOP best practices that is!) as possible.

I have two problems that probably require two solutions. The first is that I have a few of these universal/highly-coupled/AOP-candidate classes that just require static methods; the classes themselves don't have any non-static/instance-level methods. Thing开发者_如何学Gos like ExceptionHandler.handleException(Exception exc).

The second problem is that I have a few of these universal/AOP-candidate classes that do have method instances, and do need to be instantiated, but are probably best served as singletons, where my application creates one "global" instance of them and the same reference is used throughout the other packages and codebase.

Sooo...

My question is: what are the best (non-AOP) design patterns & practices for classes that:

(a) Only define static methods consumed all over the codebase; and (b) Only need to one single instance that will be consumed all over the codebase?

Singleton pattern? Factory? Static imports? Other?!?!

Thanks for any suggestions here!


Inversion of Control (probably using an IoC Container library that supports lifetime management) and constructor dependency injection of interfaces.

If you have to you could use the Service Locator pattern but I'd avoid that.

Note that your "Singletons" are just POJOs. The IoC Container takes care of insuring one and only one (if you set container-controlled lifetime or some such).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜