Skip to content
Advertisement

Having issues calling a decorator method (Java)

Working on a group project that is essentially a text-based Pokemon rip-off. My classmates and I were given a UML to work from so I can’t accept any solutions that would add methods or change their parameters. But essentially the issue we’re running into is this:

There is a singleton PokemonGenerator class, that has a method generateRandomPokemon(int level) that picks a random Pokemon from a list of 23 Pokemon and then gives them a random buff to either their Attack or HP for each level above 1. I’m just going to talk about the AttackUp one since they both function the same. So the generateRandomPokemon method creates a pokemon and then has this little bit

for(int x = 1; x < level; x++){
  addRandomBuff(randP); 
}

Where it calls the method Pokemon addRandomBuff(Pokemon p). There is also a class called PokemonDecorator, and extending from it are some classes: AttackUp, AttackDown, HpUp, and HpDown. AttackUp’s constructor reads as follows:

public AttackUp(Pokemon p)
  {
    super(p, " +ATK", 0);
  }

as PokemonDecorator’s constructor reads:

public PokemonDecorator(Pokemon p, String extraName, int extraHp)
  {
    super(p.getName() + extraName, p.getHp() + extraHp, p.getMaxHp() + extraHp);
    pokemon = p;
  }

(PokemonDecorator has member Pokemon p, and extends abstract class Pokemon whose constructor is Pokemon(name, hp, maxHp)). Anyways, we keep running into various issues with trying to use the buff/debuff classes. None of the buffs are showing up on the Pokemon they should be attached to. Here is our current iteration of the method (although it has gone through many iterations)

Pokemon addRandomBuff(Pokemon p)
  {
    int buff = (int)(Math.random()* 2) + 1;
    if(buff == 1)
    {
      p = new AttackUp(p);  
    }
    if(buff == 2)
    {
      p = new HpUp(p);
    } 
    return p; 
  }

When the code runs, the Pokemon should display “+ATK” or “+HP” by their names if the buff is attached, and they are not. Setting p = the decorators does not seem correct to me but it’s the only way I could write it without getting a compiler error about not having an instance of AttackUp or PokemonDecorator inside the PokemonGenerator class.

Advertisement

Answer

In the code in your question, you have three different Pokemon variables named p: one in the scope of AttackUp‘s constructor, one in the scope of PokemonDecorator‘s constructor and one in the scope of the addRandomBuff method.

When you say p = new AttackUp(p); or p = new HpUp(p);, this assigns a new value to the p in the addRandomBuff method. Note however that it doesn’t change the assignment of the Pokemon that is passed in from the calling method, and since the decorator pattern you are using does not mutate any state on the original Pokemon object, this is why you are not seeing the change coming through in the end.

You are very close, however; in your for loop, try changing the line to:

randP = addRandomBuff(randP);

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement