Thursday, July 3, 2014

What is the injection method? The injection method simply means that someone from the outside can c


Niklas from Wide Circle (one of my few Nickname here on the blog and much appreciated :)) told me about a site where a guy went through a little endothermic "fulhack" you can use if you do not have time to pursue all the OOP rules (and the have sometimes!). The page you can see here: http://www.untoldentertainment.com/blog/2010/03/01/5-ways-to-cheat-at-actionscript
Ie. breaking one of the main pillars of OOP (encapsulation) by using the method. I argue that he is completely wrong! The injection method is one of the BEST ways to maintain encapsulation when you need to use external classes or methods.
There are two ways to maintain encapsulation by injection method. One is to use a callback function that is injected into the class, and the other is to use an interface that another class may implement, and then injected.
What is the injection method? The injection method simply means that someone from the outside can change the behavior in a class by specifying which values, classes, instances, and so on. it will use internally. There are a number of different injection methods (number depends on who you talk to) but I'll go through 2 pieces: setter injection, ie injecting using a setter function; constructor injection submitting values to be used in the constructor.
We begin with the injection of a callback function! Callback We can take the code directly, I create a class called endothermic CallbackInjection package {/ *** A class that uses the injection * to replace a function to be called endothermic * on the debug output. * @ Author Calle Mowday * / public class CallbackInjection {private var _callback: Function; / *** The constructor * / public function CallbackInjection () {/ / Sets a default function to be / / used if no one else has injected _callback endothermic = default callback; } / *** Call the debug function with a test * string * / public function test (): void {debug ("Running test function"); } / *** Replaces the callback function to a new * function * / public function setCallback (newCallback: Function): void {_callback = newCallback; } / *** The function to be used if no other * have been injected * / private function default callback (message: String): void {trace ("[CallbackInjection]" + message); } / *** The function that uses the callback function. * / Private function debug (message: String): void {/ / Call the method _callback _callback (message); }}}
I have commented up most of the code, but we'll look at three things. The first is the variable that is defined on line 11, _callback of type Function. This function is the one we'll call on our debug function. Ie. every time something is calling on debug in the instance the function will be called on. The other is thus debug function (line 53) where we call on _callback function with parameter sent into debug. The last is setCallback endothermic (line 36) thus replacing endothermic the variable _callback endothermic with a new function.
And it is the method we want to change the default function with. In the main, I write this: / / Creates an instance of the class was the callback instance: CallbackInjection = new CallbackInjection (); / / Call the test function with the usual call hill callbackInstans.test (); / / Replaces the callback to your own callback callbackInstans.setCallback (newCallback); / / Call the test function with a new callback callbackInstans.test ();
Constructor injection next part is basically the exact same thing as the previous one, except that you put the callback function in the constructor, like this: package {/ *** A class that uses the injection * to replace a function that will be called * on the debug output. * @ Author Calle Mowday * / public class CallbackInjection {private var _callback: Function; / *** The constructor * / public function CallbackInjection (callback: Function = null) {/ / Sets a default function to be / / used if no one else has injected _callback = callback! = Null? callback: default callback; } / *** Call the debug function with a test * string * / public function test (): void {debug ("Running test function"); } / *** The function to be used if no other * have been injected * / private function default callback (message: endothermic String): void {trace ("[CallbackInjection]" + message); } / *** The function that uses the callback function. * / Private function debug (message: String): void {/ / Call the method _callback _callback (message); }}}
If you need to use a class and want to use the injection method, use is made of interfaces. Ie. instead of saving and inject endothermic a method they will use an interface inside endothermic the class, and the injectors need to make sure that the object that is injected into the class follow the correct endothermic interface. The advantage of the interface is that you can be sure that the methods you need is in the subject is, unlike injection methods where the method does not necessarily have the correct number of parameters and therefore

No comments:

Post a Comment