Results 1 to 8 of 8

Thread: Interfaces

  1. #1
    Mantisser is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default 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

    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 ); }
      }
    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?

    Thanks, am rather confused.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default 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.

    ** This space for rent **

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,432
    Blog Entries
    7
    Rep Power
    20

    Default Re: Interfaces

    A Decorator is also called a 'Wrapper'; your Decorator class is very well capable of wrapping another Decorator instance.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Mantisser is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Re: Interfaces

    So any class that (implements an interface) and (contains a class that implements the same interface) is termed a wrapper?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,432
    Blog Entries
    7
    Rep Power
    20

    Default Re: Interfaces

    Quote Originally Posted by Mantisser View Post
    So any class that (implements an interface) and (contains a class that implements the same interface) is termed a wrapper?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Mantisser is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default 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.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,432
    Blog Entries
    7
    Rep Power
    20

    Default Re: Interfaces

    Quote Originally Posted by Mantisser View Post
    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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Mantisser is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default 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

  1. Interfaces
    By dalu in forum New To Java
    Replies: 4
    Last Post: 04-26-2011, 12:22 PM
  2. Interfaces
    By computerquip in forum New To Java
    Replies: 19
    Last Post: 09-08-2009, 04:58 PM
  3. Interfaces
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 09:57 PM
  4. interfaces..
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 05:52 PM
  5. Interfaces
    By imran_khan in forum New To Java
    Replies: 5
    Last Post: 07-30-2007, 08:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •