简体   繁体   English

如何从视图访问数据或应用程序模型?

[英]How do I access the data, or model of my application from the view?

I'm relatively new to writing big graphical applications, and as a project I have to write a game in Java that uses swing, while using proper software engineering principles. 我对编写大型图形应用程序比较陌生,作为一个项目,我必须用Java编写一个使用swing并同时使用适当的软件工程原理的游戏。 I've decoupled the graphical interface from the logic of the game, but now I have no idea how I can get access to all the data I need to populate in the view. 我已经将图形界面与游戏逻辑脱钩了,但是现在我不知道如何访问视图中需要填充的所有数据。

For example, I have a class called 'Board' that has a reference to a list of 'Territory' objects, which in turn each have a reference to a polygon object for the relevant area of the map. 例如,我有一个名为“ Board”的类,该类具有对“ Territory”对象的列表的引用,而每个“ Territory”对象又具有对地图相关区域的多边形对象的引用。 I want to make a custom JPanel that displays a graphical world map, and get the collection of territories from the board object so I can make the map interactive by using the polygon objects. 我想制作一个自定义的JPanel,以显示图形化的世界地图,并从board对象获取区域集合,以便可以使用多边形对象使地图具有交互性。

I thought about using some form of singleton or factory design patten so I could access all the data as needed, but it's seems that a singleton is not what I want, and I don't even understand the factory patten well enough to determine if it's what I want our not. 我考虑过要使用某种形式的单例或工厂设计模板,以便可以根据需要访问所有数据,但是似乎单例不是我想要的,而且我什至不了解工厂模板足以确定它是否是我要我们不要的。 Am I going to be forced to pass a reference to all my objects to each component of the GUI, or is there a better way? 我是否将被迫将对我所有对象的引用传递给GUI的每个组件,还是有更好的方法?


Here's some of my code, though I don't know how much help it will be at this stage. 这是我的一些代码,尽管我不知道在此阶段会有多少帮助。

public class MapPanel extends JPanel {

    private Image image;
    private List<Territory> territories;

    public MapPanel() {
        try {
            BufferedInputStream in = new BufferedInputStream(
                    ClassLoader.getSystemResourceAsStream("MapBig.jpg"));
            image = ImageIO.read(in);
        } catch (IOException ex) {
            // handle exception...
        }

        initialize();
    }

    private void initialize() {
        this.setPreferredSize(new Dimension(900, 600));
    }

    public void setBackground(Image i) {
        this.image = i;
        repaint();
    }

    public void paintComponent(final Graphics g) {
        super.paintComponent(g);
        g.drawImage(image, 0, 0, null);
    }
}

And my Board class: 我的董事会班级:

public class Board {

  private List<Territory> territories;
  //private List<List<Tile>> rows;

  public Board(List<Territory> territories) {
    this.territories = territories;
  }

  public Iterator<Territory> getTerritories() {
    return territories.iterator();
  }
}

And my Territory class which holds the polygons I want to make use of. 还有我的Territory类,其中包含我要使用的多边形。

public class Territory implements java.io.Serializable {

  //region Private Variables
  /**
   * 
   */
  private static final long serialVersionUID = 5360937073507663827L;

  /**
   * The territory's name. 
   */
  private String name;

  /**
   * The representation of this territory in 2d space. 
   */
  private Polygon region;

  /**
   * List of Player objects currently residing in this geographical area
   * of the world map.
   */
  private List<Player> players;

  /**
   * Country that owns this territory.
   */
  private Country owner;

  /**
   * List of neighboring territories that can be traveled to.
   */
  private List<Territory> neighbors;
  //endregion Private Variables

  public Territory(String name, Polygon region) {
    this.name = name;
    this.region = region;
    players = new ArrayList<Player>();
    owner = null;
  }

  /**
   * Get the name of this territory.
   * @return    name of this territory
   */
  public String getName() { return name; }
  /**
   * Set the name of this territory.
   * @param name    name to give this territory
   */
  public void setName(String name) { this.name=name; }
  /**
   * Get the bounded region for this territory.
   * @return    a Polygon object representing the 2D bounds of this
   * territory
   */
  public Polygon getRegion() { return region; }
  /**
   * Set the bounded region for this territory.
   * @param region  a Polygon object representing the 2D bounds of this
   * territory
   */
  public void setRegion(Polygon region) { this.region=region; }
  /**
   * Get the owning Country.
   * @return    the Country object that owns the geograpical area bounded
   * by this territory.
   */
  public Country getCountry() { return owner; }
  /**
   * Set the owning Country.
   * @param country the Country object that owns the geograpical area 
   * bounded by this territory.
   */
  public void setCountry (Country country) { this.owner=country; }
  public Iterator<Territory> getNeighbors() { return neighbors.iterator(); }
  public void setNeighbors(List<Territory> neighbors) { this.neighbors = neighbors; }

  /**
   * Get a list of all Player objects currently residing in this geographical 
   * area of the world map.
   * @return    an Iterator over a List of Player objects
   */
  public Iterator<Player> getPlayers() { return players.iterator(); }
  /**
   * Adds a player to this Territory
   * @param player  Player object to add to this Territory
   */
  public void addPlayer(Player player) { players.add(player); }
  /**
   * 
   * @param player
   */
  public void removePlayer(Player player) { players.remove(player); }
}

You need to use the observable pattern to communicate between components of the MVC pattern. 您需要使用可观察的模式在MVC模式的组件之间进行通信。 In addition, you might want to consider using the Facade pattern to make sure the communication is simplified. 此外,您可能需要考虑使用Facade模式来确保简化通信。

Model: all game logic (also holds the list of objects!) Controller: Interprets user input on the GUI View: Reports action on the gui. 模型:所有游戏逻辑(还保存对象列表!)控制器:在GUI上解释用户输入视图:报告gui上的操作。

Check out this: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller 看看这个: http : //en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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