lundi 30 mai 2016

Which design pattern to use (Active and passive methods)?

I have a player which can feed a dog or chop a tree.

Below are the classes I have written:

public class Dog {

    private int health;

    public void feed(Food food){
        health = health + food.getNutritionalValue();
    }
}

public class Player{

    public void feed(Dog dog, Food food) {
        dog.feed(food);
    }

Player and Dog both have methods that are "active".

Player feeds the dog and dog starts eating the food (I am not really sure if it is good to couple methods in this way).

On the other hand, I have tree. And player is able to chop the tree.

public class Player{

public void chop(Tree tree) {
        //At this point I am not sure
    }

I am not sure if I would use getters and setters of Tree class to interact with the Tree.

Or if I should write an own method for this because the tree gets chopped so it is nothing really active I would call.

So, in the end, there would be two or more kinds of implementations but the two I am thinking of are:

tree.setAmountofWood = x

or

tree.gettingChopped(Damage int)

I think I should make an own method for this chopping-process.

Or is there any design principle I should follow?

Aucun commentaire:

Enregistrer un commentaire