Motivation
Sometimes it's important to have only one instance for a class. For instance, there should be only one window manager (or only a file system or only a print spooler) in a system. Usually singletons are used for centralized management of internal or external resources and they provide a global point of access to themselves.
The singleton patterns is one of the simplest desing patterns and involves only one class which is responsible to instantiate itself only one instance and in the same time to provide a global point of access to the instance. In that case the instance can be used from everywhere without calling the directly the constructor each time.
Intent
•Ensure that only one instance of a class is created.
•Provide a global point of access to the object.
Implementation
The implementation involves a static member in the "Singleton" class, a private constructor and a static public method that returns a reference to the static member.
Applicability & Examples
According to the definition the singleton pattern should be used when there must be exactly one instance of a class, and when it must be accessible to clients from a global access point.
Here are some real situations where the singleton is used:
Example 1 - Logger Classes
The Singleton pattern is used in the design of logger classes. This classes are ussualy implemented as a singletons, and provides a global logging access point in all the application components without being necessary to create an object each time a logging operations is performed.
Example 2 - Configuration Classes
The Singleton pattern is used to design the classes that provide the configuration settings for an application. By implementing configuration classes as Singleton not only that we provide a global access point, but we also keep the instance we use as a cache object. When the class is instantiated( or when a value is read ) the singleton will keep the values in its internal structure. If the values are read from the database or from files this avoid reloading the values each time the configuration parameters are used.
Example 3 - Accesing resources in shared mode
It can be used in the design of an application that needs to work with the serial port. Let's say that there are many classs in the application, working in an multithreading environment, that needs to operate actions on the serial port. In this case a singleton with synchronized methods has to be used to manage all the operations on the serial port.
Welcome to the world of e Gyan.This is an attempt to put together and share the vast and vivid knowledge.The focus will be on sharing of our knowledge on- Latest tools and techniques.etc.So, go ahead and share the Gyan.
Wednesday, March 23, 2011
Abstract v/s Interface
What is an Abstract Class?
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
What is an Interface?
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn�t support multiple inheritance, interfaces are used to implement multiple inheritance.
An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
What is an Interface?
An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn�t support multiple inheritance, interfaces are used to implement multiple inheritance.
Cursor and alteranative of Cursor
If possible, avoid using SQL Server cursors. They generally use a lot of SQL Server resources and reduce the performance and scalability of your applications. If you need to perform row-by-row operations, try to find another method to perform the task.
Here are some alternatives to using a cursor:
• Use WHILE LOOPS
• Use temp tables
• Use derived tables
• Use correlated sub-queries
• Use the CASE statement
• Perform multiple queries
If you do find you must use a cursor, try to reduce the number of records to process.
One way to do this is to move the records that need to be processed into a temp table first, and then create the cursor to use the records in the temp table, not from the original table. This of course assumes that the subsets of records to be inserted into the temp table are substantially less than those in the original table.
The lower the number of records to process, the faster the cursor will finish.
If you have no choice but to use a server-side cursor in your application, try to use a FORWARD-ONLY or FAST-FORWARD, READ-ONLY cursor. When working with unidirectional, read-only data, use the FAST_FORWARD option instead of the FORWARD_ONLY option, as it has some internal performance optimizations to speed performance. This type of cursor produces the least amount of overhead on SQL Server.
If you are unable to use a fast-forward cursor, then try the following cursors in this order, until you find one that meets your needs. They are listed in the order of their performance characteristics, from fastest to slowest: dynamic, static, and keyset.
Avoid using static/insensitive and keyset cursors, unless you have no other choice. This is because they cause a temporary table to be created in TEMPDB, which increases overhead and can cause resource contention issues.
If you have no choice but to use cursors in your application, try to locate the SQL Server tempdb database on its own physical device for best performance. This is because cursors use the tempdb for temporary storage of cursor data. The faster your disk array running tempdb, the faster your cursor will be.
Using cursors can reduce concurrency and lead to unnecessary locking and blocking. To help avoid this, use the READ_ONLY cursor option if applicable, or if you need to perform updates, try to use the OPTIMISTIC cursor option to reduce locking. Try to avoid the SCROLL_LOCKS cursor option, which reduces concurrency.
When you are done using a cursor, don't just CLOSE it, you must also DEALLOCATE it. Deallocation is required to free up the SQL Server resources used by the cursor. If you only CLOSE the cursor, locks are freed, but SQL Server resources are not. If you don't DEALLOCATE your cursors, the resources used by the cursor will stay allocated, degrading the performance of your server until they are released.
If it is appropriate for your application, try to load the cursor as soon as possible by moving to the last row of the result set. This releases the share locks created when the cursor was built, freeing up SQL Server resources.
If you have to use a cursor because your application needs to manually scroll through records and update them, try to avoid client-side cursors, unless the number of rows is small or the data is static. If the number of rows is large, or the data is not static, consider using a server-side keyset cursor instead of a client-side cursor. Performance is usually boosted because of a reduction in network traffic between the client and the server. For optimum performance, you may have to try both types of cursors under realistic loads to determine which is best for your particular environment.
When using a server-side cursor, always try to fetch as small a result set as possible. This includes fetching only those rows and columns the client needs immediately. The smaller the cursor, no matter what type of server-side cursor it is, the fewer resources it will use, and performance will benefit.
If you need to perform a JOIN as part of your cursor, keyset and static cursors are generally faster than dynamic cursors, and should be used when possible.
Here are some alternatives to using a cursor:
• Use WHILE LOOPS
• Use temp tables
• Use derived tables
• Use correlated sub-queries
• Use the CASE statement
• Perform multiple queries
If you do find you must use a cursor, try to reduce the number of records to process.
One way to do this is to move the records that need to be processed into a temp table first, and then create the cursor to use the records in the temp table, not from the original table. This of course assumes that the subsets of records to be inserted into the temp table are substantially less than those in the original table.
The lower the number of records to process, the faster the cursor will finish.
If you have no choice but to use a server-side cursor in your application, try to use a FORWARD-ONLY or FAST-FORWARD, READ-ONLY cursor. When working with unidirectional, read-only data, use the FAST_FORWARD option instead of the FORWARD_ONLY option, as it has some internal performance optimizations to speed performance. This type of cursor produces the least amount of overhead on SQL Server.
If you are unable to use a fast-forward cursor, then try the following cursors in this order, until you find one that meets your needs. They are listed in the order of their performance characteristics, from fastest to slowest: dynamic, static, and keyset.
Avoid using static/insensitive and keyset cursors, unless you have no other choice. This is because they cause a temporary table to be created in TEMPDB, which increases overhead and can cause resource contention issues.
If you have no choice but to use cursors in your application, try to locate the SQL Server tempdb database on its own physical device for best performance. This is because cursors use the tempdb for temporary storage of cursor data. The faster your disk array running tempdb, the faster your cursor will be.
Using cursors can reduce concurrency and lead to unnecessary locking and blocking. To help avoid this, use the READ_ONLY cursor option if applicable, or if you need to perform updates, try to use the OPTIMISTIC cursor option to reduce locking. Try to avoid the SCROLL_LOCKS cursor option, which reduces concurrency.
When you are done using a cursor, don't just CLOSE it, you must also DEALLOCATE it. Deallocation is required to free up the SQL Server resources used by the cursor. If you only CLOSE the cursor, locks are freed, but SQL Server resources are not. If you don't DEALLOCATE your cursors, the resources used by the cursor will stay allocated, degrading the performance of your server until they are released.
If it is appropriate for your application, try to load the cursor as soon as possible by moving to the last row of the result set. This releases the share locks created when the cursor was built, freeing up SQL Server resources.
If you have to use a cursor because your application needs to manually scroll through records and update them, try to avoid client-side cursors, unless the number of rows is small or the data is static. If the number of rows is large, or the data is not static, consider using a server-side keyset cursor instead of a client-side cursor. Performance is usually boosted because of a reduction in network traffic between the client and the server. For optimum performance, you may have to try both types of cursors under realistic loads to determine which is best for your particular environment.
When using a server-side cursor, always try to fetch as small a result set as possible. This includes fetching only those rows and columns the client needs immediately. The smaller the cursor, no matter what type of server-side cursor it is, the fewer resources it will use, and performance will benefit.
If you need to perform a JOIN as part of your cursor, keyset and static cursors are generally faster than dynamic cursors, and should be used when possible.
STL containers
What are the types of STL containers?
• deque
• hash_map
• hash_multimap
• hash_multiset
• hash_set
• list
• map
• multimap
• multiset
• set
• vector
Currently there are two major types of containers defined in the C++ library: sequence containers and associative containers.
Sequence containers are: vector deque list
Associative containers are: set multiset map and multimap.
There are 3 containers adapters: stack queue and priority_queue.
The STL has two more data structures that some people argue they might be containers as well: bitset and valarray. There is also the specialization of the vector container for booleans which might not be a container.
No hash containers are defined in the current C++ STL standard. However other STL implementation might have some hash based containers (ie STL SGI implementation).
The upcoming C++ standard defines among others in TR1 (Technical Report 1) new containers : array unorder_set unordered_multiset unorder_map and unordered_multimap. Most of the TR1 is implemented in the gcc compiler. Dinkumware has a full TR1 specification implementation.
• deque
• hash_map
• hash_multimap
• hash_multiset
• hash_set
• list
• map
• multimap
• multiset
• set
• vector
Currently there are two major types of containers defined in the C++ library: sequence containers and associative containers.
Sequence containers are: vector deque list
Associative containers are: set multiset map and multimap.
There are 3 containers adapters: stack queue and priority_queue.
The STL has two more data structures that some people argue they might be containers as well: bitset and valarray. There is also the specialization of the vector container for booleans which might not be a container.
No hash containers are defined in the current C++ STL standard. However other STL implementation might have some hash based containers (ie STL SGI implementation).
The upcoming C++ standard defines among others in TR1 (Technical Report 1) new containers : array unorder_set unordered_multiset unorder_map and unordered_multimap. Most of the TR1 is implemented in the gcc compiler. Dinkumware has a full TR1 specification implementation.
Wednesday, March 16, 2011
Data Type
"P" means "pointer"
"W" means "wide"
"STR" means "string"
"C" means "const"
PWSTR means "pointer of wide string", that is, unsigned short* (or wchar_t*)
LPSTR means "long pointer of string", that is, char*
WCHAR means "wide char", that is, unsigned short (or wchar_t)
LPCSTR means "const pointer of string", that is, const char*
PWSTR is a pointer to a UNICODE string.
[ typedef WCHAR* PWSTR; typedef wchar_t WCHAR; ]
LPSTR is a pointer to a 8 bit ANSI string
[typedef CHAR* LPSTR; typedef char CHAR; ]
WCHAR is a wchar_t as mentioned above and LPCSTR is an 8 bit ANSI string
[ typedef __nullterminated CONST CHAR* LPCSTR; ]
These typedefs can be found in file WinNT.h
Char/wchar/TCHAR : The C strings for ANSI and Unicode
CString : The C++/MFC class wrapper for C strings
BSTR : The Visual Basic string type
_bstr_t : A C++ class wrapper for the Visual Basic string type
CComBSTR : Yet another C++ class wrapper for the Visual Basic
string type used predominately in ATL code
Subscribe to:
Posts (Atom)