Recently I had been provided with the interesting problem “Cube list to be implemented with the double linked list”.
I began with solving the problem with the straightforward algorithm. But started refactoring the same with OOP principles and design patterns.
The solution is implemented with C# Core 2.0. You can find the source code at Google Drive.
Phase 1: Use most important design patterns
The most important design principles and patterns, I applied in the first phase of refactoring are as below.
- Dependency Inversion principle is implemented with necessary interfaces.
- Factory pattern
- Service Locator pattern is implemented using inbuilt .NET core dependency injection framework.
- Iterator pattern is implemented using IEnumerable and yield.
- Cube is implemented with both singly linked list and doubly linked list.
- Both the list classes implement common interface ICubeNode. The Cube list class is not aware of (doesn’t care) what type of list is used. (Dependency Inversion principle)
- The type of list to be used to build the cube can be selected by the user at run-time.
- The selection is passed as a parameter to the Factory class. (dependency injection)
- The Factory will generate the required node objects (service locator pattern)
- Each and every element of the cube can be extracted using Iterator pattern.
Note: Thanks to Armen Shimoon for sharing the idea to invoke the factory pattern within the .NET Core dependency injection container.