Design Patterns with CSharp
Design patterns are mostly used and important in our daily lives, for interviews as well as day-to-day work.
One of the most popular books for learning design patterns is “Design Patterns: Elements of Reusable Object-Oriented Software,” which was authored in 1994 by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Due to the four authors, it has become known as the Gang of Four (GoF) design patterns book and is often abbreviated as “GoF Design Patterns.”
Creational Design Patterns
- Deals with the process of object creation in such a way that they can be decoupled from their implementing system.
- Provides more flexibility in deciding which objects need to be created for a given use case or scenario.
- There are five design patterns in this category, which are:
- Singleton
- Prototype
- Factory Method
- Abstract Factory
- Builder
Singleton Pattern
This pattern ensures that a class has only one instance and provides a global point of access to it.
• The singleton class includes:
• static private instance
• Private Constructor
• Public Instance Property or GetInstance() method
Singleton Pattern Examples
- You need a single instance of an object throughout the application.
- Exception logging
- Database Manager
- Business Layer Manager
Prototype Pattern
• Used to create a duplicate object or clone of the current object to enhance performance.
• Used when the creation of an object is costly or complex.
Prototype Pattern Examples
- App needs copies of objects, where objects
- Are not simple value types.
- Have other objects as properties like Customer has Address object property
Shallow:
Breadth vs Depth; think in terms of a tree of references with your object as the root node.
Shallow:
The variables A and B refer to different areas of memory, when B is assigned to A the two variables refer to the same area of memory. Later modifications to the contents of either are instantly reflected in the contents of other, as they share contents.
Deep:
The variables A and B refer to different areas of memory, when B is assigned to A the values in the memory area which A points to are copied into the memory area to which B points. Later modifications to the contents of either remain unique to A or B; the contents are not shared.