lundi 25 mai 2020

Bridge Pattern how to take out Implementor logic

I am trying to figure out how to use the bridge pattern in the following situation. The problem I am facing is I am not able to separate the implementors.

public class GoodTest {
    void sayHi(){
        System.out.println("I want to say:"); // Top part - same for all implementors

        System.out.println("Good Test"); // Middle part different

        System.out.println("I am done"); // Bottom part - same for all implementors
    }
}

public class GoodMathTest {
    void sayHi(){
        System.out.println("I want to say:"); // same for all implementors

        int x = 10;
        System.out.println(x);             // middle part is different from others 
        System.out.println("Good Math Test");  

        System.out.println("I am done"); // same for all implementors
    }
}

public class GoodMathWrittenTest {
    void sayHi(){
        System.out.println("I want to say:"); // same for all implementors

        int x = 10;
        System.out.println(x*x);
        System.out.println("Good Math Written Test"); // middle part different 
        System.out.println(x*x*x);

        System.out.println("I am done"); // same for all implementors
    }
}

I believe the solution, in this case, is to take out Test Type and Subject Test and build their own hierarchy and pass an instance of these to a class which can direct the call to one of these implementors.

The Problem:

  1. As the original method uses things which would be delegated to Subject Test and Test type. How do we take out and break the code Subject Test and Test type.
  2. If I do the above, how would I make write the method for the concrete class which uses the implementors received by dependency injection?

Thanks

Aucun commentaire:

Enregistrer un commentaire