dimanche 6 octobre 2019

Initialize classes in their own interface

I looked at the some code and noticed such construction:

public interface TestIntrerface {
  void doFirst();
  void doSecond();
}

public interface Interface1 extends TestIntrerface {
  Interface1 inter = new FirstClass();
}

public interface Interface2 extends TestIntrerface {
  Interface2 intr = new SecondClass();
}


public class FirstClass implements Interface1 {
  public void doFirst() {
    //...
  }

   public void doSecond() {
      //...
   }
}

public class SecondClass implements Interface2 {
   public void doFirst() {
      //...
   }

  public void doSecond() {
      //...
  }
}


public class Test {
  public static void main(String[] args) {
    Interface1.inter.doFirst();
    Interface2.intr.doSecond();
  }
}

It has potential for me but I've never seen that construct before. Two questions: 1. What is the name of this pattern (is that pattern(?)) ? 2. It is a good practice to use this in the real world?

Thanks

Aucun commentaire:

Enregistrer un commentaire