Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Factory Method

Factory method is a creational design pattern which solves the problem of creating product objects without specifying their concrete classes.

Example

We will create a computer factory. Our factory will create 3 types of computers: laptop, personal computer and server.

  1. Create an interface of computer.

    // computer/computer.go
    
    type Computer interface {
      GetType() string
      GetCPU() int
      GetOS() string
    }
  2. Create types of our computers.

    // computer/computer.go
    
    type Type string
    
    const (
      LaptopType           Type = "laptop"
      PersonalComputerType Type = "personal computer"
      ServerType           Type = "server"
    )
  3. 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
    }
  4. 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
      }
    }