This repo contains the implementation of various design patterns implementation using C#. It also demonstrates posibilities to implement same pattern in different ways
Broadly there are 3 types of design patterns prevelant in use. these are based on famous GOF
These patterns deal with creation of object instances. These are of different types depending on what you intend to do with them.
This pattern ensure that only one instance of a class is created at any point of time. The sample code demonstrate different way of implementing singleton pattern. Refer the entire implementation here.
- Basic This class implements a very basic version of singleton using private constructor and factory method to instanciate the instance.
- Eager This class implements the eager initialization pattern fro creating singleton objects.
- Lazy This class shows how to utilize the Lazy feature of C# to implement singleton pattern.
- Thread Safe This class implements a thread safe version of singleton using the static constructor method.
- Thread Lock This class shows how to create a singleton class by using a lock to synchronize the creation of instance between multiple threads.
This pattern is used to create objects without exposing the instantiation logic to the client. The sample code demonstrate different way of implementing factory pattern. Refer the entire implementation here.
This demonstrates the class AccountFactory which creates instance of SavingAccount or CurrentAccount and returns a IAccount object. This uses the classic new() operator to create instances. The caller can work with both the instances in exactly same way, abstracting thier internal details and implementation details.
The class AccountFactoryServiceProvider demonstrates how to use IServiceCollection to create instances of SavingAccount or CurrentAccount and return IAccount object. This uses the IServiceCollection to create instances. The caller can work with both the instances in exactly same way, abstracting thier internal details and implementation details.
The class AccountFactoryReflection demonstrates how to use Activator to create instances of SavingAccount or CurrentAccount and return IAccount object. The caller can work with both the instances in exactly same way, abstracting thier internal details and implementation details.
It also shows how to leverage a base class AccountBase to provide common fields and functionality across all derived class. This enables us to create a common abstraction among all derived classes and still make use of Factory to create instance thereby ensuring common pattern id followed by each client.
These patterns deals with the structure of code and classes. It highlights how different classes interact to form a larger system of classes.
These patterns deals with the interaction and behavioral responsibilities of classes involved in the overall design.
You can fork the repository at your local machine and run the code using Visual Studio or any other IDE of your choice.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.