0

In a class which needs to "contain information" about another class (sorry I don't know the terms for this), should I store the reference to that other class as something like an integer/id, or should I store it as an instance of the other class? What is this called, if there is a name for it?

As a very basic example, an app where we want to store what a user's favorite restaurant is:

public class User {
    public int id { get; set; }
    public string name { get; set; }

    // id of restaurant...
    // public int favoriteRestaurantId { get; set; }

    // ...or entire instance of Restaurant type
    // public Restaurant favoriteRestaurant { get; set; }
}

public class Restaurant {
    public int id { get; set; }
    public string name { get; set; }
}

Note: if you think this is off topic, please explain why this question would be allowed and is a highly rated/useful question, but mine is not: Interface vs Base class Or at the very least tell me what this is "called" so I can research it more myself. As far as I can tell from Stackoverflow's FAQ this question is on topic.

trent
  • 25,033
  • 7
  • 51
  • 90
KayakinKoder
  • 3,243
  • 3
  • 23
  • 37
  • 2
    In OOP you make objects directly refer to other objects, not to their ids or names. So, if a `user` has a favorite `restaurant`, the `user` should store the `restaurant` instance as one of its properties (instance variables), rather than the name or id of the restaurant. – Leandro Caniglia Sep 24 '18 at 09:15
  • 1
    One object containing a reference to another is called object composition. See: https://stackoverflow.com/questions/3441090/what-is-composition-as-it-relates-to-object-oriented-design. It is possibly the most important principle in OOP. – jaco0646 Sep 24 '18 at 21:20
  • Your question is tagged [tag:oop], but you ask "should I?" It's much more "object-oriented" to store references to other objects, but whether you *should* depends on many other factors. In data-oriented programming, storing IDs (and having functions that operate on a higher-level data structure) is more common. – trent Sep 29 '18 at 12:15
  • See, for example, [What is data oriented design?](https://stackoverflow.com/questions/1641580/what-is-data-oriented-design) I think your question boils down to "what paradigm should I choose?" which is ultimately up to you. – trent Sep 29 '18 at 12:18

1 Answers1

1

Your first variant

public int favoriteRestaurantId { get; set; }

only makes sense if you are only interested in the id and not the other attributes (name) of the restaurant object. Otherwise you will need some external container that stores all restaurant objects and have to search the container for the restaurant with the given id.

In your second variant

public Restaurant favoriteRestaurant { get; set; }

if you write

someUser.favouriteRestaurant = someRestaurant;

this also stores a reference to an existing someRestaurant. It will not copy the whole object. at least not in languages like C# and Java.

Only if you do something like

someUser.favouriteRestaurant = new Restaurant(someRestaurant);

the user will have its own copy of the restaurant object.

There are cases where this would make sense but in your example it is probably not a good idea for two reasons:

  1. If for example the name of the someRestaurant changes, this should also change the name of favouriteRestaurant. This will not happen automatically if favouriteRestaurant is a copy.

  2. It is a waste of memory.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45