Category: Design Principles

Implement important design patterns in a single solution


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.

  1. Dependency Inversion principle is implemented with necessary interfaces.
  2. Factory pattern
  3. Service Locator pattern is implemented using inbuilt .NET core dependency injection framework.
  4. Iterator pattern is implemented using IEnumerable and yield.

Basic Idea

  1. Cube is implemented with both singly linked list and doubly linked list.
  2. 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)
  3. The type of list to be used to build the cube can be selected by the user at run-time.
  4. The selection is passed as a parameter to the Factory class. (dependency injection)
  5. The Factory will generate the required node objects (service locator pattern)
  6. 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.