tags:

views:

310

answers:

7

Hi,

I want to build a nice API (C#) to make it easier for people to consume, I think I've seen this before and want to know how to do this:

MyNamespace.Cars car = null;

if(someTestCondition)
       car = new Honda();
else    
       car = new Toyota();

car.Drive(40);

Is this possible? If so, what needs to be done?

+1  A: 

Make a class named Cars. Give it the Drive method. Extend that base class in your Honda and Toyota classes.

Aaron Smith
+9  A: 
Interface Car
{
void Drive(int miles);
}

class Honda : Car
{
...
}
class Toyota : Car
{
...
}
Otávio Décio
You beat me to it. I'll remove mine
Jacob Adams
that should be interface ICar
foson
Hey man, no need - this is no competition :)
Otávio Décio
@foson - I did that at first but changed it to Car so it would fit with his sample code.
Otávio Décio
@ocdecio.myopenid.com, Then change your class lines to use "Car" instead of "ICar" =]
strager
@strager: yeah, you're right. oh well.
Otávio Décio
+6  A: 

You could do this a couple of different ways. You could declare an abstract base class or you could have an interface that your object implement. I believe the "C#" preferred method would be to have an interface. Something like:

public interface ICar
{
 public Color Color { get; set; }

 void Drive(int speed);
 void Stop();

}

public class Honda : ICar
{

 #region ICar Members

 public Color Color { get; set; }

 public void Drive(int speed)
 {
  throw new NotImplementedException();
 }

 public void Stop()
 {
  throw new NotImplementedException();
 }

 #endregion
}

public class Toyota : ICar
{
 #region ICar Members

 public Color Color { get; set; }

 public void Drive(int speed)
 {
  throw new NotImplementedException();
 }

 public void Stop()
 {
  throw new NotImplementedException();
 }

 #endregion
}
Steven Behnke
A: 
namespace MyNameSpace
{
  public interface Cars
  {
    public void Drive(int miles);
  }
  public class Honda : Cars
  {
    public void Drive(int miles) { ... }
  }
  public class Toyota : Cars
  {
    public void Drive(int miles) { ... }
  }
}
orip
i changed my code, is your solution still accurate?
Blankman
Interesting how some of us saw that as miles driven and others of us saw it as the speed the car was moving at. A think we need a more complete functional spec. ;)
Steven Behnke
A: 

Don't forget the namespace, also see my comment in the question about variable names

namespace MyNamespace {
    public interface Cars {
     void Drive(int speed);
    }

    public class Honda : Cars {
     public void Drive(int speed) { 
      }
    }
    public class Toyota : Cars {
     public void Drive(int speed) {

     }
    }
}
Bob
A: 

Make an abstract class called Cars with an abstract method called Drive(). Subclass it and add implementations.

Dmitri Nesteruk
+5  A: 

I see everyone is pushing interface / abstract base class changes to you. The pseudocode you provided more or less implies you already have this in place.

I'll pose something else:

You'll want to create a "CarFactory" that will return a specific implementation of your base class / interface. The Create method can take your test conditions as parameters so you create the correct car.

EDIT: Here's a link from MSDN -- http://msdn.microsoft.com/en-us/library/ms954600.aspx

EDIT: See the comments for another link.

Austin Salonen
Based on the changes to his question, I think your answer is the best fit now.
Aaron Smith
Posting an example would be really helpful, because he probably hasn't dealt with factories before.
strager
http://www.dofactory.com/Patterns/PatternAbstract.aspx for an Abstract Factory Example.
Steven Behnke