in Design Patterns by (1.7k points)
Q.  What is Inversion of Control in Design Pattern?

1 Answer

0 votes
by (1.7k points)

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);

Related questions

0 votes
asked Aug 12, 2020 in Design Patterns by RShastri (1.7k points)
0 votes
asked Jul 24 in Design Patterns by SakshiSharma (32.2k points)
0 votes
asked Oct 17, 2019 in Design Patterns by Robin (14.6k points)
+1 vote
asked Oct 17, 2019 in Design Patterns by Robin (14.6k points)
...