Page 1 of 1
Interface/Abstract programming
Posted: Wed Dec 13, 2006 12:57 pm
by MrPotatoes
i know how to use them more-or-less but are there any tutorials out there that show you how to use these? i just need some psuedo code really.
what i normally do is define the interface and make an abstract class that impliments that interface. then with that abstract is what i make my real classes out of IF they need that class. otherwise i just impliment the interface directly.
also, what would you classify both (abstract/interface) of these under? they are basically class templates or diagrams. just wondering
thanx for the help ahead of time

Posted: Wed Dec 13, 2006 1:28 pm
by Begby
They are kind of like class templates. An interface is more like a contract though, while an abstract class is similar to an interface but also includes some common functionality to be passed into an implementation.
An interface is used to guarantee that a particular class has methods that take specific arguments and/or return something consistent while encapsulating what goes on.
Here is a simple example.
This is bad because every time you want to add a new type of food you have to modify the dinner class. The classes are not loosely coupled meaning a change to one class requires changes to another.
Code: Select all
class taco
{
function fold() { ..stuff..}
}
class burrito
{
function roll() { ..stuff.. }
}
class dinner()
{
function make( object $food )
{
if( $food instanceof burrito )
$food->roll() ;
if( $food instanceof taco )
$food->fold() ;
$this->serve($food) ;
}
}
This is good. Now we are programming to an interface. Dinner doesn't care what is passed to it as long as it implements the food contract. Now if you add a new type of food you don't have to bother editing the dinner class.
Code: Select all
interface food
{
function prepare() ;
}
class taco implements food
{
function prepare() { ..stuff..}
}
class burrito implements food
{
function prepare() { ..stuff.. }
}
class dinner()
{
function make( food $food )
{
$food->prepare() ;
$this->serve($food)
}
}
Posted: Wed Dec 13, 2006 10:38 pm
by Ambush Commander
Right: the main thing about these language contracts is that they formalize something that could very well be done without them, but requires more discipline.
Posted: Mon Dec 18, 2006 9:56 am
by raghavan20
It is important to make decision on whether to use interface or abstract class.
After reading a few chapters in some books related to design patterns I generally see interfaces are preferred.
If you think that a set of operations will be implemented by set of classes in different ways then put the set of operations as interface methods.
if you think only a partial set of operations will be implemented differently by a set of classes then use abstract class.
but do not use abstract class if you are not going to implement all the methods in it.
abstract class is good for centralizing code but the problem is, it cannot cope up with change. Also overriding (abstract)base class method in child class is not preferred generally especially when done in many classes; it is better to declare that function as abstract and handle in child classes.
Posted: Mon Dec 18, 2006 1:33 pm
by timvw