Object Owning Another Object

I am currently developing a Monopoly game using Java.

Each player in the game can own different properties. The problem I am facing is how to assign different property objects to each player. I have a Player class and a Properties class. Would composition be the best way to do this? If so, how do I do this?

+4
source share
5 answers

I would add a new PropertyManager class.

This allows you to easily provide business rules in one place (a good separation of problems), rather than diving through a bunch of players or property objects if you choose a song in any of them. This will prevent the Player and / or property classes from becoming weighted with the purchase / sale of business rules in the future.

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 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
source

Think about it in real life.

When you play a monopoly and you acquire real estate, you take a property card and add it to your list of properties in front of you.

So, in this case, you are a Player object adding Property objects to your property list.

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

The composition works. As long as the player has a property object, and the property object contains all the necessary data, you should be fine (provided that you implement the necessary getter and setter methods).

0
source

a property may have an owner property that is a Player.

You can also create a list in Player of Properties.

0
source

You will need composition and polymorphism.

Assuming a player can have more than one property, you will need a Property List. If the properties may differ in the attributes that they have, you can apply Polymorphism and Inheritance. You will probably see only Inheritance below, but you will need Polymorphism when you get various properties and manipulate them.

Mostly:

 public static void main(String args[]){ Player player1 = new Player(); BlackProperty blackProperty = new BlackProperty(); BlueProperty blueProperty = new BlueProperty(); player1.addProperty(blackProperty); player1.addProperty(blueProperty); } 

All your domain classes:

 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 } 
0
source

All Articles