samedi 28 mai 2016

Add an object in the deph "level" of an object composition with collections

I'm learning java and starting to try to do things in the right way...

I don't know if the Title is right, please advice me if it's not clear, english is not my mother language. I have a too large amount of code to post it. So i made this "simplify" version for evidence my doubt.I think it's more clear. If is not enough clear please tell me.

  • The context: I have a object A who have a collections of B object, and B have a collection of C objects, and C... a collection of D. I need to put the D object in this "composition"(?) of objects. The object D have the information about in which C object should be put in. every C object is assigned to a B object according his id.
  • The problem:How to know in A to whish B object pass the D object.
  • The "solution": make a method to filter the D object according his id and return the B object id.

This is the scheme of my solution:

    public class Test2 {
    public static void test (String args[]){
        A a = new A();
        //... filling the A,B,C data..
        //... done...
        //.. now the cake:
        public String newD = "C=45,D=67,more info...."; // 45 is the C id, 67 is the D id.
        D dude = new D(newD);
        // so i need to put this friend in the C object...
        a.addSomeDude(dude); // looks easy...
    }

}
class A{ // I have 3 B's in my collection = B_1, B_2, B_3. And i can have more...
    private List<B> Blist;
    private String id;
    // blah blah
    public void addSomeDude(D dude){ //... ok, i have some problems... i don't know where to put this dude
        String Bid = SaveTheday.whoGoesWhere(dude.getId()); //oh! you save the day SavetheDay, what a suitable name!
        for(B bDude : Blist)
            if(bDude.getId() ==Bid ){
                bDude.addSomeDude(dude);
            }

    }
    //some things...
}
class B{ // I have some C's in my collections...
    private List<C> Clist;
    private String id;
    //blah blah blah
    public void addSomeDude(D dude){ //... i know where to put this dude, i just get the C id from the dude...
        for(C cGuy : Clist)
            if(dude.getCid() == cGuy.getId()){
                cGuy.addSomeDude(dude);
            }
    }
}
class C{ // I i have some D dudes...
    private List<D> Dlist;
    private String id;
    //yes, more things...
    public void addSomeDude(D dude){
        D objectDude = new D(dude.getCId());
        Dlist.add(objectDude);
    }

}
class D{
    private  String id;
    private String cId;
    //...some things and getters
}
class SaveTheday{
    //...well, maybe i can do the job...
    public static String whoGoesWhere(String id){
        String [] B_1 = {"1","2","3","4","5","6"}; // these are C ids...
        String [] B_2 = {"11","22","33","44","55"};
        String [] B_3 = {"12","45","55","66","77"};
        for( String i : B_1)
            if (i.equals(id))
                return "B_1";
        for( String i : B_2)
            if (i.equals(id))
                return "B_2";
        for( String i : B_3)
            if (i.equals(id))
                return "B_3";
        return null;
    }
}

This eventually works, but I'm starting to thinks it's a very not friendly OO principles solution. Why?

  1. What if i need to add a new C id? yes, add it to the comparison method, but, what about the "open to expansion, no to modification" principle?
  2. And if i need to add a new B, unknown number of B's?
  3. Where the hell put the whoGoesWhere method, i think i did a good job encapsulating the comparison (i can change it without affect the others classes) but, where to put it? is in the right place?
  4. i feel out there should be a better solution to this problem, more flexible, expansible and maintainable solution.

So, can you guys tell me what I'm missing? there's some pattern what I'm not seeing? I hope a expressed myself right, if not, please tell me to add more details.

Aucun commentaire:

Enregistrer un commentaire