14 July, 2016

Difference Between the Service, Factory, and Provider in Angular js


Services

Syntax: module.service( 'serviceName', function );
Result: When declaring serviceName as an injectable argument you will be provided with an instance of the function. In other words new FunctionYouPassedToService().

Factories

Syntax: module.factory( 'factoryName', function );
Result: When declaring factoryName as an injectable argument you will be provided with the value that is returned by invoking the function reference passed to module.factory.

Providers

Syntax: module.provider( 'providerName', function );
Result: When declaring providerName as an injectable argument you will be provided with (new ProviderFunction()).$get(). The constructor function is instantiated before the $get method is called - ProviderFunction is the function reference passed to module.provider.

Note :
Providers have the advantage that they can be configured during the module configuration phase.

Use this code to understand the difference between the Service, Factory, and Provider in Angular js.
var masterApp = angular.module("masterApp", []); 
var MyFunc = function() 
{
  this.name = "default name";

  this.$get = function() 
  {
   this.name = "new name"
   return "Hello from MyFunc.$get(). this.name = " + this.name;
  }; 

  return "Hello from MyFunc(). this.name = " + this.name;
};

// returns the actual function
masterApp.service( 'myService', MyFunc );

// returns the function's return value
masterApp.factory( 'myFactory', MyFunc );

// returns the output of the function's $get function
masterApp.provider( 'myProv', MyFunc );

masterApp.controller('sfpController', function($scope, myService, myFactory, myProv) 
 {
   $scope.serviceOutput = "myService = " + myService;
   $scope.factoryOutput = "myFactory = " + myFactory;
   $scope.providerOutput = "myProvider = " + myProv;
 });




and the HTML :

Service | Factory | Provider

{{serviceOutput}}
{{factoryOutput}}
{{providerOutput}}

16 May, 2016

Error: Could not find or load main class TestEnum | Java

   /**
* @author Shashank Shekhar
*  @version 1.0
* This program test enum under if condition
*/
package Chapter3;
public class TestEnum
{
 enum Season {spring, summer, fall, winter};
 
 public static void main(String[] args)
 {
  Season W1 = Season.summer;
  if(W1.equals(Season.winter))
  {
   System.out.println("Well....Weather is Winter...");
  }
  else
  {
   System.out.println("Well....Weather is Not Winter...");   
  }
 }
}


Generally what happen when we compile a normal java program without any package (or namespace in C#) is that ".class" is generated in place where ".Java" files is placed.

So, executing below command will generate TestEnum.class in folder where TestEnum.java is located.

D:\Temp\javac TestEnum.java




But problem starts once you have put or create any namespace/package in ".java" file.

So executing below command results in given error even if everything is correct.

D:\Temp\java Chapter3.TestEnum
Error: Could not find or load main class TestEnum

Work around :
Found many articles talking about similar problem but with different solutions of trying classpath which did not work for this problem. There is twist while compiling a file manually on command line, especially those having package name specified on the top of program. Just include -d option with . which denotes current directory, and java compiler creates a dedicated folder as per the name of package provided in the class.

D:\Temp\javac -d . TestEnum.java









Please note that -d option does have impact only when there is any package name exclusively specified over the top of class, otherwise, it will also have same behavior as if  -d option were not present while compiling.