Advantages of Singleton
-You can be sure that a class has only a single instance.
-You gain a global access point to that instance
-The singleton object is initialized only when it’s requested for the first time
-You can change any class into a Singleton simply by making its constructors private and adding the appropriate static functions and variable.
-It is possible to extend the Singleton class through sub-classes.
Creational design pattern
They are about the creation (instantiation) of objects in such a way that the system remains flexible and afford code reuse.
Singleton
It assures that there is one and only one instance of a class and provides a
global point of access to it.
Creational design patterns
The lock statement
prevents another thread to access the code inside the lock.
Another thread will stop execution when it encounters the lock and only continue when the lock is released by the first thread.
We say that the second thread is blocked
Disadvantages of singleton
The Singleton makes an instance global since the class is internal or public. Global is
bad.
The Singleton pattern can mask bad design, for instance, when the components of the
program know too much about each other.
The pattern requires special treatment in a multithreaded environment so that multiple
threads won’t create a singleton object several times.
There is no good way to destroy or decommission a Singleton.
Not inherited. A class derived from a Singleton is not a Singleton. If it needs to be a
Singleton, the static function and variable need to be added to it.
Bad efficiency: Each call to GetInstance invokes the if statement. For most of those calls, the if statement is useless.
Non-transparent: Users of a Singleton know that they are using it, because they must invoke the GetInstance method
Why we dont use a static class to ensure a single global instance of a variable
When to use the singleton pattern
when a class in your program should have just a single instance available across the scope as allowed by the class
The Prototype pattern
-It is used to create a duplicate object or clone of the current object.
It provides an interface for creating parts of a product.
This pattern is used when the creation of an object is costly or complex.
Value types
When we copy a value from one object to another, a different memory cell is created
Reference types
-An assignment of one object to another creates another pointer to the same memory cell.
So, when the content of the first object changes, the content of the second one changes as well.
The MemberwiseClone() method
-That means that value types
are copied to the new object, but for reference types, the variable in the new object points to the original value.
When to use the prototype method
-when the creation of an object is costly or complex.
-when the creation of an object is costly or complex.
Advantages of The prototype pattern
Disadvantages of the Prototype pattern
Cloning complex objects that have circular references might be very tricky.
Factory Method Pattern
-It builds on the concept of a factory class by defining an interface or abstract class for the factory class and allow subclasses thereof (concrete creators) to decide which product class to instantiate.
-Different concrete creators create different product types.
Advantages of the Factory Pattern Method
Disadvantages of the Factory Method Pattern
When to use the factory Method Pattern
-when you don’t know beforehand the exact types and
dependencies of the objects your code should work with
-when you want to provide users of your library or framework with a way to extend its internal components
Factory Pattern vs Builder Pattern ( Factory)
Every factory class creates an objects of a specific class.
Factory Pattern vs Builder Pattern ( Builder)
The builder classes create objects of the same class but with different values for the
properties.