[ajug-members] correct interface programming
521
521 at ofig.org
Fri Jul 25 15:14:16 EDT 2008
J.Wilson: in other words, for me (a newbe), the Growable interface must only define methods(actions) that are related to growing, wether it be a crystal or a tree; but what if they grow in a different fasion? say a tree or a crystal... the Growable interface does not define how something grows? it only supplies a GrowRate method and a getCurrentSize method or the like...? am i saying this correctly?
barclay
On Fri, 25 Jul 2008 14:46:49 -0400, Wilson, Jeff wrote
> Of course, the real point of interfaces is to allow a client to only depend on the methods declared in that one interface and not care about the actual implementation.
>
> For example, I can use the Growable interface with something that is neither a Plant or a Tree:
>
> public class Crystal implements Growable {
> public double getGrowthRate() { return 0.023; }
> public Dimension getCurrentSize() { return new Dimension (1.2, 3.0); }
> }
>
> Now I can write a class that predicts the sizes of any thing that implements Growable:
>
> public class GrowthPredictor {
> public Dimension predictSize(int numberOfPeriods, Growable growable) {
> double width = growable.getCurrentSize().width();
> double height = growable.getCurrentSize().height();
> double factor = numberOfPeriods * growable.getGrowthRate();
> width += (width * factor);
> height += (height * factor);
>
> return new Dimension(width, height);
> }
> }
>
> This new class doesn't care how many interfaces its argument (growable) implements, as long as it implements the Growable interface. This method can be compiled without having any access to Tree or Crystal -- it isn't bound to the class, but the interface. In fact, this class can be compiled before Tree and Crystal classes even exxist, and it will work just fine when, in the future, you pass it a Tree or a Crystal.
>
> +jeff (MJW)
> ---
>
> "Foreploy: Any misrepresentation about yourself for the purpose of obtaining sex."
>
> ---------------------------------------------
> M. Jeff Wilson, Lead Member Technical Staff
> AT&T Services, Inc.
> Operations & Service Dev
> jw9615 at att.com
> +1 404.499.7235
>
> > -----Original Message-----
> > From: Wilson, Jeff
> > Sent: Thursday, July 24, 2008 4:54 PM
> > To: ajug-members at ajug.org
> > Subject: Re: [ajug-members] correct interface programming
> >
> > Here's some perfectly valid code:
> >
> > public interface Growable {
> > double getGrowthRate();
> > Dimension getCurrentSize();
> > }
> >
> > public interface Shady {
> > boolean isInShade(Point pt);
> > }
> >
> > public interface LumberSource {
> > double getPotentialBoardFeet();
> > boolean isReadyToCut();
> > }
> >
> > public class Tree extends Plant implements Growable, Shady, LumberSource {
> > private double width;
> > private double height;
> >
> > public Dimension getCurrentSize() { return new Dimension(width, height); }
> > public boolean isInShade(Point pt) { return false; }
> > public double getPotentialBoardFeet() { return 2000.0; }
> > public boolean isReadyToCut() { return false; }
> > }
> >
> > public class Plant {
> > public double getGrowthRate() { return 0.42; }
> > }
> >
> > No, Tree can implement any number of interfaces, there is no limit.
> >
> > Yes, Tree must have a method declaration for every method in all if the interfaces. HOWEVER, a method declared in a
> > subclass of Tree with the same signature as a method in one of Tree's interfaces satisfies that contract. For
> > example, in the code above, the getGrowthRate() method of the Growable interface is met by the Plant class. That is
> > perfectly valid.
> >
> > Yes, by implementing an interface, this is a contract -- the compiler will force you to have a method for every
> > method in an interface your class implements, either in the class or one of its parent classes.
> >
> >
> >
> > +jeff (MJW)
> > ---
> >
> > "Foreploy: Any misrepresentation about yourself for the purpose of obtaining sex."
> >
> > ---------------------------------------------
> > M. Jeff Wilson, Lead Member Technical Staff
> > AT&T Services, Inc.
> > Operations & Service Dev
> > jw9615 at att.com
> > +1 404.499.7235
> > > -----Original Message-----
> > > From: 521 [mailto:521 at ofig.org]
> > > Sent: Thursday, July 24, 2008 3:43 PM
> > > To: ajug-members at ajug.org
> > > Subject: [ajug-members] correct interface programming
> > >
> > > am i saying this correctly?, that which is below... -barclay
> > >
> > > {{{
> > > if i program to an interface, i do not have to worry about the number of
> > > interfaces that are implemented, do i? but do i not have to use all methods
> > > in an implemented interface? i do have to- it's a contract. correct? and
> > > because you can only extend one class, you have no contract there. correct?
> > > can someone see what my question is?
> > >
> > > a tree; a tree can grow, a tree can provide shade; and a tree can provide
> > > lumber for table-tennis.
> > >
> > > so my tree class is designed...
> > >
> > > then if i am implementing a 'tree'- it depends on which implementation i use
> > > thus have a contract to use certain methods
> > > }}}
> > >
> > >
> > > _______________________________________________
> > > ajug-members mailing list
> > > ajug-members at ajug.org
> > > http://www.ajug.org/mailman/listinfo/ajug-members
> >
> > *****
> >
> > The information transmitted is intended only for the person or entity to which it is addressed and may contain
> > confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or
> > taking of any action in reliance upon this information by persons or entities other than the intended recipient is
> > prohibited. If you received this in error, please contact the sender and delete the material from all computers.
> > GA622
> >
> >
> >
> > _______________________________________________
> > ajug-members mailing list
> > ajug-members at ajug.org
> > http://www.ajug.org/mailman/listinfo/ajug-members
>
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.ajug.org/pipermail/ajug-members/attachments/20080725/caa1ca08/attachment-0001.html
More information about the ajug-members
mailing list