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:
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, while I implementedthe object hierarchy required by the task description is implemented here with embedded structs, this method offers no polymorphism. The polymorphism required by the task description was for printing, which Gois handleshandled nicely with interfaces, and in fact, with an interface type already defined in the fmt package. ThusThe myfunctional polymorphic collectionpolymorphism is not a collection based on the common rootorthogonal ofto the object hierarchy, butnot onconflated thiswith common interface, fmt.Stringerit.
<lang go>package main
 
Line 507:
"os"
)
 
type printable interface {
print()
}
 
func main() {
// create instances
animals := []fmt.Stringerprintable{
&animalAnimal{Alive: true},
&catCat{},
&labLab{dog{animal{true}, false}, "yellow"},
&collie Dog: Dog{dogAnimal: Animal{animal{true},Alive: true}, false},
Color: "yellow",
},
&Collie{Dog: Dog{
Animal: Animal{Alive: true},
if d. ObedienceTrained: {true,
}},
}
 
Line 520 ⟶ 530:
fmt.Println("created:")
for _, a := range animals {
fmta.Printlnprint(" ", a)
}
 
Line 545 ⟶ 555:
return
}
var clones []fmt.Stringerprintable
gob.NewDecoder(f).Decode(&clones)
if err != nil {
Line 555 ⟶ 565:
fmt.Println("\nloaded from objects.dat:")
for _, c := range clones {
fmtc.Printlnprint(" ", c)
}
}
 
type animalAnimal struct {
Alive bool
}
 
func (a *animalAnimal) Stringprint() (r string) {
if a.Alive {
rfmt.Println(" = "live animal, unspecified type")
} else {
rfmt.Println(" = "dead animal, unspecified type")
}
return r + " animal, unspecified type"
}
 
type dogDog struct {
animalAnimal
ObedienceTrained bool
}
 
func (d *dogDog) Stringprint() string {
rswitch := "dog"{
ifcase !d.Alive {:
return fmt.Println("dead " + rdead dog")
ifcase ld.ObedienceTrained {:
}
return fmt.Println("trained " + rtrained dog")
if d.ObedienceTrained {
default:
return "trained " + r
return r + fmt.Println(" dog, not trained")
}
return r + ", not trained"
}
 
type catCat struct {
animalAnimal
LitterBoxTrained bool
}
 
func (c *catCat) Stringprint() string {
rswitch := "cat"{
ifcase !c.Alive {:
return fmt.Println("dead " + rdead cat")
ifcase c.LitterBoxTrained {:
return fmt.Println(" litter box trained cat" + r)
default:
return r + fmt.Println(" cat, not litter box trained")
}
if c.LitterBoxTrained {
return "litter box trained " + r
}
return r + ", not litter box trained"
}
 
type labLab struct {
dogDog
Color string
}
 
func (l *labLab) Stringprint() string {
rvar :=r "lab"string
if l.Color == "" {
r += "lab, color unspecified"
} else {
r = l.Color + " lab"
}
returnswitch r{
ifcase !l.Alive {:
return fmt.Println("dead " + dead", r)
case l.ObedienceTrained:
return fmt.Println("trained " + trained", r)
default:
return r + fmt.Printf(" %s, not trained\n", r)
}
if !l.Alive {
return "dead " + r
}
if l.ObedienceTrained {
return "trained " + r
}
return r + ", not trained"
}
 
type collieCollie struct {
dogDog
CatchesFrisbee bool
}
 
func (c *collieCollie) Stringprint() string {
r := "collie"
if !c.Alive {
return "dead " + r
}
switch {
ifcase !c.Alive {:
fmt.Println(" dead collie")
case c.ObedienceTrained && c.CatchesFrisbee:
r = fmt.Println("trained " + rtrained + "collie, catches frisbee")
case c.ObedienceTrained && !c.CatchesFrisbee:
r = fmt.Println("trained " + rtrained + "collie, but doesn't catch frisbee")
case !c.ObedienceTrained && c.CatchesFrisbee:
rfmt.Println(" += " collie, not trained, but catches frisbee")
case !c.ObedienceTrained && !c.CatchesFrisbee:
rfmt.Println(" += " collie, not trained, doesn't catch frisbee")
}
return r
}</lang>
Output: