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.
For this project:
- A singleton dessign pattern was used, so any users using it can access the same instance of the DMC.
- 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.
Just import this project to Visual studio.
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.
We implemented a dictionary that looks for the type of objects the DMC gets, and:
- 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.
- If we do not find the object, we insert it.
- If we find the same object, we update.
- 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.
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).
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).