vendredi 13 décembre 2019

Implementing Dependency Injection design pattern

I was told that it's possible to implement the Dependency Injection design pattern two times in the following code and that changing the method signatures was allowed:

public class Person {
    String title;
    ArrayList<String> name = new ArrayList<>();

    public Person() {
        name.add("First");
        name.add("Middle");
        name.add("Last");
    }

    public void setTitle(int i) {
        switch (i) {
            case 0:
                title = "Ms.";
                break;
            case 1:
                title = "Mr.";
                break;
            case 2:
                title = "Mx.";
                break;
    }
    }
}

So my attempt of adding the Dependency Injection is as follows:

public class Person {
    String title;

    ArrayList<String> name = new ArrayList<>();


    public Person(String First,String Middle,String Last) {
        name.add(First);
        name.add(Middle);
        name.add(Last);
    }

    public void setTitle(String title) {

        this.title = title;

    }
}

Is this a correct way of implementing the design pattern? or could this code be implemented in a better way?

Aucun commentaire:

Enregistrer un commentaire