Butterfly DI Container - Global and Local Factories

Jakob Jenkov
Last update: 2014-05-25

In Butterfly Container there is a difference between local and global factories. As you saw earlier, a factory definition (AKA factory chain) is often translated into a chain of factories. Normally, a factory definition is broken into factories like this:

  1. One global factory, which can be referenced from other global factories (other factory definitions).

  2. A chain of local factories which can only be referenced by factories within the same global factory defintion the local factories are part of.

Here is an example factory definition:

global1 = * com.jenkov.MyObject().getName();

And here is how the factory chain/graph looks inside the container (GF = Global Factory, LF = Local Factory):

If a global factory references another global factory, these two global factories are linked too, inside the container. Here is a configuration example and the corresponding factory graph:


global1 = * com.jenkov.MyObject();
global2 = * global1.getName();


When an instance is obtained from a factory, the factory's instance() method is called. This is what you see happening in the factory graph for global2: GF(global1.instance()).

Replaceable Global Factories

One of the design goals for Butterfly Container was to enable runtime replacement of factories. This is not so easy, however, when all the factories are linked together inside the cotainer.

To solve this problem a factory proxy was introduced which encapsulates the global factories. That way the concete global factory inside the global factory proxy can be replaced at runtime. Therefore, the object graph from the previous section really looks like this:

As you can see, the local factory calling the global1 factory is really linked to global1's factory proxy. If the definition (factory chain) on the inside of this proxy is replaced with a new, this will have effect for all other factories linked to it.

Jakob Jenkov

Featured Videos

P2P Networks Introduction




















Advertisements

High-Performance
Java Persistence
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next