0

this is more of an architectural question. I'd like to have a global object in my project which is accessible to every class, that needs it but without creating an instance of it every time. The object itself is a composition of different services. My first approach was to define an interface in which the object is instantiated and then can be injected into each class by just saying implements. What I want to know is, if this approach is clean or kinda hacky or really gross. Here is my implementation so far:

public final class SystemServices
{
    private final SecurityService securityService;
    private final PersistencyService persistencyService;
    private final RecordService recordService;
    private final DispatcherService dispatcherService;

    private SystemServices(Builder builder)
    {
        this.securityService = builder.securityService;
        this.persistencyService = builder.persistencyService;
        this.recordService = builder.recordService;
        this.dispatcherService = builder.dispatcherService;
    }

    public SecurityService getSecurityService()
    {
        return securityService;
    }

    public PersistencyService getPersistencyService()
    {
        return persistencyService;
    }

    public RecordService getRecordService()
    {
        return recordService;
    }

    public DispatcherService getDispatcherService()
    {
        return dispatcherService;
    }

    public static class Builder
    {
        private SecurityService securityService;
        private PersistencyService persistencyService;
        private RecordService recordService;
        private DispatcherService dispatcherService;

        public Builder setSecurityService(SecurityService securityService)
        {
            this.securityService = securityService;
            return this;
        }

        public Builder setPersistencyService(PersistencyService persistencyService)
        {
            this.persistencyService = persistencyService;
            return this;
        }

        public Builder setRecordService(RecordService recordService)
        {
            this.recordService = recordService;
            return this;
        }

        public Builder setDispatcherService(DispatcherService dispatcherService)
        {
            this.dispatcherService = dispatcherService;
            return this;
        }

        public SystemServices build()
        {
            return new SystemServices(this);
        }
    }
}

And this is the interface in which an instance of SystemServices is created:

public interface ServiceProvider
{
    public static SystemServices systemServices = new SystemServices.Builder()
            .setSecurityService(new SecurityService())
            .setPersistencyService(new PersistencyService(new BlackBoxDb(BlackboxApplication.getAppContext())))
            .setRecordService(new RecordService()).setDispatcherService(new DispatcherService()).build();
}

Now I can access the object in every class by just using systemServices.getSecurityService.doSomethingSecurityRelated()

The code works and seems too nice for me as a beginner, but I'm sure there is something ugly with that approach. So any criticism is appreciated :) Also it would be interesting how the JVM handles Interfaces. Is it really just one single(ton) object or does it create one object for each class?

2 Answers 2

1

The JVM would handle the interface just like you want, there will be only one SystemServices created. But the approach you describe is indeed considered 'ugly'. The best way I think would be to use a dependency injection framework like Spring. It is very good at just what you need- having global objects that other classes can access. It also makes testing much easier, and also enables you to quickly change what global objects should be used.

0

I wouldn't say it's ugly or hacky. It's more unusual and unidiomatic.

It actually seems like a pretty nice, lightweight, way to get dependencies injected into all your classes. You do have to recompile the interface to change the implementations, which in theory is bad, but in practice, and depending on your requirements, may not matter. For example, it will make unit testing difficult as your ServiceProvider interface would have to be rewritten to provide a mock SecurityService.

I do notice that you already seem to be depending on spring, as in BlackboxApplication.getAppContext(). Why not make all the services available on spring context? Is it because you want to make them available to classes that aren't managed by spring? With the annotation based loading up of the spring context, I don't see any big overhead, and spring does buy you a lot of nice infrastructure code.

I could be wrong about you already using spring. If you aren't, I see nothing wrong with your approach. It did surprise me at first! But that's kind of cool.

3
  • First of all: thanks! It feels good to get a positive feedback, even though its unidiomatic. Well, since this is an Android Application, Spring will be pretty useless here. All classes mentioned are self implementations (maybe I should have added the import statements to clarify this). A maybe more "idiomatic" approach which comes to my mind would be the classic Singleton pattern, but I am not sure if it may collide with the already implemented Builder pattern. Aug 28, 2014 at 19:01
  • True, a singleton that you import and "instantiate" in all the classes that need the services would accomplish the same thing. There do seem to be some dependency injection frameworks for android: square.github.io/dagger Aug 28, 2014 at 19:06
  • Yes, after a little googling I just found out, that there is also a Spring version for Android, but still for my pretty "small" app, its not necessary. I will post the singleton approach tomorrow and hopefully get some feedback since I do not have experience with singleton and especially combining it with the builder patter at all. Aug 28, 2014 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.