add default implementation to interfaces ( similar to abstract class in java)

2,942 views
Skip to first unread message

ChrisLu

unread,
Sep 1, 2012, 8:42:37 PM9/1/12
to golan...@googlegroups.com
Hi, I am having trouble to add default implementation of an interface, to simulate inheritance. The concept is similar to java's abstract class default implementation. But I don't know the right approach in Go.

For example, I want to build a graph of servers, on different racks and data centers.
Currently, I have these code:

type Node struct{
  parent *Node
  children []*Node
}
//several default function implementations
func (*Node) AddChild(){
  ...
}

type Server struct {
  Node
  ipAddress string
  ...
}
type Rack struct {
  Node
  ipAddressRange string
  ...
}
type DataCenter struct {
  Node
  ipAddressRange string
  ...
}

However, server.Node.parent is a *Node, and can not be converted to a Rack object.

Chris

DisposaBoy

unread,
Sep 2, 2012, 3:27:53 AM9/2/12
to golan...@googlegroups.com
From the looks of it, the common thing is an ip address or range thereof so it sounds like you actually want Node to be an interface as opposed to a  struct. e.g. you can create a node interface that all the relevant types implement direct or indirect via embedding a base struct. It's not clear what the relations are between the types but assuming an ipAddress can be represented as an ipAddressRange then it seems ok to put that in the base and then you need only implement an IpAddressRange, something like: http://play.golang.org/p/NhM-sqYpNFm





 

ChrisLu

unread,
Sep 2, 2012, 4:29:03 AM9/2/12
to golan...@googlegroups.com
Thanks for replying! My concern is that if Node is an interface, I need to put the implementation of AddChild() to each of the struct Rack and DataCenter.

Chris
 

John Asmuth

unread,
Sep 2, 2012, 7:03:40 AM9/2/12
to golan...@googlegroups.com
The right approach is to not use inheritance as a building block in your design.

If you tell us what you want the code to do, functionally (rather than "I want it to have inheritance"), we can help you use the tools go provides effectively.

Jesse McNelis

unread,
Sep 2, 2012, 9:23:19 AM9/2/12
to ChrisLu, golan...@googlegroups.com
On Sun, Sep 2, 2012 at 6:29 PM, ChrisLu <chri...@gmail.com> wrote:
> Thanks for replying! My concern is that if Node is an interface, I need to
> put the implementation of AddChild() to each of the struct Rack and
> DataCenter.

AddChild() seems like it would be a two line method not exactly a
worrying amount of code duplication.
But you can just do this.

type Node interface{
AddChild(Node)
}

type Element struct{
parent Node
children []Node
}

func (*Element) AddChild(Node){}

type Server struct {
Element
ipAddress string
...
}
type Rack struct {
Element
ipAddressRange string
...
}
type DataCenter struct {
Element
ipAddressRange string
...
}

Server, Rack, DataCenter all satisfy the Node interface.


--
=====================
http://jessta.id.au

ChrisLu

unread,
Sep 3, 2012, 5:25:59 PM9/3/12
to golan...@googlegroups.com, ChrisLu, jes...@jessta.id.au
Thanks! This helps a lot!

Here is what I concluded: implementing the abstract class in Go should be split into two tasks.
1. implement an interface
2. implement an interface implementation, and mixin the implementation to the "inheriting" class.

Chris

adri...@googlemail.com

unread,
Sep 12, 2016, 11:17:40 PM9/12/16
to golang-nuts, chri...@gmail.com, jes...@jessta.id.au

Agreed, that said I'm still unclear while implementing interface, is it better to use anonymous or explicit interface embedding:
That might help too:  Abstract Classes in Go
Reply all
Reply to author
Forward
0 new messages