Hello Dear DaniWeber's. I've been away for some time, and have recently made the transition from VB to C# (Finally). Using C# has opened my eyes to many things that I had either not fully understood, or had a lazy attitude towards. I find myself visiting the core of OOP and indeed polymorhism. Previousley I had used inheriting and implementing very loosley, maybe not even fully understanding their concepts and am now visiting each area and making sure I can implement things such as Interfaces and Abstract classes properly. In short, I'm trying to adopt a more professional approach to my development.
Below is (What I think) is a fair useage of interfaces, however, I am looking for advice for the community to guide me further. Is the code below acceptable, or even correct. My main focus of this question is the use of objects within Interfaces, is this common practice, or even acceptable? I have used the classic Vehicle example in order to try and depict what i have been doing.
I can create a new "Car" like so, and set some of it's properties:
Car MyCar = new Car();
MyCar.Wheels.WheelDiameter = 100;
MyCar.Wheels.WheelCount = 4;
MyCar.Wheels.NutsPerWheel = 1;
MyCar.Wheels.TyrePressure.FrontWheel = 36;
MyCar.Wheels.TyrePressure.RearWheel = 34;
The Interfaces and structure are as follows:
/// <summary>Tyre Pressure Interface</summary>
interface ITyrePressure
{
int FrontWheel { get; set; }
int RearWheel { get; set; }
}
/// <summary>Tyre Pressure Object Implements ITyrePressure</summary>
public class TyrePressure : ITyrePressure
{
int frontWheel;
public int FrontWheel
{
get { return frontWheel; }
set { frontWheel = value; }
}
int rearWheel;
public int RearWheel
{
get { return rearWheel; }
set { rearWheel = value; }
}
}
/// <summary>Vehicle Wheels Interface</summary>
interface IWheel
{
/// <summary>Vehicle Tyre Pressure Information</summary>
TyrePressure TyrePressure { get; set; }
/// <summary>Number of Wheels For Derived Vehicle</summary>
int WheelCount { get; set; }
/// <summary>Number of Nuts Per Wheel</summary>
int NutsPerWheel { get; set; }
/// <summary>Wheel Diameter</summary>
int WheelDiameter { get; set; }
}
/// <summary>Vehicle Wheels Object, Implements IWheel</summary>
public class Wheel : IWheel
{
//Initiate Child Objects
public Wheel()
{
//Create A New Instance of The Tyre Pressure Class
TyrePressure = new TyrePressure();
}
TyrePressure tyrePressure;
public TyrePressure TyrePressure
{
get { return tyrePressure; }
set { tyrePressure = value; }
}
int wheelCount;
public int WheelCount
{
get { return wheelCount; }
set { wheelCount = value; }
}
int nutsPerWeel;
public int NutsPerWheel
{
get { return nutsPerWeel; }
set { nutsPerWeel = value; }
}
int wheelDiameter;
public int WheelDiameter
{
get { return wheelDiameter; }
set { wheelDiameter = value; }
}
}
/// <summary>Vehicle Interface</summary>
interface IVehicle
{
/// <summary>Vehicle Wheel Data</summary>
Wheel Wheels { get; set; }
/// <summary>Get Current Fuel LEvel</summary>
int GetFuelLevel { get; }
/// <summary>Get Vehicles Current Speed</summary>
int Speed { get; }
/// <summary>Apply Brake Routine</summary>
void ApplyBrake();
/// <summary>Accelerate Routine</summary>
void Accelerate();
/// <summary>Change Gear Routine</summary>
void ChangeGear();
/// <summary>Event Hnadlers</summary>
event EventHandler<int> MaximumSpeed;
event EventHandler Stopped;
event EventHandler<int> FuelWarning;
}
/// <summary>Car Class Implements IVehicle</summary>
class Car : IVehicle
{
/// <summary>Initiat A New Car</summary>
public Car()
{
fuel = 100;
wheels = new Wheel();
}
/// <summary>Define Event Handlers</summary>
public event EventHandler<int> MaximumSpeed;
public event EventHandler Stopped;
public event EventHandler<int> FuelWarning;
int fuel;
/// <summary>Get Remaining Fuel Level</summary>
public int GetFuelLevel { get { return fuel; } }
int speed;
/// <summary>Get Car's Current Speed</summary>
public int Speed { get { return speed; } }
/// <summary>Apply Brake</summary>
public void ApplyBrake()
{
Console.WriteLine("Screeeeeech!");
speed -= 10;
//If the speed is, or is below 0
if (Speed <= 0)
{
//Correct any mistaken negative values.
speed = 0;
EventHandler handler = Stopped;
if (handler != null)
{
//Inform the driver they have stopped.
handler(this, null);
}
}
}
/// <summary>Accelerate</summary>
public void Accelerate()
{
EventHandler<int> handler = MaximumSpeed;
//Check to see if the car has reached it's speed limit.
if (Speed >= 100 && handler != null)
{
//Alert the driver the maximum speed has been reached.
handler(this, Speed);
return;
}
//Increase the car's speed.
speed += 10;
fuel -= 1;
Console.WriteLine("Vaarooooooooom!");
}
/// <summary>Change Gear</summary>
public void ChangeGear()
{
//Not used to a clutch...
Console.WriteLine("Crunch, Clunk, Grrrrrrrr");
fuel -= 1;
}
Wheel wheels;
/// <summary>Car Wheel Data</summary>
public Wheel Wheels
{
get { return wheels; }
set { wheels = value; }
}
}
Any advice or suggestions would be much apreciated.