An example implementation of the GoF Builder pattern using Funcional Programming. Immutability achieved by sequential evolving of frozen objects.
- Node.js
- NPM
- Basic knowledge of Functional Programming and Ramda library
$ git clone https://github.com/dibikhin/fp-builder-js.git
$ cd fp-builder-js
$ npm install
$ npm test
Build a complex message
object by filling fields one by one in independent steps - withTitle()
, withBody()
, etc. Can call the building steps in any order.
pipe()
simplifies passing parameters without temporary variables. build()
in the end is for consistency and helps with identifying the Builder pattern.
See the full example in ./builder.spec.js
.
const message = pipe(
withTitle({ title: 'test title' }),
withBody({ body: 'test body' }),
withPriority({ priority: 'high' }),
build,
)(emptyMessage)
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
- Roman Dibikhin
Thanks to:
- The "Gang of Four" for the "Design Patterns" book
- Peter Norvig and Scott Wlaschin for inspiration
- The Builder Pattern in JavaScript - An article about someone's OOP version
- Ramda - A practical functional library for JavaScript programmers
- Mocha - A feature-rich JavaScript test framework
- Chai - A BDD / TDD assertion library
- Source Making - A free catalog of Design Patterns, AntiPatterns, and Refactorings
- F# for fun and profit - An inspiring and addictive blog about Functional Programming