Reputation: 151
I've got two classes that go together, but at any given time an object of a given class might or might not be partnered with an object of the other class, and the partners might change over the course of the program.
I want each object to be able to access its partner if it has one, and I want to make sure that both objects keep accurate records of their current partner.
I've come up with something that seems to work (below), but it takes three function calls to avoid getting caught in an infinite loop, and it just seems kind of messy. Is there a better way that I'm not seeing?
(By the way, this is C#, so all of those variables are references. In case it wasn't obvious.)
class Horse {
private Carriage existingCarriage;
public Horse(int num) { horseNumber = num;}
public void setCarriage(Carriage newCarriage) {
if(existingCarriage != null) {
Carriage temp = existingCarriage;
existingCarriage = null;
temp.setHorse(this);
}
existingCarriage = newCarriage;
}
}
//pretty much the same thing with "Horse" and "Carriage" switched.
class Carriage {
private Horse existingHorse;
public Carriage() {}
public void setHorse(Horse newHorse) {
if(existingHorse != null) {
Horse temp = existingHorse;
existingHorse = null;
temp.setCarriage(this);
}
existingHorse = newHorse;
}
}
Upvotes: 0
Views: 121
Reputation: 8661
I'd suggest creating a third class. A Horse is separate to a Carriage, and vice-versa - they don't need each other to exist. When they are together, they are a HorseAndCarriage:
public class Horse
{
public int Id { get; set; }
public Horse(int id)
{
Id = id;
}
}
public class Carriage
{
public int Id { get; set; }
public Carriage(int id)
{
Id = id;
}
}
public class HorseAndCarriage
{
public int HorseId { get; set; }
public int CarriageId { get; set; }
public HorseAndCarriage(int horseId, int carriageId)
{
HorseId = horseId;
CarriageId = carriageId;
}
}
When you assign a horse to a carriage, you aren't modifying properties of the horse or of the carriage, you are creating a new HorseAndCarriage
entity (and if the horse was already assigned to another carriage, or the carriage was already assigned to another horse, you would delete that HorseAndCarriage
). If you want to know what carriage is assigned to a horse, or vice versa, you simply look for an entry in the collection of HorseAndCarriage
s.
I used ids in the model above, because you can't share references across different machines, and you can't persist them to a database. If you don't care about that, you could update the HorseAndCarriage
class to use references instead:
public class HorseAndCarriage
{
public Horse Horse { get; set; }
public Carriage Carriage { get; set; }
public HorseAndCarriage(Horse horse, Carriage carriage)
{
Horse = horse;
Carriage = carriage;
}
}
Upvotes: 2
Reputation: 1647
I don't think you need as much code. Something simple like this should do the trick:
class Horse
{
public Carriage existingCarriage;
public void setCarriage(Carriage newCarriage)
{
existingCarriage = newCarriage;
if (existingCarriage != null) existingCarriage.existingHorse = this;
}
}
class Carriage
{
public Horse existingHorse;
public void setHorse(Horse newHorse)
{
existingHorse = newHorse;
if (existingHorse != null) existingHorse.existingCarriage = this;
}
}
Upvotes: 0