Originally Posted by
Eman
I'm sorry but I still don't get it?
What is the "interface". I haven't done anything like Linkedlist can you make it simpler please.
"The point of abstraction is to protect your code from changes"
What changes? Changes from another programming? Could anyone please explain with a simple code? or real life example?
and thanks for the quick reply. Didn't expect any.
Very shortly (sorry if I'm explaining it in a bad way):
Interface is something that allows communication between two devices/objects. Interface may refer to an application programming interface as well as to a specific kind of object (google 'IUnknown'). In the first case, 'interface' describes a collection of classes or functions (or anything else) that allows to write programs under specific environment.
Let's say, you need a software controlling some device, let's say it's a mouse. You want this software to work with different kinds of mice. Of course, all of the mice can click with 2/3 buttons and scroll with the middle one. This is their only common feature. Some mice are optical, some are not.
Your code to control the mouse can be quite compilcated, taking into consideration many factors (like the time between two clicks, speed of moving, set-up for left-handed or right-handed).
The most basic thing you need is scanning the mouse movement. To do this, you would probably acquire light diode status (in the case of an optical mouse) and status of rollers (in the case of a mechanical one).
Because you check the mouse status in many places of your software, it would be a very bad idea to check what kind of mouse is currently plugged in. Here comes in the interfaces.
Code:
class MouseListener {
public:
virtual int GetVerticalShift();
virtual int GetHorizontalShift();
virtual int GetScrollerShift();
virtual bool IsLeftDown();
virtual bool IsRightDown();
virtual bool IsMiddleDown();
};
As you can see, with these 6 methods given you do not care what kind of mouse you have currently plugged in, because it's an encapsulated feature. All of the technical principles are hidden in this class (checking what kind of mouse is plugged in, etc).
MouseListener provides you the most basic operations for every kind of mouse - if the mouse does not have a scroller, the GetScrollerShift would always return 0.
We could go even farther. We could create a class:
Code:
// any pointing device (mouse, touchpad)
class PointingDeviceListener {
public:
virtual int GetVerticalShift();
virtual int GetHorizontalShift();
};
// simple two-button mouse
class Mouse2Listener : public PointingDeviceListener {
public:
virtual bool IsLeftDown();
virtual bool IsRightDown();
};
// three-button mouse with scroller
class Mouse3Listener : public MouseListener {
public:
virtual bool IsMiddleDown();
virtual int GetScrollerStatus();
};
MouseListener is something that you use in every point of your program. If the implementation of the MouseListener changes, your program will not be affected.
And if you want to use this code for touchpad, just plug in the touchpad and the code will work.
And there is much more to say.