Since Go uses an composition system instead of (multiple) inheritance, I'm just wondering about these 3 code snippets. Go says they force the programmer to use composition.
A) should be (almost) correct Go-Code, B) pseudo C) pseudo
Imho the result will always be the same on all three codes, beside the fact, that B) and C) can be used for even more stuff and A) forces you to stick to composition?
Even if you assume B) to not have the sort-method inside of the class but - lets say global like A) doesn't make a real difference oO
A) Go code:
interface Sort
Len()
Less(i, j int) bool
Swap(i, j int)
func (qs *Sort) sort()
doTheSorting
type MyData struct {
var value int
}
func (s *MyData) Len() { ... }
func (s *MyData) Less(i, j int) bool { ... }
func (s *MyData) Swap(i, j int) { ... }
B) Looks like Inheritance but can imho be seen as embedded, according to how the compiler works.
class Sort
public sort() { ... }
abstract Len()
abstract Less(i, j int) bool
abstract Swap(i, j int)
C)
interface SortInterface
void Len()
bool Less(i, j int)
void Swap(i, j int)
class Sort implements SortInterface
public sort() { ... }
Usage B and C:
class MyClass **embed** Sort
int value
void Len() { ... }
bool Less(i, j int) { ... }
void Swap(i, j int) { ... }