Why Node.js is a good choice for your next web app

You might have noticed the big surge in Node.js’ popularity in the recent year or two. Why is that so? Why is it getting so popular despite well-tried languages like PHP or Python?

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Dependency injection in Jenetics

The obvious solution would be to inject the needed Random engine as additional constructor- or method parameter. Beside the additional clutter, introduced by this DI method, it is also not very flexible. Since I didn’t know in advance where the Random engine might be used, I would have been forced to add an additional parameter on virtually every overridable method.

The injection can also be performed via static fields or methods. Using static dependency injection is nowadays considered as design fault, unjustly so in my opinion. If done correctly and in the right environment, it can be a simple method for writing flexible code.

Jenetics uses the RandomRegistry class for statically injecting the Random engine.

The method setRandom allows to set the desired Random engine and the getRandom method allows the client code to fetch the currently set PRNG. It is clear that this two methods are thread safe.

The short code example above shows how the RandomRegistry is used. The value of the RndVal class is initialized with a random double value created by the currently set Random engine. If you need a reproducible RndVal object, it is possible to replace the current Random engine with an instance you can control. After the object has been created, the original Random is restored.

Besides the verbosity of the code, it also has a serious flaw: it changes the Random engine globally and not only for the creation of the RndVal object. This is what the using method is for. It allows to change the Random engine only locally, without effecting the globally configured Random engine.

This method guarantees that the RndVal, created within the using block, uses the Random(123) object and all other RandomRegistry users are working with the globally defined Random engine.

Conclusion

For the Jenetics library it turns out that the static Random engine injection works perfectly fine. The global synchronization point of the getRandom method isn’t a problem in this environment, since it is only called a couple of times per generation. As I mentioned it at the beginning, know your environment. There is only one additional drawback I can see with static DI: the configured Random engine is only global per class loader. Two different class loader open two different scopes for the RandomRegistry.

Details about the implementation is given in the following post.

Add a comment

Related posts:

I Compliment 6 Strangers Every Day.

Why strangers? Because I see people who need it. I live in New York City, where lots of people could use a bright spot in their day, especially during their commute. Today while on my way to my…

New evidence that lobbying affects legislative outcomes

As state legislatures across the country wrap their annual sessions, commentators are trying to explain why some bills passed while others failed. A common element in these explanations is lobbying —…

Understanding IP Fragmentation

Fragmentation occurs when an IP datagram traverses a network which has a maximum transmission unit (MTU) that is smaller than the size of the datagram. Take for example a standard Ethernet datagram…