Q11. What is difference between static class and Singleton class?
Q12. What is eager loading in entity framework. How it is different from Lazy loading in EF?
Q13. What are the various types of Design pattern in C#?
Q14. Tell me one liner about all design patterns you know?
Q15. Tell something more about Factory method design pattern. How to implement it?
============================================================================
Q11. What is difference between static class and Singleton class?
Answer:
Static Class:-
You cannot create the instance of static class.
Loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
Static Class cannot have constructor.
We cannot pass the static class to method.
We cannot inherit Static class to another Static class in C#.
A class having all static methods.
Better performance (static methods are bonded on compile time)
Singleton:-
You can create one instance of the object and reuse it.
Singleton instance is created for the first time when the user requested.
Singleton class can have constructor.
You can create the object of singleton class and pass it to method.
Singleton class does not say any restriction of Inheritance.
We can dispose the objects of a singleton class but not of static class.
Methods can be overridden.
Can be lazy loaded when need (static classes are always loaded).
We can implement interface(static class can not implement interface).
One of the key advantage of singleton over static class is that it can implement interfaces and extend classes while the static class cannot
============================================================================
Q12. What is eager loading in entity framework. How it is different from Lazy loading in EF?
Answer:
Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query, so that we don't need to execute a separate query for related entities. Eager loading is achieved using the Include() method. eg: Here we have two tables users and userDetails and they have a common key that is userId.
User usr = dbContext.Users.Include(a => a.UserDetails).FirstOrDefault(a => a.UserId == userId);
Lazy Loading means the related entities are not loaded, until we iterate through them or bind them the data. By default, LINQ to SQL loads the related entities, using Lazy Loading.
============================================================================
Q13. What are the various types of Design pattern in C#?
Answer:
Design patterns make your solution reliable, expandable and testable.
There are 3 types of design pattern
1. Creational - If you have a huge project with a lot of classes, a lot of classes mean you are going to deal with a lot of objects. So you need to create different objects (like new Customer(), new product(), new invoice(), etc.). If these objects creations are scattered on the client code, then it leads to lots of complicated logic at the client code.
- Singleton
- Factory Method
- Abstract Factory
2. Structural - These patterns deal with the composition of objects structures. The concept of inheritance is used to compose interfaces and define various ways to compose objects for obtaining new functionalities.
- Adaptor
- Façade (Faas Saad)
- Proxy
3. Behavioral - This patterns deals with object behavior i.e how object is going to behave when communicating with other objects.
- Chain of responsibility
- Observer
- Iterator
In total there are 23 GOF (gang of four design pattern) which fall on above mentioned categories.
============================================================================
Q14. Tell me one liner about all design patterns you know?
Answer:
Singleton
- A class of which only a single instance can exist.
- Lets you ensure that a class has only one instance, while providing a global access point to this instance.
Factory Method
- Creates an instance of several derived classes.
- Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created
Abstract Factory
- Creates an instance of several families of classes
- Create instances of several classes belonging to different families
- Lets you produce families of related objects without specifying their concrete classes.
Adaptor
- Match interfaces of different classes
- Allows objects with incompatible interfaces to collaborate.
Façade (Faas Saad)
- A single class that represents an entire subsystem
- A single class that represents an entire complex system
- Provides a simplified interface to a library, a framework, or any other complex set of classes
Proxy
- An object representing another object.
- Provides a surrogate object, which references to other object.
- Lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.
Chain of responsibility
- A way of passing a request between a chain of objects
- Passes a request among a list or chain of objects.]
- Lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.
Observer
- A way of notifying change to a number of classes.
- Allows an object (subject) to publish changes to its state and other objects (observer) that depend upon that object are automatically notified of any changes to the subject's state
- Lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they're observing.
Iterator
- Sequentially access the elements of a collection
- Provides a way to access the elements of a collection object in sequential manner without knowing its underlying structure.
- Lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).
============================================================================
Q15. Tell something more about Factory method design pattern. How to implement it?
Answer:
Factory design pattern is Creational design pattern
Get good example on LearningTopics #Factory Pattern