Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding Against Class or Interface 3

Status
Not open for further replies.

golcarlad

Programmer
Nov 23, 2004
232
GB
What are the pros and cons of coding against a class vs coding against an interface? e.g. why would you decide to write:

IFoo foo = new Foo();

vs

Foo foo = new Foo();

Ive heard of trends e.g. I always code against the interface etc, just dont get why you would do one thing vs another, surely theres a reason why you do something, not just follow a trend for the sake of it.

 
if you need to alter Foo you can create a new object which implments IFoo without changing the class Foo.

this helps adhere to the open/close principle. you can also then use the factory methodology to instiantiate new concreate objects, because they all inherit IFoo
Code:
interface IFoo { }
class Foo1 : IFoo { }
class Foo2 : IFoo { }

public class FooFactory()
{
   public IFoo Create(int fooType)
   {
       case(fooType)
       {
           case 1:
               return new Foo1();
           case 2:
               return new Foo2();
           default:
               throw new Exception();
       }
   }
}


//in code
IFoo bar = new FooFactory().Create(1);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
You generally inherit from another class when you want to have basic functionality built in.

For example: You have a class called Automobile. In an interface you would define a few things that it can do

StartEngine()
SteerLeft()
SteerRight()
ShiftGears()
TurnOff()

Now if you know that every Automobile is going to start using the StartEngine() method - then why bother writing it every time you create a new Automobile class? You can just make that method do something and if the next class that inherits Automobile needs to do something slightly different, then you can override that method.

 
It really all depends on your requirements whether you program to an interface or to a concrete class. Programming to an interface helps you encapsulate what varies so that when requirements change or are added you can better handle the situation and not have to change the use of the class. But programming to an interface doesn't just mean use an inteface(ex IFormattable). The use of an abstract class also is considered programming against an interface. The difference is that the abstract class has some implementation where as the Interface doesn't.

I'm not sure I would call programming to an interface a trend. If you were a java programmer this would be second nature and in a general sense makes your application more flexible. It sounds like you are interested in the reasoning behind programming to an interface and people have written books about the topic. I would pick up Head First Design Patterns (ISBN-10: 0596007124) It's easy to read and will answer your question plus some.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top