拥有另一个对象的对象

时间:2012-07-19 00:23:03

标签: java object ownership

目前我正在使用Java设计一款Monopoly游戏。

游戏中的每个玩家都拥有不同的属性。我遇到的问题是如何为每个玩家分配不同的属性对象。我有一个Player类和一个Properties类。组合是最好的方法吗?如果是这样,我该怎么做?

5 个答案:

答案 0 :(得分:2)

用现实世界的术语来思考。

当您玩Monopoly并购买房产时,您可以使用房产卡并将其添加到您面前的房产列表中。

因此,在这种情况下,您是一个Player对象,将Property对象添加到属性列表中。

public class Player
{
    private List<Property> properties;
}

答案 1 :(得分:2)

我会添加一个新类PropertyManager。

这使您可以轻松地在一个位置提供业务规则(良好的关注点分离),而不必在任何一个选择组合时潜入一堆玩家或属性对象。这将使玩家和/或物业类在未来通过购买/销售业务规则而变得沉重。

public final class PropertyManager {

  /**
   * The PropertyManager instance, example usage:
   *   PropertyManager.INSTANCE.buyProperty(property, buyer);
   * NB: Good candidate for dependency injection instead if you are doing this.
   */
  public static final PropertyManager INSTANCE = new PropertyManager();

  private static final Map<Property, Player> propertyListing = new HashMap<Property, Player>();

  /**
   * Buy a property from the banker, banker or property manager could maintain
   * Collection of available properties
   */
  public void buyProperty(Player buyer, Property property) {
    if (propertyListing.containsKey(property)) {
      throw new IllegalStateException("Unable to buy unless owner sells it");
    }
    propertyListing.put(property, buyer);
  }

  /**
   * Broker a transaction between two players for the sale of a property
   */
  public void sellProperty(Player seller, Player buyer, Property property) {
    if (!propertyListing.containsKey(property)) {
      throw new IllegalStateException("Unable to sell Property which is not owned");
    }
    Player owner = propertyListing.get(property);
    if (!owner.equals(seller)) {
      throw new IllegalStateException("Unable to sell property seller doesn't own");
    }
    // TODO : Deduct/transfer monies (ensure sufficient value in buyer account etc)
    propertyListing.put(property, buyer); 
  }

  /**
   * Retrieve a List of all of the Player's properties
   */
  public List<Property> getProperties(Player owner) {
    // TODO Either iterate through the propertyListing or use a second Map for player to List<Property>, NB: they should be guarded with a shared lock if you do this (threading).
  }

  /**
   * Retrieve the owner of a Property or null if it is unowned
   */
  @Nullable // javax annotation indiciates can be null
  public Player getOwner(Property property) {
    return propertyListing.get(property);
  }

  /**
   * Hide the constructor as the only property manager should be INSTANCE
   */
  private PropertyManager() {
    // Prevent further instantiation
  }
}

答案 2 :(得分:0)

作文有效。只要玩家拥有属性对象,并且属性对象包含所有必需的数据,您应该没问题(假设您实现了必要的getter和setter方法)。

答案 3 :(得分:0)

该属性可以拥有属于Player的所有者属性。

您还可以在属性播放器上构建列表。

答案 4 :(得分:0)

您将需要组合和多态。

假设玩家可以拥有多个属性,则需要一个属性列表。如果属性的属性不同,则可以应用“多态”和“继承”。您可能只会在下面看到继承,但是当您获取不同的属性并操纵它们时,您将需要多态性。

主要:

public static void main(String args[]){
  Player player1 = new Player();

  BlackProperty blackProperty = new BlackProperty();
  BlueProperty blueProperty = new BlueProperty();

  player1.addProperty(blackProperty);
  player1.addProperty(blueProperty);
}

您的所有域类:

public class Player{
  private List<Properties> propertyList;

  // getters and setters

  public void addProperty(Properties properties){
    if(this.propertyList == null){
      this.propertyList = new ArrayList<Properties>();
    }

    this.propertyList.add(properties);
  }
}

public class Properties{
  private int noOfFloor;
  // getters and setters
}

public class BlackProperty extend Properties{
  private String noOfGate;
  // getters and setters
}

public class BlueProperty extend Properties{
  private String noOfLawn;
  // getters and setters
}