Sunday, 20 December 2015

Factory in AngularJS



“Factory” in real world means a premise where products are manufactured. Let’s take an example of a computer manufacturing firm. Now the company produces different kinds and sizes of computers like laptops, desktops, tablets etc.
Now the process of manufacturing the computer products are same with slight variation. To manufacture any computer we need processor, RAM and hard disk. But depending on what kind of final case packing is the final product shapes.

That’s what the use of Factory in Angular.
For example see the below code we have a “Customer”, “Phone” and “Address” class.
function Customer()
{
        this.CustomerCode = "1001";
        this.CustomerName = "Shiv";
}

function Phone()
{
        this.PhoneNumber = "";
}

function Address()
{
        this.Address1 = "";
        this.Address2 = "";
}
So now we would create different types of “Customer” object types using the combination of “Address” and “Phones” object.
1)    We would like to combine “Customer” with “Address” and create a “Customer” object which has “Address” collection inside it.
2)    Or must be we would like to create “Customer” object with “Phone” objects inside it.
3)    Or must be “Customer” object with both “Phone” and “Address” objects.
In other words we would like to have different permutation and combination to create different types of “Customer” objects.
So let’s start from bottom. Let’s create two factory function’s one which creates “Address” object and the other which creates “Phone” objects.
Function CreateAddress()
{
var add = new Address();
return add;
}

Function CreatePhone()
{
var phone =  new Phone();
return phone;
}
Now let’s create a main factory function which uses the above two small factory functions and gives us all the necessary permutation and combination.
In the below factory you can see we have three functions:
1)    “CreateWithAddress” which creates “Customer” with “Address” objects inside it.
2)    “CreateWithPhone” which creates “Customer” object with “Phone” objects inside it.
3)    “CreateWithPhoneAddress” which creates “Customer” object with aggregated “Phone” and “Address” objects.
function CreateCustomer() {

return {
CreateWithAddress: function () {
var cust = new Customer();
cust.Address = CreateAddress();
return cust;
            },

CreateWithPhone: function () {
var cust = new Customer();
cust.Phone = {};
cust.Phone = CreatePhone();
return cust;
            },

CreateWithPhoneAddress: function () {
var cust = new Customer();
cust.Phone = CreatePhone();
cust.Address = CreateAddress();
return cust;
            }
        }
    }
Below is a simple “CustomerController” which takes “CustomerFactory” as the input. Depending on “TypeOfCustomer” it creates with “Address”, “Phones” or both of them.
Function CustomerController($scope, Customerfactory)
    {
        $scope.Customer = {};
        $scope.Init = function(TypeofCustomer)
        {
if (TypeofCustomer == "1")
            {
                $scope.Customer = Customerfactory.CreateWithAddress();
            }
if (TypeofCustomer ==  "2")
            {
                $scope.Customer = Customerfactory.CreateWithPhone();
            }
if (TypeofCustomer == "3") {
                $scope.Customer = Customerfactory.CreateWithPhoneAddress();
            }
        }
    }
You also need to tell Angular that the “CreateCustomer” method needs to be passed in the input. For that we need to call the “Factory” method and map the “CreateCustomer” method with the input parameter “CustomerFactory” for dependency injection.
var app = angular.module("myApp", []); // creating a APP
app.controller("CustomerController", CustomerController); // Register the VM
app.factory("Customerfactory", CreateCustomer);
So if we consume the “CustomerController” in UI, depending on situation it creates different flavors of “Customer” object. You can in the below code we have three different “DIV” tags and depending on the “TypeofCustomer” we are displaying data.



 


No comments:

Post a Comment