Categories

Aug 12 in Design Patterns
Q: Q.  What is Inversion of Control in Design Pattern?

1 Answer

Aug 12

Inversion of control is a broad term but for a software developer it's most commonly described as a pattern used for decoupling components and layers in the system.

For example, say your application has a text editor component and you want to provide spell checking. Your standard code would look something like this:

public class TextEditor {

    private SpellChecker checker;

    public TextEditor() {
        this.checker = new SpellChecker();
    }
}

What we've done here creates a dependency between the TextEditor and the SpellChecker. In an IoC scenario we would instead do something like this:

public class TextEditor {

    private IocSpellChecker checker;

    public TextEditor(IocSpellChecker checker) {
        this.checker = checker;
    }
}

You have inverted control by handing the responsibility of instantiating the spell checker from the TextEditor class to the caller.

SpellChecker sc = new SpellChecker; // dependency
TextEditor textEditor = new TextEditor(sc);
Click here to read more about Loan/Mortgage
Click here to read more about Insurance

Related questions

Madanswer
Oct 17, 2019 in Design Patterns
Oct 17, 2019 in Design Patterns
Oct 17, 2019 in Design Patterns
...