Quantcast
Channel: Ricardo Parente's Blog » Code Standard
Viewing all articles
Browse latest Browse all 5

Inversion of Controls

$
0
0

Often programmed in real time, without planning. We leave the logic flow, sometimes we’re lucky and end up doing the right thing, sometimes not.

Do not mean to say that the Inversion Control, is right or wrong way. And yes demonstrate different ways to do an implementation.

So basic, inversion of control, when the control is, for instance, method calls takes place in reverse. I left the software flow control to make such an instance, and leaves it up to a third party. Then comes the confusion. Why behave like dependency injection, many say the same thing, others say no.

The simplest form of understanding Inversion of control is to think as follows.

Imagine the scenario where I have a class Car() that needs to instantiate the class FuelInjection() to start the car.

We have two ways to do this, see:


class Car {
	private FuelInjection var injection;

	// método construtor
	function Car() {
		injection = new FuelInjection();
	}

	function Boolean turnOn() {
		if( injection.injectFuel() == true ) {
			// implementation
		}
	}
}

Well, the Car class constructor() instantiate the class FuelInjection(), to use the process of turning the car another way to do this would be using inversion of control and would be injecting a dependency … since the class Car, depends on the Fuel Injection to start the car. Consider in practice.


class Car {
	private FuelInjection var injection;

	// método construtor
	function Car() {
		// codigo do contrutor
	}

	function set Injecao( FuelInjection val ) {
		injection = val;
	}

	function Boolean turnOn() {
		if( injection.injectFuel() == true ) {
			// implementations
		}
	}
}

So, when creating the instance of the class Car(), which will depend on the flow of the software, making the injection of class FuelInjection() for class Car() through the set injection();

In such an implementation, we work with a high level of engagement, making our classes more organized and responsible for doing only what it can.

See more about this in the article on Division of Responsibilities

Well I managed to spend a little of the concept that the pattern Inversion of Controls offers for software development.

Thanks.

By Paulo Teixeira

Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images