ishan16696 / design-patterns-Golang

Design patterns in Golang with implementation.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

design-patterns-Golang

  1. FactoryMethod

    • Define an interface for creating an object, but let subclasses/Methods decide which class to instantiate. Defer the instantiation to subclasses/Methods.

      • Interface defination:
            type Factory interface {
                NewPerson() *person
                NewAgent() *secretAgent
            }
      • Defer the instantiation to subclasses/Methods:
            fact := factory.NewFactory(config)
        
            // with factory method, we can construct the instances of both person/agent.
            person := fact.NewPerson()
            agent := fact.NewAgent()
  2. Builder

    • Separate the construction of a complex object or Segregating the builder into multiple builders. It is used to construct a complex object step by step and the final step will return the object.
      •  emp1 := fact.SetName("Ishan").SetTechStack([]string{"C++", "Docker", "Go"}).BuildDev()
  3. Usage of interface

    • Define an interface
      • // Action is a context-aware action.
        type Action interface {
            // Do performs an action.
            Do(ctx context.Context)
        }
    • Implements that interface
      •  // ActionFunc is a function that implements Action.
         type ActionFunc func(ctx context.Context)
        
         // Do performs an action.
         func (f ActionFunc) Do(ctx context.Context) {
             f(ctx)
         }

About

Design patterns in Golang with implementation.

License:MIT License


Languages

Language:Go 100.0%