logo

Crowdly

Tu equipo está desarrollando un videojuego de rol . Se plantea una clase para l...

✅ Перевірена відповідь на це питання доступна нижче. Наші рішення, перевірені спільнотою, допомагають краще зрозуміти матеріал.

Tu equipo está

desarrollando un

videojuego de rol

. Se

plantea una clase para la creación de criaturas de diferentes tipos, y la

inicialización de éstas con unos atributos físicos y de comportamiento dados.

Asumiendo clases

Goblin, Ogre, Orc y Troll que heredan de Creature

(abstracta), un compañero te propone la siguiente

implementación:

public class CreatureCreator {

  public

Creature

newCreature(CreatureType type,

     PhysicalAttributes

physicalAttributes,

     BehavioralAttributes

behavioralAttributes) {

        Creature creature = null;

        if

(type.equals(CreatureType.

GOBLIN)) {

           creature = new Goblin();

        } else if

(type.equals(CreatureType.

OGRE)) {

           creature = new Ogre();

        } else if

(type.equals(CreatureType.

ORC)) {

           creature = new Orc();

        } else if (type.equals(CreatureType.TROLL)) {

           creature = new Troll();

       }

       creature.setPhysicalAttributes(physicalAttributes);

       creature.setBehavioralAttributes(behavioralAttributes);

       return creature;

   } // newCreature

}

 

Claramente

ves inconvenientes (¿cuáles?) en la implementación, y le propones seguir el

patrón

Factory Method

. Te entrega el

siguiente código:

public abstract class CreatureCreator {

  public

Creature

newCreature(CreatureType type,

     PhysicalAttributes physicalAttributes,

     BehavioralAttributes

behavioralAttributes) {

        Creature creature = null;

        creature = this.createCreature(type);

        creature.setPhysicalAttributes(physicalAttributes);

        creature.setBehavioralAttributes(behavioralAttributes);

        return creature;

    } // newCreature

    public abstract

Creature

createCreature(CreatureType type);

}

0%
0%
0%
Більше питань подібних до цього

Хочете миттєвий доступ до всіх перевірених відповідей на moodle.uam.es?

Отримайте необмежений доступ до відповідей на екзаменаційні питання - встановіть розширення Crowdly зараз!