Skip to content

This is an example of a DMC implementation. In this project we use the singleton design pattern and use a basic thread-locking protection (double checked locking).

License

Notifications You must be signed in to change notification settings

pontazaricardo/Singleton-Demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Singleton-Demo

This is an example of a DMC that allows multiple users to access a centralized data management entity in order to insert new objects or update the existing ones.

demo

Design patterns

For this project:

  1. A singleton dessign pattern was used, so any users using it can access the same instance of the DMC.
  2. A double-checked locking dessign pattern was implemented in the data structure holding the data in order to make the DMC multithread and thread-safe.

Usage

Just import this project to Visual studio.

Interfaces

Because the idea of the DMC is to manage objects that are somewhat unknown for it, we use the IComparable interface. From this, the clients implement their own CompareTo function, allowing us not only to compare but to sort objects of the same type.

Data structure

We implemented a dictionary that looks for the type of objects the DMC gets, and:

  1. If the type is already recorded in the dictionary, we use the CompareTo function to scan a list of objects of that type in the dictionary.
    1. If we do not find the object, we insert it.
    2. If we find the same object, we update.
  2. If the type is a new type, we create the entry in the dictionary and create a list associated with it with the given object.

Searches

Because per type we use a List structure. We can either scan the entire list or perform a smart search. Scanning the list will require O(n) time, so in order to speed the search, when inserting, we insert the object in a sorted way, so we can perform a binary search when looking for the object. This way allows us to reduce the search time to O(ln n).

demo2

Results during execution time

Inside the Main file we have the following code:

Parallel.Invoke( // We will create two threads that will access the same instance of the DMC (where each thread creates their own instantiation of the object).
	() => FirstMethod(),
	() => SecondMethod()
);

This code will create two threads (one per method). Because the access of the threads to the methods will be done in parallel, the output might change every time you run it, but it will reach the same final solution at the end. (It is just a parallel multitask arrangement).

About

This is an example of a DMC implementation. In this project we use the singleton design pattern and use a basic thread-locking protection (double checked locking).

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages