Object serialization: Difference between revisions
Content added Content deleted
(→{{header|Go}}: updates for two library changes) |
(→{{header|Go}}: task conformance: polymorphic func prints rather than returns string. Also a bug fix, more readable object initialization, and a rewrite of the introduction.) |
||
Line 499: | Line 499: | ||
Go has a few choices for serialization. The method shown here is the native method, which is compact and type-aware. |
Go has a few choices for serialization. The method shown here is the native method, which is compact and type-aware. |
||
A note on object oriented stuff, |
A note on object oriented stuff, the object hierarchy required by the task description is implemented here with embedded structs. The polymorphism required by the task description is handled nicely with an interface. The functional polymorphism is orthogonal to the object hierarchy, not conflated with it. |
||
<lang go>package main |
<lang go>package main |
||
Line 507: | Line 507: | ||
"os" |
"os" |
||
) |
) |
||
type printable interface { |
|||
print() |
|||
} |
|||
func main() { |
func main() { |
||
// create instances |
// create instances |
||
animals := [] |
animals := []printable{ |
||
& |
&Animal{Alive: true}, |
||
& |
&Cat{}, |
||
& |
&Lab{ |
||
Dog: Dog{Animal: Animal{Alive: true}}, |
|||
Color: "yellow", |
|||
⚫ | |||
&Collie{Dog: Dog{ |
|||
Animal: Animal{Alive: true}, |
|||
⚫ | |||
⚫ | |||
} |
} |
||
Line 520: | Line 530: | ||
fmt.Println("created:") |
fmt.Println("created:") |
||
for _, a := range animals { |
for _, a := range animals { |
||
a.print() |
|||
} |
} |
||
Line 545: | Line 555: | ||
return |
return |
||
} |
} |
||
var clones [] |
var clones []printable |
||
gob.NewDecoder(f).Decode(&clones) |
gob.NewDecoder(f).Decode(&clones) |
||
if err != nil { |
if err != nil { |
||
Line 555: | Line 565: | ||
fmt.Println("\nloaded from objects.dat:") |
fmt.Println("\nloaded from objects.dat:") |
||
for _, c := range clones { |
for _, c := range clones { |
||
c.print() |
|||
} |
} |
||
} |
} |
||
type |
type Animal struct { |
||
Alive bool |
Alive bool |
||
} |
} |
||
func (a * |
func (a *Animal) print() { |
||
if a.Alive { |
if a.Alive { |
||
fmt.Println(" live animal, unspecified type") |
|||
} else { |
} else { |
||
fmt.Println(" dead animal, unspecified type") |
|||
} |
} |
||
return r + " animal, unspecified type" |
|||
} |
} |
||
type |
type Dog struct { |
||
Animal |
|||
ObedienceTrained bool |
ObedienceTrained bool |
||
} |
} |
||
func (d * |
func (d *Dog) print() { |
||
switch { |
|||
case !d.Alive: |
|||
fmt.Println(" dead dog") |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
default: |
|||
⚫ | |||
⚫ | |||
} |
} |
||
⚫ | |||
} |
} |
||
type |
type Cat struct { |
||
Animal |
|||
LitterBoxTrained bool |
LitterBoxTrained bool |
||
} |
} |
||
func (c * |
func (c *Cat) print() { |
||
switch { |
|||
case !c.Alive: |
|||
fmt.Println(" dead cat") |
|||
⚫ | |||
⚫ | |||
default: |
|||
⚫ | |||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
⚫ | |||
type |
type Lab struct { |
||
Dog |
|||
Color string |
Color string |
||
} |
} |
||
⚫ | |||
func (l * |
func (l *Lab) print() { |
||
var r string |
|||
if l.Color == "" { |
if l.Color == "" { |
||
r |
r = "lab, color unspecified" |
||
} else { |
} else { |
||
r = l.Color + " lab" |
r = l.Color + " lab" |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
case l.ObedienceTrained: |
|||
⚫ | |||
default: |
|||
⚫ | |||
} |
} |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
} |
} |
||
type |
type Collie struct { |
||
Dog |
|||
CatchesFrisbee bool |
CatchesFrisbee bool |
||
} |
} |
||
func (c * |
func (c *Collie) print() { |
||
r := "collie" |
|||
⚫ | |||
return "dead " + r |
|||
} |
|||
switch { |
switch { |
||
⚫ | |||
fmt.Println(" dead collie") |
|||
case c.ObedienceTrained && c.CatchesFrisbee: |
case c.ObedienceTrained && c.CatchesFrisbee: |
||
fmt.Println(" trained collie, catches frisbee") |
|||
case c.ObedienceTrained && !c.CatchesFrisbee: |
case c.ObedienceTrained && !c.CatchesFrisbee: |
||
fmt.Println(" trained collie, but doesn't catch frisbee") |
|||
case !c.ObedienceTrained && c.CatchesFrisbee: |
case !c.ObedienceTrained && c.CatchesFrisbee: |
||
fmt.Println(" collie, not trained, but catches frisbee") |
|||
case !c.ObedienceTrained && !c.CatchesFrisbee: |
case !c.ObedienceTrained && !c.CatchesFrisbee: |
||
fmt.Println(" collie, not trained, doesn't catch frisbee") |
|||
} |
} |
||
⚫ | |||
}</lang> |
}</lang> |
||
Output: |
Output: |