Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,556,436
Community Members
 
Community Events
184
Community Groups

Groovy: How to access a common object without injecting it into each class

I am doing a lot of work with Script Runner and I have decided to build out a semi-framework.

One of my objects is a Logger Object that helps to enforce logging consistency across all the other objects.

public class LogThis
{
// Properties
def log
def errorLevels = [
debug: false,
info : true,
error: true
]
}

In this case, using this class, all info and error messages are displayed, but debug is not.  To turn on debug:

logger.enableDebug()

Is there a way, or is it bad practice in Groovy to make that class a global class that is accessible from inside of objects?

Currently I inject the log class into each object I instantiate:

(new Assignees(log))
.setIssue(issue)
.setNewAssignee(newAssignee)
.commitChange()

This is a sample of calling a class that will change the assignee on an issue.  I am injecting the log object into the Assignees object.

I find that very, very repetitive, and not expandable.  If I need to start injecting another object, and another object, my class signature start to get unweildy at some point.

 

My inclination is to create some form of Dependency Injection Container (or IoC Container, etc, etc), but I have no idea if Groovy can handle that, or if it's even a Best Practice in Groovy.

This is kind of an "advanced" topic, but hopefully somebody wants to geek out on this subject :-)

Thanks in advance.

 

1 answer

1 accepted

0 votes
Answer accepted
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 01, 2019

You could use the singleton design pattern: https://en.wikipedia.org/wiki/Singleton_pattern

Instead of injecting the logger into each class, the class could obtain it by calling the "getInstace()" method or just doing it the Groovy way: "Logger.instance".

Thanks,

I just did not think of using a singleton in groovy at all.  Not sure why, maybe because singleton is such an anti pattern in my home language of PHP, that it just fell off the radar. 

Now I get to refactor a dozen or so scripts ... 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events