Reputation: 9017
This should be a simple question.
Let's say I have an airline and a flight. one airline can have multiple flights. Each flight should be tied to one airline.
How do I create classes to mimic this behavior? What I came up with as follows:
public class Airline
{
private string Name;
private string Description;
private List<Flight> Flights;
public Airline(string Name, string Description)
{
this.Name = Name;
this.Description = Description;
}
public void AddFlight(Flight Flight)
{
if (!this.Flights.Contains(Flight))
{
this.Flights.Add(Flight);
}
}
public void RemoveFlight(Flight Flight)
{
this.Flights.Remove(Flight);
}
}
public class Flight
{
private string No;
private string Time;
private Airline Airline;
public Flight(Airline Airline, string No, string Time)
{
this.No = No;
this.Time = Time;
this.Airline = Airline;
}
}
Is there any better approach to do this task? What I don't like is that when I create a flight, I have to add an airline to it and then I have to call Airline.AddFlight in order to associate it with my airline. Also, I'm looking if there's anything else wrong with the code or anything that can be improved.
Upvotes: 3
Views: 187
Reputation: 1782
This is fine. Except for the circular referencing. You have the Flight class containing an Airline object, and the Airline class containing a List of Flight Objects. Let the Airline Object be the Parent, in your code, you only add flights to the Airline's List of flights when the parent child relationship deems it appropriate. The Flight class needs no knowledge of the Airline class.
Upvotes: 1
Reputation: 54618
I think, in this case, it would be to your advantage to have the the Add method on the Airline, and the Flight class as a subclass with a private/protected constructor:
public abstract class Airline
{
protected List<Flight> _flights = new List<Flight>();
public abstract Flight AddFlight(string no, string time)
{
this._flights.Add(new Flight(this, no, time));
}
public class Flight
{
protected Flight(Airline airline, string no, string time)
{
}
}
}
In this case, you ask the Airline to create the flight and return the Flight.
Upvotes: 1
Reputation: 8818
For one thing, you really don't need the reference to airline within flight. A flight shouldn't need to know what airline it is a part of. That association is inherent. Don't create new flights by themselves, just create them using Airline.AddFlight()
.
So, first thing to do is to change the Flight
constructor to take out airline:
public Flight(string No, string Time)
{
this.No = No;
this.Time = Time;
}
So, here's an example. You want to create 2 airlines, each with two flights. Heres the code to do so:
Airline airline1 = new Airline("Airline1","First Airline");
airline1.AddFlight(new Flight("1","12:00"));
airline1.AddFlight(new Flight("2","12:00"));
Airline airline2 = new Airline("Airline2","Second Airline");
airline2.AddFlight(new Flight("1","12:00"));
airline2.AddFlight(new Flight("2","12:00"));
Upvotes: 1
Reputation: 100527
One more option that may enforce link is to pass factory method for a flight to Airline's AddFlight method like following and essentially force everyone to construct flights this way.
class Airline{
...
Flight AddFlight(Func<Airline, Flight> flightFactory)
{
var flight = flightFactory(this);
privateListOfFlights.Add(flight);
return flight;
}
}
Usage (function could be shared, not just inline lambda):
myAirline.AddFlight(airline=> new FancyFlight(airline, otherArgs));
Upvotes: 1
Reputation: 6329
In Windows Forms, Menus use pattern like the following:
class Airline
{
// code as it was
public Flight AddFlight(string No, string Time)
{
var flight = new Flight(this, No, Time);
AddFlight(flight);
return flight;
}
}
That way you maintain flexibility and don't state the relationship two times.
Upvotes: 0
Reputation: 2146
Why don't you add the Flight to the Airline upon construction:
public Flight(Airline Airline, string No, string Time)
{
this.No = No;
this.Time = Time;
this.Airline = Airline;
Airline.AddFlight(this);
}
?
Upvotes: 5