samedi 30 octobre 2021

Fighting back sequential coupling with correct design pattern

everyone reading this. First and most important, thank you for spending time on this.

So now let me go straight to the question.

Recently I received a homework where I have to deal with sequential coupling (masking it deeper in the code so outer classes doesn't see it). I'll put below an example of this and explanation.

    class Start{
    static void main(String[] args) {
        //First way
        Dog dogA = new Dog().setDogAType("prefix")
        //Second way
        Dog dogB = new Dog().setDogBType("prefix")
        //Custom
        Dog dogC = new Dog().firstMehtod("prefix")
             .secondMethod_a()
        dog.betweenSecondAndThird()
        dog = dog.theLastMethod()
        dog.optionalLastLast()
    }
}

class Dog {

    String id, prefix, subfix, name

    Dog() {
        //getting tokens for operations in the methods
    }
    
    Dog firstMehtod(String prefix) {
        println("firstMehtod")
        return this
    }

    Dog secondMethod_a() {
        println("secondMethod_a")
        return this
    }

    Dog secondMethod_b() {
        println("secondMethod_b")
        return this
    }

    Dog thirdMethod_a() {
        println("thirdMethod_a")
        return this
    }

    Dog thirdMethod_b() {
        println("thirdMethod_b")
        return this
    }

    Dog theLastMethod() {
        println("alwaysLast")
        return this
    }

    static void betweenSecondAndThird() {
        println("betweenSecondAndThird")
    }

    void optionalLastLast() {
        println("optionalLastLast")
    }   


    Dog setDogAType(String prefix) {
        Dog dog = new Dog().firstMehtod(prefix)
            .secondMethod_a()
            .thirdMethod_b()
            .theLastMethod()
    }

    Dog setDogBType(String prefix) {
        Dog dog = new Dog().firstMehtod(prefix)
            .secondMethod_b()
        dog.betweenSecondAndThird()
        return dog.thirdMethod_b()
            .theLastMethod()
    }
}

As you can see from the name of the method there is first method, two second methods and so on, we also have two methods that call some of the methods in their order and we can also have in outer class using directly the first, second and so on methods. Important note is that all methods are not building the Dog, but more like helper method for calling some stuff that build dogs in the database.

The idea and the thing that should be achieved is to:

  • Get hide the sequantial coupling as much as possible To have two-three quick ways
  • To achive the same goals as with setDogBType() and setDogAType() Be
  • able to make a completly custom one like the ones in the Start class.

My current idea is to use the Strategy pattern by doing the following:

  • Make abstract class BaseStrategy that will contain these first, second methods, with abstract method for building a dog.
  • Make three strategies - one for DogAType, one for DogBType with the specific methods called and one DogCustomType that will call the methods based on the arguments passed.
  • Dog class will be just like a DTO, that will carry our results

How do you see my current idea and do you see something I'm missing or having a better idea to do it?

PS: Wishing you an awesome weekend!

Aucun commentaire:

Enregistrer un commentaire