Logo

Programming-Idioms

History of Idiom 73 > diff from v6 to v7

Edit summary for version 7 by :

Version 6

2015-08-21, 17:04:04

Version 7

2015-08-21, 17:12:34

Idiom #73 Create a factory

Create a factory named pFact for any sub class of Parent and taking exactly one string str as constructor parameter.

Idiom #73 Create a factory

Create a factory named pFact for any sub class of Parent and taking exactly one string str as constructor parameter.

Code
type ParentFactory func(string) Parent

var pFact ParentFactory = func(str string) Parent {
	return Parent{
		name: str,
	}
}
Comments bubble
A Factory is a function which returns an object.

Go doesn't have subtyping, but Parent could be any type: struct, interface, etc.
Demo URL
http://play.golang.org/p/6pE8ijPdWu