Results 1 to 8 of 8
Thread: Interfaces
- 05-15-2012, 04:48 AM #1
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Interfaces
Hi
Was just messing around with the decorator pattern and there are some things i don't understand about interfaces.
Take the following code from sourcemaker
Above the decorator class implements the LCD interface. Fair enough. The Decorator also contains (is that the right word?) an object of the LCD interface (private LCD inner), however, i thought that interfaces represent functionality, and a class which implements an inteface must contain its methods. What is the difference between containing an interface and implementing it?XML Code:static class Decorator implements LCD { private LCD inner; public Decorator( LCD i ) { inner = i; } public void write( String[] s ) { inner.write( s ); } public void read( String[] s ) { inner.read( s ); } }
Thanks, am rather confused.
- 05-15-2012, 09:57 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,475
- Rep Power
- 16
Re: Interfaces
It doesn't contain an interface.
It contains an object of a class that implements that interface.
The interface is a contract that simply says "something that implements me has this functionality".
So your Decorator object will contain an object that does whatever an LCD does.
Got to say, that's not much of a Decorator since it doesn't actually add/change the functionality of the underlying LCD class.Please do not ask for code as refusal often offends.
- 05-15-2012, 10:00 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Interfaces
A Decorator is also called a 'Wrapper'; your Decorator class is very well capable of wrapping another Decorator instance.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-15-2012, 03:12 PM #4
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: Interfaces
So any class that (implements an interface) and (contains a class that implements the same interface) is termed a wrapper?
- 05-15-2012, 03:25 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Interfaces
Well, sort of; the wrapper should call the class it wraps and add to or change some of its functionality. Look at the classes in the IO section of the core library; most of those classes are decorators/wrappers. I also wrote a blog entry on this subject once (if I'm not mistaken); see the Blogs button near the top of this page.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-15-2012, 06:55 PM #6
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: Interfaces
Ok i read yor post. Is there any reason why you chose to represent the decorator class as an abstract superclass instead of a concrete superclass?
Thanks alot for your help.
- 05-15-2012, 07:01 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Interfaces
It is quite a common idiom: an interface defines what has to be done (here it's the definition of a decorator); an abstract class implements some methods common to all concrete decorators and it implements the interface. All the concrete classes (that extend from this abstract class) only have to implement their particular methods without having to worry about the hulla baloo implemented by the abstract super class.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-15-2012, 07:59 PM #8
Member
- Join Date
- May 2012
- Posts
- 6
- Rep Power
- 0
Re: Interfaces
Ok, it think i understand what your saying. By using an abstract class the concrete classes do not have to worry about the methods of the interface? Following from this premise if i was to use a concrete super class this would mean all the abstract classes would contain the interface methods?
If howver you wanted to overide the interface methods in the concrete classes this is still possible yes?
Similar Threads
-
Interfaces
By dalu in forum New To JavaReplies: 4Last Post: 04-26-2011, 12:22 PM -
Interfaces
By computerquip in forum New To JavaReplies: 19Last Post: 09-08-2009, 04:58 PM -
Interfaces
By jon80 in forum New To JavaReplies: 2Last Post: 05-03-2008, 09:57 PM -
interfaces..
By sireesha in forum New To JavaReplies: 5Last Post: 01-16-2008, 05:52 PM -
Interfaces
By imran_khan in forum New To JavaReplies: 5Last Post: 07-30-2007, 08:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks