Dart Interfaces: Implicit Interface | Software Architecture

Dart Interfaces: Implicit Interface | Software Architecture

Implicit Interface

Implicit interfaces in Dart mean that a class automatically implements an interface that consists of all its instance methods and getters.

In other words, if a class has certain methods or properties, it can be treated as implementing an interface with those methods and properties.

Example

void main() {
  var obj = B(); // Creating an instance of class B
  obj.display1(); //Calling display1() method of class B
  obj.display2(); // Calling display2() method of class B
  obj.display3(); // Calling display3() method of class B
}

class A { // Class A with methods display1() and display2()
 // Method to display message indicating it's from class A
  void display1() {
    print('Class A first method');
  }
// Method to display message indicating it's from class A
  void display2() {
    print('Class A second method');
  }
}

class C { // Class C with method display3()
// Method to display message indicating it's from class 
  void display3() {
    print('Class C first method');
  }
}
 class B implements A, C{
  void display1() {  // Override display1() with custom message
    print('After implementation class A first method');
  }
  void display2() {  // Override display2() with custom message
    print('After implementation class A second method');
  }
  void display3() {   // Override display3() with custom message
    print('After imlementation class C first method');
  }
}
Output:
//After implementation class A first method
//After implementation class A second method
//After imlementation class C first method

Output Explanation:

When the main() function is executed, it creates an instance of class B and calls its methods display1(), display2(), and display3(). The output of each method call will reflect the custom messages provided in their implementations:

  • display1() and display2() methods will print custom messages indicating their implementations in class B.

  • display3() method will print a custom message indicating its implementation in class B as well.

This code demonstrates how Dart allows a class to implement multiple interfaces and provide custom implementations for the methods defined in those interfaces, showcasing the concept of implicit interfaces in Dart.

Software Architecture:

Loose Coupling:

  • Implicit interfaces allow different parts of a program to talk to each other without knowing too much about how they work internally.

  • Classes can talk to each other as long as they follow the same rules (interfaces).

  • This helps to keep different parts of a program separate, so you can change one part without breaking everything else.

Flexibility:

  • With implicit interfaces, a class can follow different sets of rules (interfaces) depending on what it needs to do.

  • This means you can use different classes in the same place as long as they follow the same rules.

  • It gives you more options for how you build your program.

Modularity:

  • Implicit interfaces encourage breaking your program into smaller, independent parts.

  • Each part only needs to know about the rules (interfaces) it uses, not how the other parts work internally.

  • This makes it easier to understand and change different parts of your program without affecting the rest.

Testability:

  • Implicit interfaces make it easier to test parts of your program in isolation.

  • You can create pretend versions of classes (mock objects) that follow the same rules as the real ones.

  • This makes it easier to check if each part of your program works correctly.

Contract Enforcement:

  • Implicit interfaces act like agreements between different parts of your program.

  • They specify what each class should be able to do without saying exactly how it should do it.

  • This helps to make sure everything works together smoothly and reliably.