Monday, 4 November 2019

Simple Factory Vs Factory Method Vs Abstract Factory

After searching a numerous articles I found the following is an excellent and simple article .


https://vivekcek.wordpress.com/2013/03/17/simple-factory-vs-factory-method-vs-abstract-factory-by-example/



Simple Factory Vs Factory Method Vs Abstract Factory by Example

Posted by vivekcek on March 17, 2013

I came across lots of situations in which, I have to explain the difference between a Simple Factory, Factory Method and Abstract Factory patterns.

The main objective of this post is how simply you can explain the difference among these patterns using some example. I hope the readers are aware of these patterns, because I am not going to explain each pattern in depth.

Simple Factory Pattern

Definition:
Creates objects without exposing the instantiation logic to the client.
Refers to the newly created object through a common interface

Diagram:

SimpleFactory

Explanation:

The heart of above Simple Factory pattern is the ‘MobileFactory’ class. From the Client when we create an instance of this ‘MobileFactory’, this class will load and cache all the classes that implement the ‘IMobile’ interface by using reflection or some other logic. After that we can call the ‘GetMobile(string Name)’ method in the ‘MobileFactory’ class which will return the specified object through the parameter.

The Client will expect an object that implements the ‘IMobile’ interface.

Factory Method

Definition:

Defines an interface for creating objects, but let subclasses to decide which class to instantiate
Refers the newly created object through a common interface.

Diagram:

FMethod

Explanation:

In Factory Method pattern we will introduce a new interface called ‘IMobileFactory’ and two concrete implementation’s ‘NokiaFactory’ and ‘IphoneFactory’. These concrete classes control the object creation.

In my example the client want a Nokia object. So the steps are given below.

1.The client will load a reference to ‘NokiaFactory’. But Client won’t refer the ‘NokiaFactory’ class directly like the Simple Factory pattern. The client refers the concrete implementation through an interface ‘IMobileFactory’.

2.Then the Client call the ‘CreateMobile()’ method that will return an object of type ‘IMobile’.

3.Here we have to inform the client the concrete implementation to be used through some configuration or parameters.

Abstract Factory

Definition:

Abstract Factory offers the interface for creating a family of related objects, without explicitly specifying their classes

Diagram:

abstractfactorypg

Explanation:

The main point regarding Abstract Factory pattern is that, this pattern creates a family of related objects that have different parent class or interface.

In Abstract Factory pattern the object creation happens in the same way as Factory Method. The only difference is the creation of related objects.

I now updated our old ‘IMobileFactory’ interface with two new methods.
CreateNokiaMobile() – Returns a Nokia objects that implements an ‘INokia’ interface.
CreateAppleMobile()- Returns Iphone objects that implements ‘IApple’ interface.

There are two concrete implementation of ‘IMobileFactory’ named 3GMobileFactory and 4GMobileFactory.

3GMobileFactory – Can return 3G supported mobiles of Nokia and Apple. So as per definition confirms, which returns a family of related objects that have different parents (INokia, IApple).

4GMobileFactory – Can return 4G supported mobiles of Nokia and Apple


Friday, 10 May 2019

Random interview questions

What is Lazy keyword?
What is Covariance and Contravariance?
What is the difference between FirstOrDefatul,First,SingleOrDefault and SingleOrDefault?
What are Generic delegates?
Why Deletegates? Where did you used delegates in your project?
What is the difference between IQuarrable and IEnumarable and which one considered to be best for Performance point of view?
What are the Generations of Garbage Collector?
What is the difference between Var and Dynamic keywords?
Write down the query for inner join using LINQ?
What is the difference between Event and Deletegates?
Write down the code for Singleton patter for thread safty?
What are Extension methods?
Why to use Static Constructor and When it will called?
What is lazyloading and Eagerlyloading?
What is DBContext?
What is IDisposable?