Skip to main content
gamedev.net
🔒 Locked

Interface Design [SOLVED]

Started by Thaligar 5 replies 1k views
Original Post
Thaligar
Thaligar
what is the better interface, one with only pure virtual functions (i'm using C++) or one which also has some member variables and some implemented functions so the derived class haven't got to care about all the stuff and to ensure that some specific functions have really the assumed behavior, it is probably an opinion than a design decision but i'm interested how you would handle this, greets and thanks in advance for your opinion, tgar [Edited by - Thaligar on January 4, 2007 9:25:49 AM]
ToohrVyk
ToohrVyk
The better interface is the one that is most adapted to the task that the object is designed to accomplish. Without knowledge of the task, or even of the interface, it isn't possible to say that one is better than the other, as both fit in certain situations but not in others.

Most people, however, will note that an Interface is usually used to refer to a class with no member variables or implemented methods, and Abstract Class is used to refer to a class which has some pure virtual methods along with some implemented methods and some variables. In this light, your question does not really make any sense, as the second option isn't an Interface to begin with.
Thaligar
Thaligar
that was fast, thanks ToohrVyk,

i tought that the interface with member vars and so is a bad programming habit,

thanks again,
tgar
Glak
Glak
Do you really need an interface? Maybe you want an abstract class. Maybe you really need some plain-old-data.

Right now I am writing a compiler for a high-level language of my own design. I rewrote the tokenizer (the part that turns individual characters into identifiers, keywords, operators, and literals) recently. My original implementation used virtual functions. My current implementation uses gotos. My current implementation is far easier to understand and is much shorter. I am mostly done with the parser and no virtual funtions there either. No inheritance in the program so far either. I thought that Expression (my parse tree node type) would be subclassed but I am finding that it works better to just let it evolve.

My point? Don't always go with what is best according to the book you are currently reading. Fads come and go. Different techniques work for different people. Different approaches work for different problems. Just know all of your options and use common sense.
Sneftel
Sneftel
Between an interface and an abstract class, it's often a good idea to have both. That is, have an interface which defines the class contract, and an abstract class with the parts which will almost certainly be common to all classes implementing the interface. This gives you the convenience of the boilerplate code in the abstract class, without causing inconvenience if later on a particular class needs to do something the abstract class can't handle.
Fred304
Fred304
Quote:
Original post by Glak
My original implementation used virtual functions. My current implementation uses gotos.

Jehova!
Thaligar
Thaligar
thanks for your opinions,

Sneftel, your approach looks interesting, i'll give it a try,

thanks again for your time,
greets
tgar

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.