On Wed, Nov 18, 2009 at 12:53 PM, Robert B. <
auto...@gmail.com> wrote:
> Ok, I think I'm getting the purpose of RobertImplementation and
> MyImplementation totally confused. Let's stick with CommonImplementation and
> FullImplementation (realizing that each user may write any
> FullImplementation).
OK.
> type CommonMethods interface {
> FuncA();
> FuncB();
> }
> // A user implements these
> type RequiredMethods interface {
> Func1();
> Func2();
> }
No. You want this:
type FullMethods interface {
FuncA();
FuncB();
// A user implements these
Func1();
Func2();
}
> type CommonImplementation struct {
> userImpl RequiredMethods;
> }
type CommonImplementation struct {
self FullMethods; // Or call this userImpl if you want
}
> // FuncA() and FuncB() may call anything in userImpl.
Actually in FullMethods. Which means that they can make method calls
to FullImplementation methods that are not implemented in
CommonImplementation. The key here for polymorphism is that a method
call to the CommonImplementation doesn't lose the full object because
the full object is still embedded in self.
> func (this *CommonImplementation) FuncA() { ... }
> func (this *CommonImplementation) FuncB() { ... }
> type FullImplementation struct {
> self *CommonImplementation; // Ben's requirement
> // CommonImplementation; // why not just embed like this?
> }
The second embedding is the one to use. My suggestion put self inside
the CommonImplementation. Putting self inside of FullImplementation
means that you lose the full object when you call the base class.
That prevents you from, for instance, being able to see that the child
has overridden a method.
> // Func1() and Func2() may call anything in CommonImplementation
> // or FullImplementation.
> func (this *FullImplementation) Func1() { ... }
> func (this *FullImplementation) Func2() { ... }
>
> My question is, is the above the correct Go idiom that should be used when
> rethinking abstract base types from Java?
With the significant revisions I described this Go idiom will let you
port a design with abstract base types to Go. However discovering
alternate designs that are more natural for Go will often result in
simpler code that performs better.
Cheers,
Ben