Re: Interface Delegation or ??
On Dec 23, 5:40 pm, Codedigestion <ShreeMu...@gmail.com> wrote:
On Dec 23, 3:38 pm, Lew <l...@lewscanon.com> wrote:
t...@milkyway.gsfc.nasa.gov wrote:
Ah, but they can now create their own implementation of the interface=
and pass it in to some public method that takes the interface as an
input and thus get access to something they weren't able to get to
before... I.e.,
Lew wrote:
No previous implementation of the interface can have been passed to a
public method in a public class, since none of the type or its subtype=
s
were publicly visible. Your scenario cannot happen.
...
The actual interactions are precisely specified for Java.
If the public class is in the same package as the package-private interf=
ace,
it can get away with exporting the interface type via a public method.
While technically legal, it does raise a warning in my IDE. (NetBeans=
, as it
happens, but I'm pretty sure Eclipse supports this warning also.)
"Exporting non-public type through public API"
This pathological example does just that in the methods that set and get=
an
'InnerFace' instance. It makes 'Fimpl' a little difficult to use from=
outside
the package; the client code has to treat the signatures as though they =
used
'Object' instead of 'InnerFace' and they don't have access to the supert=
ype,
specifically not to its methods. (Not through the supertype reference=
, at least.)
While this usage is barely legal, the best advice is not to do it. Do=
n't
expose restricted visibility types to wider examination. If you make =
a type
package-private, it's for a reason. It's a type.
public class Fimpl implements Runnable
{
/** Keep the interface on the down-low.
*/
/* p-p */ static class NestedFimpl implements InnerFace
{
@Override
public void foo()
{
System.out.println( "NestedFimpl.foo()" );
}
}
private volatile InnerFace face;
/** No-arg constructor.
*/
public Fimpl()
{
this( null );
}
/* p-p */ Fimpl( InnerFace f )
{
face = (f == null? new NestedFimpl() : f);
}
/* p-p */ void setFace( InnerFace f )
{
face = (f == null? new NestedFimpl() : f);
}
/* p-p */ InnerFace getFace()
{
return face;
}
/** Delegate an action to <code>foo()</code>.
*/
@Override
public void run()
{
face.foo();
}
/** Expose the package-private interface to the whole world.
* @param f non-visible type, pass an <code>Object</code>.
*/
public void setInnerFace( InnerFace f )
{
setFace( f );
}
/** Expose the package-private interface to the whole world.
* @return non-visible type, treat as <code>Object</code>.
*/
public InnerFace getInnerFace()
{
return getFace();
}
}
--
Lew
Peace Rob,
I'm not exactly sure what you're trying to do, but as I can ascertain
from your first post, it seems that you're trying to use a Design
Pattern called the "Strategy Pattern". I think this is what you're
looking for, perhaps:
-----------------------------
public interface IPoint {
public void dosomething();
}
-----------------------------
public class Point2D implements IPoint {
public void doSomething() {
System.out.println("I'm 2D.")
}
}
-----------------------------
public class Point3D implements IPoint {
public void doSomething() {
System.out.println("I'm 3D.")
}
}
-----------------------------
public abstract class Vertex {
IPoint iPoint;
public Vertex(){
}
public void pleaseDoSomething() {
iPoint.doSomething();
}
}
----------------------------
public 2DVertex extends Vertex{
doSomething = new Point2D();
}
----------------------------
public 3DVertex extends Vertex{
doSomething = new Point3D();
}
----------------------------
I don't know if this code will even work. Quite frankly, I'm new to
this myself, but I think this may be what you're looking for; The
Strategy Pattern. Especially if you want to program to 'composition'
instead of 'implementation'. I've found out about all of this from
Head First Design Patterns:http://www.amazon.com/gp/product/0596007124?ie=
=UTF8&tag=myprfothusst1...
Let me know how this works for ya', would ya'? Well this stuff makes
a lot more sense in the book. Perhaps I've done a poor job of
explaining it. I think if you do a google search for "Strategy Design
Patterns" you may get better examples, God willing.
God Bless,
shree
package wikipedia.patterns.strategy;
// MainApp test application
class MainApp {
public static void main(String[] args) {
Context context;
// Three contexts following different strategies
context = new Context(new ConcreteStrategyA());
context.execute();
context = new Context(new ConcreteStrategyB());
context.execute();
context = new Context(new ConcreteStrategyC());
context.execute();
}
}
// The classes that implement a concrete strategy should implement
this
// The context class uses this to call the concrete strategy
interface IStrategy {
void execute();
}
// Implements the algorithm using the strategy interface
class ConcreteStrategyA implements IStrategy {
public void execute() {
System.out.println( "Called ConcreteStrategyA.execute()" );
}
}
class ConcreteStrategyB implements IStrategy {
public void execute() {
System.out.println( "Called ConcreteStrategyB.execute()" );
}
}
class ConcreteStrategyC implements IStrategy {
public void execute() {
System.out.println( "Called ConcreteStrategyC.execute()" );
}
}
// Configured with a ConcreteStrategy object and maintains a
reference to a Strategy object
class Context {
IStrategy strategy;
// Constructor
public Context(IStrategy strategy) {
this.strategy = strategy;
}
public void execute() {
strategy.execute();
}
}
---------------------------------------------
http://en.wikipedia.org/wiki/Strategy_pattern