拥有另一个对象的对象



目前我正在使用Java设计一款大富翁游戏。

游戏中的每个玩家都可以拥有不同的属性。我遇到的问题是如何为每个玩家分配不同的属性对象。我有一个Player类和一个Properties类。作文是做这件事的最佳方式吗?如果是,我该怎么做?

我将添加一个新的类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
  }
}

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

当你玩"大富翁"游戏并购买房产时,你会拿到房产卡,并将其添加到你面前的房产列表中。

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

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

组合有效。只要一个播放器有一个properties对象,并且properties对象包含所有必要的数据,你就可以了(假设你实现了必要的getter和setter方法)。

属性可以有一个Owner属性,即Player。

你也可以在属性播放器上建立一个列表。

您将需要组合和多态性。

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

主要:

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
}

最新更新