Kuldeep
Kuldeep

Reputation: 13

Share One common variable inside my Swing Application

I want to share One common variable inside my Swing Application throughout the application life cycle. It should be same Application Context in Servlets .What are the ways i can achieve it?

Upvotes: 0

Views: 113

Answers (1)

rimero
rimero

Reputation: 2383

You could use some kind of singleton, even though I would never recommend singletons in general.

You can also use an IOC container in your Swing application or pass references yourself as needed.

Like @Guillaume said, it doesn't look good in terms of design, but you have options.

You could also use static variables and refactor the code as soon as you understand a bit more the implications of your original design approach, and before the code base grows significantly.

When you start introducing quickly many global objects without rethinking your design, it can introduce issues that you'll not be able to fix for many reasons:

  • Too much code and too much tight coupling
  • Weird concurrency issues depending on what your code does
  • Lack of flexibility if someday you need many instances of those global objects.

Upvotes: 1

Related Questions