mercredi 22 août 2018

code readability / habitual practice / design pattern?

I have been analyzing some classes that can be sumarized like this:

public class RulerFather {
    private Girl girl;
    private Pet  pet;

    public  RulerFather()
    {
        girl = new Girl(this);
        pet  = new Pet(this);
    }

    /*
     *  allowed messages between Girl and Pet   
     */
    public feedPet()
    {
        pet.receiveFood();
    }

    public lickGirl()
    {
        girl.beLicked();
    }
}
public class Girl {
    Father father;
    public Girl(Father father)
    {
        this.father = father;
    }
...

Father instantiates girl and pet, and defines allowed messages that can be exchanged. E.g. in the code of Girl, she can feed the pet (send a message to the pet) by doing:

father.feedPet();

I mean, objects created by father can exchange messages but only using the method provided by father, so father is kind of "ruler" defining which methods are allowed to exchange between them (at that level of interaction).

My question is, in the code I analyzed, I saw a mixture between the mentioned above and another way that is just making the references of girl and pet public instead of private (this leads to freedom and some confusion as girl can call any method of pet, in some way RulerFather defines a number of valid interactions between girl and pet during the scope of the objects created by RulerFather).

Is this related to any recommended coding practice ? (or perhaps even a design pattern i do not know?)

Aucun commentaire:

Enregistrer un commentaire