Factory method is a creational design pattern which solves the problem of creating product objects without specifying their concrete classes.
We will create a computer factory. Our factory will create 3 types of computers:
laptop, personal computer and server.
-
Create an interface of computer.
// computer/computer.go type Computer interface { GetType() string GetCPU() int GetOS() string }
-
Create types of our computers.
// computer/computer.go type Type string const ( LaptopType Type = "laptop" PersonalComputerType Type = "personal computer" ServerType Type = "server" )
-
Implement struct for
laptop. Other structures will be the same.// computer/laptop.go type Laptop struct { Type Type OS string CPU int } func NewLaptop() Computer { return &Laptop{ Type: LaptopType, OS: "MacOS", CPU: 8, } } func (l *Laptop) GetType() string { return string(l.Type) } func (l *Laptop) GetCPU() int { return l.CPU } func (l *Laptop) GetOS() string { return l.OS }
-
Implement function to get computer.
// factorymethod.go var ErrComputerNotFound = errors.New("computer not found") func Get(compType Type) (Computer, error) { switch compType { case LaptopType: return NewLaptop(), nil case PersonalComputerType: return NewPersonalComputer(), nil case ServerType: return NewServer(), nil default: return nil, ErrComputerNotFound } }