-
-
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()
- Interface defination:
-
-
- 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()
-
- 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.
-
- 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) }
-
- Define an interface
ishan16696 / design-patterns-golang Goto Github PK
View Code? Open in Web Editor NEWDesign patterns in Golang with implementation.
License: MIT License