lundi 4 mai 2015

Which design pattern to use to store streams of objects of different types to disk in go?

Consider a function that saves streams of objects of different types to disk:

func Save(fill func(c chan BaseType), file string) {}

This function is used in the following way throughout the code:

// Here we've got different data structures data1, data2, ...
// that must be stored to disk
Save(
  func(c chan BaseType) {
    // SaveChildren1 generates objects of type Child1 based
    // on the data1 data structure
    SaveChildren1(c, data1) 
  },
  filename1)

Save(
  func(c chan BaseType) {
    // SaveChildren2 generates objects of type Child2 based
    // on the data2 data structure
    SaveChildren2(c, data2)
  },
  filename2)

And so on. There are a lot of different child types and each requires it's own generator -- a function SaveChildren1, SaveChildren2 etc. These functions actually fill the channels with objects.

The question is how would you refactor these "binders" -- the functions that transform SaveChildren (a function of 2 args) into a function of 1 argument? Currently this code doesn't look like a well written go-style code.

Aucun commentaire:

Enregistrer un commentaire