Blankman
Blankman

Reputation: 266988

How do I implement this type of OOP structure?

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?

Upvotes: 1

Views: 412

Answers (7)

Austin Salonen
Austin Salonen

Reputation: 50215

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.

Upvotes: 5

Dmitri Nesteruk
Dmitri Nesteruk

Reputation: 23789

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

Upvotes: 0

Bob
Bob

Reputation: 99704

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) {

        }
    }
}

Upvotes: 0

Otávio Décio
Otávio Décio

Reputation: 74250

Interface Car
{
void Drive(int miles);
}

class Honda : Car
{
...
}
class Toyota : Car
{
...
}

Upvotes: 10

orip
orip

Reputation: 75427

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) { ... }
  }
}

Upvotes: 0

Steven Behnke
Steven Behnke

Reputation: 3344

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
}

Upvotes: 6

Aaron Smith
Aaron Smith

Reputation: 3362

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

Upvotes: 1

Related Questions