Skip to content

This repo contains the implementation of various design patterns implementation using C#.

License

Notifications You must be signed in to change notification settings

pravinchandankhede/designpatterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unit Tests

Design Patterns

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

Creational Patterns

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.

  1. Basic This class implements a very basic version of singleton using private constructor and factory method to instanciate the instance.
  2. Eager This class implements the eager initialization pattern fro creating singleton objects.
  3. Lazy This class shows how to utilize the Lazy feature of C# to implement singleton pattern.
  4. Thread Safe This class implements a thread safe version of singleton using the static constructor method.
  5. 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.

Traditonal Factory

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.

Factory using Dependency Injection

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.

Factory using Reflection

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.

Structural Patterns

These patterns deals with the structure of code and classes. It highlights how different classes interact to form a larger system of classes.

Behavioral Patterns

These patterns deals with the interaction and behavioral responsibilities of classes involved in the overall design.

Installation

You can fork the repository at your local machine and run the code using Visual Studio or any other IDE of your choice.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

About

This repo contains the implementation of various design patterns implementation using C#.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages