Programming Patterns
⏱️ ETC: 4 hours
Software design patterns are reusable solutions to common problems that arise during the design of software applications. The knowledge of design patterns is also what distinguishes a senior software engineer from a junior coder.
That being said, in distributed systems like blockchains, where nodes need to handle complex peer-to-peer communication, state synchronization, and consensus mechanisms, design patterns provide battle-tested solutions for critical challenges. You'll need patterns like Observer for handling block and transaction propagation and Factory patterns for node instantiation across different network configurations. Without solid design patterns, a blockchain node can quickly become an unmaintainable mess of spaghetti code that's prone to bugs and difficult to upgrade.
The foundation of design patterns was laid by the "Gang of Four" in their seminal 1994 book "Design Patterns: Elements of Reusable Object-Oriented Software." Their collection of 23 patterns remains fundamental to software architecture and is organized into three categories:
- Creational: The design patterns that deal with the creation of an object.
- Structural: The design patterns in this category deals with the class structure such as Inheritance and Composition.
- Behavioral: This type of design patterns provide solution for the better interaction between objects, how to provide loose coupling, and flexibility to extend easily in future.
Our favorite resource for mastering design patterns using the GoF separation is the Refactoring Guru. Even though we would suggest that you read the whole Guru website the essential patterns are :
- Creational: Singleton, Builder, Factory
- Structural: Proxy, Bridge, Flyweight, Facade
- Behavioral: Observer, Mediator, Visitor
Once again, a good exercise would be to try implementing the pseudocode provided on the site using a language from the Proficiency in 2 Languages chapter. You can also refer to the Code Examples which do exactly that.
💡 Tasks
- Read and understand all of the patterns mentioned above
Optional
For those who want to dive deeper into design patterns, we highly recommend reading the complete Design Patterns: Elements of Reusable Object-Oriented Software by the Gang of Four.
To complement the theoretical knowledge from the book with practical implementations, we've found this repo to be particularly valuable, as it provides simple and modern code examples of each pattern.