Principles of Package Design is a book written by Matthias Noback
The book code examples are written in PHP and the tools that appears in it are from the PHP community.
I would like that people that works using Node.js could enjoy of the knowledge that this book offers to its readers. Therefore I going to use Node.js to write the code examples and I going to use tools typically related with it. 😍
The style of the code and the chosen tools are 100% my decision. (The good and the bad parts! 😚)
-
For every chapter of the book has I created a directory where appears the final version of the code in the mentioned chapter.
-
Every commit has a reference to the chapter related. Any change I needed to do has a commit, with the objective of follow the flow of the book.
Note: If you want to open the links in another tab, just do a CTRL+click on the link.
-
The Single Responsibility Principle
git commits done during the chapter
- Initial commit
- update README
- In order to commit formatted code I installed prettier, pretty-quick and husky
- adding gitignore file
- adding prettier config file
- creating confirmationMailMailer using a factory function with too many responsibilities
- extracting the creation of confirmation mail responsibility into his own place
- refactoring confirmationMailMailer to reduce its responsibilities and use instead confirmationMailFactory
- renaming confirmationMailMailer file -
The Open/Closed principle
git commits done during the chapter
- create a genericEncoder that violates the open/closed principle
- worsen the genericEncoder adding a case for yaml format
- worsen the genericEncoder adding the method prepareData that also violates the open/closed principle
- creating 3 different encoders that implements an interface, in this case I'm trusting in duck typing due the lack of interfaces in javascript language unless you use flowjs or an superset like typescript
- creating a encodeFactory whose only responsibility is to create encoders based in the given format
- refactoring genericEncoder to use the encoder factory
- making encoderFactory open for extension injecting factories
- creating another mandatory method for the encoders: prepareData
- using prepareData of the specific encoder, however we just introduced temporal coupling...
- removing the temporal cooupling :), who knows, maybe even it is not longer necesary the existence of genericEnconder, instead we could use the encodeFactory whenever we need it :)
-
The Liskov substitution principle
git commits done during the chapter
- adding dropbox specific implementation of fileinterface where change Owner has not implementation, this is not be a good example of a good substitute, calling that method will crash the application
- instead of throwing exception, don't do nothing... silent behavior... we are lying to the user of our class...
- splitting interfaces in order to avoid to need to throw an error or not make something if some user uses changeOwner in a class that not have implementation
-
The Interface Segretation Principle
git commits done during the chapter
- example of a serviceContainer that implements a interface that violates the interface segregation principle
- separating the virtual interfaces I created, that allows one client use only what need, set, get or both :)
- kernel code expect a service container that only need the method set
- a example of controller that expect a service container that only need the method get
- userRepository it is depending of a concrete class instead of a interface
- making getUnitOfWork public to use in the new file query.js, automatically all the clients of entityManager depend of this method too, regardless if they use it, can cause backward compatibility problems...
- adding role and header interfaces to use in entityManager
-
The Dependency Inversion Principle
git commits done during the chapter
- creating fizzbuzz function
- making fizzBuzz open for extension, step 1
- making fizzBuzz open for extension finished, and right now with the rules is depending on abstractions, an example of the inversion principle
- adding authentication function that violates SRP, open close andependency inversion among others
- making authentication depending of a concrete class of userProvider, dependency inversion still is been violated
- authentication is not depending in connection anymore, now depends in an abstraction
-
The Release/Reuse Rquivalence Principle
git commits done during the chapter
- No commits here, a chapter with good tips :)
-
The Common Reuse Principle
git commits done during the chapter
- No commits here, a chapter with good tips :)
-
The Common Closure Principle
git commits done during the chapter
- No commits here, a chapter with good tips :)
-
The Acyclic Dependencies Principle
git commits done during the chapter
- No commits here, a chapter with good tips :)
-
The Stable Dependencies Principle
git commits done during the chapter
- No commits here, a chapter with good tips :)
-
The Stable Abstraction Principle
git commits done during the chapter
- No commits here, a chapter with good tips :)
-
Conclusions
git commits done during the chapter
- No commits here, a chapter with good tips :)
I hope you enjoy the repository as much I while I was writing it 😃
I strongly encourage that you should buy the book, it is really fantastic.