Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How deep do you take the Has-a when it comes to Object Oriented Design?

Tags:

c#

oop

If I say a car has a certain number of tires, a tire size, a tire brand, then I am guessing I could make a class like this:

public class Car
{
    public int TireCount {get;set;}
    public float TireSize {get;set;}
    public string TireBrand {get;set;}
}

On the other hand, I could make Tire a class and then make that a property of the class Car like so:

public class Tire
{
   public int Count {get;set;}
   public float Size {get;set;}
   public string Brand {get;set;}
}

public class Car
{
    public Tire tire {get;set;}
}

What is the better way? How deep do I take the relationship? Is it possible to over Object if that there is such as saying?

like image 788
Xaisoft Avatar asked Jan 24 '26 04:01

Xaisoft


1 Answers

As deep as it makes sense to go for your application.

In your example, your Car class would have multiple properties related to tires...so a Tire class (or collection of Tires) makes sense. Probably something like:

public class Tire
{
    public float Size {get; set;}
    public string Brand {get; set;}
}

public class Car
{
    public List<Tire> Tires {get; private set;}
}

Then you could do:

Car myCar = new Car();

// Some initialization here

int tireCount = myCar.Tires.Count();

To get the count of tires.

like image 161
Justin Niessner Avatar answered Jan 25 '26 19:01

Justin Niessner