Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, April 10, 2016

Address calling class

Address calling class


I have an abstract class Moveable with the method abstract void move() which is extended by the class Bullet and the abstract class Character, and Character is extended by the class Survivor and the class Zombie. In Survivor and Bullet the move() method doesnt require any parameters while in the class Zombie the move() method depends on the actual position of the survivor. The survivor and multiple zombies are created in the class Gui.

I wanted to access the survivor in Zombie - what's the best way of doing this? In Gui i wrote a method getSurvivor() but i don't see how to access this method in Zombie?

I am aware that as a workaround i could just pass a [Survivor survivor] as parameter in move() and ignore it in Bullet and Survivor, but that feels so ... bad practice.

Answer by Andrzej Doyle for Address calling class


It depends where your Survivor is being held. Your Zombie will need a reference to a Survivor from somewhere - what does it need it for? Who should be responsible for providing this?

It sounds like your Gui is doing too much actually, I'd prefer to have a class called something like GameState that manages the positions and then just have the Gui handle the output. In any case, if there's a single survivor you can simply have a getSurvivor() method on your Gui/Gamestate - and the Zombie should probably have a reference to the current game state that it's part of. Alternatively you could make the GameState a singleton, and the Zombies could access it that way.

Edit: In response to your comment, perhaps something like this:

public class Zombie  {     private final Gui guiThatMadeMe;       public Zombie(Gui owner)     {        guiThatMadeMe = owner;     }       ....       public void move()     {        Survivor guy = guiThatMadeMe.getSurvivor();          // Do whatever you need to with the survivor     }  }      public class Gui  {     private Survivor lonelyGuy;       ...        public Survivor getSurvivor()     {        return lonelyGuy;     }       public void createNewGame() // I'm guessing you have some method like this     {         lonelyGuy = new Survivor();         for (int i = 0; i < NUM_ZOMBIES; i++)         {            Zombie zombie = new Zombie(this); // pass in this reference            // Do something with the zombie         }         // other setup     }  }  

In any case, the Zombie will have to obtain a reference to something that knows how to get the survivor. An alternative option would be to have the Survivor passed directly into the Zombie, but that doesn't feel quite as clean somehow. It might be a viable option if Zombies must always have one and exactly one Survivor, but that feels like an artificial limitation.

Answer by crazyscot for Address calling class


If there's only one Survivor, you should look into the Singleton design pattern.

Answer by fish for Address calling class


In real world (hehe) the zombies start moving towards the survivor when they detect the survivor.

So you could start the modeling by adding method .detect(Survivor) to your Zombie class. Or .follow(Survivor). In this simplified model your Gui class that creates the survivor and the zombies you could simply call the detect method of the Zombie with a reference to the Survivor. And to Moveable you could add method for getting the current location.

And don't forget to pack a shotgun when you go out.

Answer by Sean Patrick Floyd for Address calling class


Actually, I would say that move() should not be a method without parameters. The semantics of Move imply that there is a target to move to, so it should be either

move(Target t)  

where Target is an interface that is implemented by Zombie and Survivor or

move(Position p)  

Preferably, you can find an abstraction that covers both (a Target, for example could be either a target person or a target position)

Answer by pdbartlett for Address calling class


Extending @Andrzej Doyle's GameState idea above...

If all the Zombie needs from the (potentially complex) Gui class is the position of the Survivor, have Gui implement a simple interface with just that method and have Gui pass an instance of itself to the Zombies when it constructs them.

That makes Zombie more testable as only a single method needs to be mocked out.

interface GameState {    Survivor getSurvivor();  }    class Zombie extends Character {    public Zombie(GameState gs, ...) {      this.gs = gs;      ...    }    ...  }    class Gui implements GameState {    ...    private createZombie(...) {      return new Zombie(this, ...);    }    ...  }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.