Skip to content

mrbrunelli/terere-builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Builder, Factory and Repository pattern with Javascript

Util content: dofacory.com/javascript/design-patterns

Builder pattern

  • Useful for creating custom behaviors, using methods with descriptive names.
  • It's a lot like a production line, where each employee performs a single function.
Example
myBuilderFunction
  .stepOne()
  .stepTwo()
  .stepThree()
  .stepFour()
  .finish()

Factory pattern

  • Useful for concentrating instance creation in one place.
  • Gives the possibility to crate some logic when creating an instance.
Example
function createOjbInstance(optionalParam) {
  const param = optionalParam || "I am a default param"
  return new MyClass(param)
}

const objOne = createObjInstance()
const objTwo = createObjInstance()
const objThree = createOjbInstance("I am an optional parameter")
const objFour = createOjbInstance()

Repository pattern

  • Useful for abstracting data persistence.
  • Methods have descriptive names (find, findById, findByName, create, save, delete, remove etc).
Example
const myRepository = new MyRepositoryClass()

myRepository.save({ id: "123abc", fieldOne: "one", fieldTwo: "two" })
myRepository.save({ id: "456dfg", fieldOne: "three", fieldTwo: "four" })

const allRepositoryData = myRepository.find()
// [{ id: "123abc", fieldOne: "one", fieldTwo: "two" }, { id: "456dfg", fieldOne: "three", fieldTwo: "four" }]

What the hell is Tereré?

Practically an iced tea with water or juice

About

My studies of Builder Pattern, Factory Pattern and Repository Pattern with Javascript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors