简体   繁体   中英

How can I access an object's method from another class giving the class not to much visibility?

I have a problem designing my little game. I have different classes and now I want to access an objects Method from another class, which is painting the Game. Now this is what I wrote:

public class GameMain {

    public GameMain() {
       new GameFrame();
    }
}

GameFrame class:

public class GameFrame extends JFrame {

     public GameFrame() {
         new GameLabel(this);
     }

}

GameLabel class:

public class GameLabel extends JLabel {

    private GameFrame gameFrame;

     public GameLabel(GameFrame gameFrame) {

       }
    @Override
  public void paintComponent(Graphics g) {

  }
}

Now I have a class which is called Player. This Player class has a method which is called paintPlayer(). This Method takes a Graphic as Parameter to paint on it. So I want to call the object method paintPlayer() from my paintComponent() method in my GameLabel class. Therefore I Need the current Player object so what I did was: (Old Version of my GameMain class)

public class GameMain {

  private GameFrame frame;
  private GameLabel label;
  private Player player;

  public GameMain() {
      frame = new GameFrame(this);
      label = new GameLabel(this, frame);
      player = new Player(this);
  }
  public GameFrame getFrame() {
     return frame;
  }

  public GameLabel getLabel() {
    return label;
  }

  public Player getPlayer() {
     return player;
  }

 }

I just created all Game Object in my GameMain class and generated getters for the object. Then I just gave all the object constructers an instance of GameMain so I am able to get all Objects I Need from my getters in the GameMain class. So this is what I did in my GameLabel class:

public class GameLabel extends JLabel {

   private GameMain main;

   public GameLabel(GameMain main) {
      this.main = main;
   }
   @Override
   public void paintComponent(Graphics g) {
       main.getPlayer().paintPlayer(g);
   }
 }

Everything worked but I was told it is a really bad way to access the paintPlayer() method. I was told that I give to much visibility to each class in my pattern because I can access every Object from every class which has an instance of my class GameMain. But what do I have to do instead? I mean to paint my Player I Need an instance of him in my GameLabel class.

This is not exactly how I would do it--programming patterns such as ECS are more efficient--but in an OO way and staying close to your original intent, I would have the following.

class Painter {
    Graphics g;

    public Painter(Graphics g) {
        this.g = g;
    }

    public void paint(Paintable p) {
        p.paint(g);
    }
}

interface Paintable {
    void paint(Graphics g);
}

class Player implements Paintable {
    public void paint(Graphics g) {
        // do your paint here
    }
}

class GameLabel implements Paintable {
    Player player;

    public void paint(Graphics g) {
        player.paint(g);
    }

    public void setPlayer(Player player) {
        this.player = player;
    }
}

class GameMain {
    Painter painter;
    Player player;
    GameLabel label;

    public GameMain() {
        // set up your frame and Graphics, etc.
        Graphics g = ...; 
        painter = new Painter(g);
        player = new Player();
        label = new GameLabel();
        label.setPlayer(player);
    }

    public void doIt() {
        painter.paint(label);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM