AngularJS – Part 1

  • What is Angular JS ?
    • javascript forms the {controller and model}
      • html+angular script forms the {view}
  • How to use Angular JS Code ?
    • Include the angular script :https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js
    • Include ng-app attribute to the root HTML element of the AngularJS app and set its value to the module name which holds all the controllers required by the app.
    • Include the controller for the angular js app,which is a normal JS function taking in multiple parameters like a $scope,$http etc.It is a function where we can manipulate and populate values for the model
      • angular is object created by Angular Js whose parameters include the app name which we have already registered.
      • controller function is registered using angular.module().controller() function call by passing the controller name,which we will be referring in the view,thus associating the view with the controller.
    • Include the view in the html tags and provide the controller name as ng-controller=“controller name ” to which the view is associated with.
    • Include the model values from this controller to the view by using angular evaluation {{}}.We can obtain bean values,or attribute values  etc from the controller to be represented in the view
  • How to execute an AngularJS application ?
    • HTML document is loaded and evaluated by the browser
    • AngularJS file is loaded, the angular global object is created.It also registers the controller and its functions are executed.
    • Now, AngularJS scans through the HTML and looks for AngularJS apps and views.
    • On finding a view the angular js connects that view to the corresponding controller function.
    • AngularJS executes the controller functions to render the views with data from the model populated by the controller.
    • AngularJS listens for browser events and if any of these browser events require a view to change, AngularJS will update the view correspondingly. If the event requires that the corresponding controller function is called, AngularJS will do that too. Not all events require the controller function to be called, even if the view is updated as a result of the event.

Iterator Pattern

What is Iterator Design Pattern ?

  • is a behavioural design pattern
  • defines a way to move through a collection of data in a particular class
  • We can also include other uses to this as well.
  • GoF definition : “Provides a way to access the elements of an aggregate object without exposing its underlying representation.”

Where do we use it ?

  • Collections Interface

Advantages

  • uniformly traverse through a group of objects.

References

  1. http://www.avajava.com/tutorials/lessons/iterator-pattern.html
  2. https://dzone.com/articles/design-patterns-iterator
  3. http://javapapers.com/design-patterns/iterator-design-pattern/

Interpreter Pattern

What is Interpreter Design Pattern ?

  • is a behavioural design pattern
  • we create an expression or grammar of a sentence,how a sentence needs to be,using which we interpret the context of given sentence
  • this pattern helps in interpreting a particular context by evaluating language grammar or expression
  • For every grammar rule we need to include in defining the context is provided in the class.
  • GoF definition : “Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.”

Where do we use it ?

  • java.util.Pattern / java.text.Format uses this.
  • Google Transalators
  • Java compiler that interprets java source code into byte code.
  • SQL Parsing
  • Symbol processing engine

Disadvantages

  • Time consuming
  • Affects efficiency

References

  1. https://sourcemaking.com/design_patterns/interpreter
  2. http://javapapers.com/design-patterns/interpreter-design-pattern/
  3. http://www.journaldev.com/1635/interpreter-design-pattern-in-java-example-tutorial

JTA

What is a Transaction  and how does it work ?

  • it is a communication between two or more hosts for utilising the resources.
  • is a set of operations performed serially.Even if an operation fails,the entire transaction fails and all the previous successfully completed transactions has to be rolled back so that system will no longer be  in a corrupt state.
  • can be database operation between the client and server or even a message passing between two servers.
  • Transactional Resources are provided by the hosts and Transaction Manager  manages the transaction by creating and monitoring the operations
  • The standard model for transaction : f X/Open XA Model
    • JDBC for databases
    • JMS for messaging services etc
    • Transaction Processing Monitor or TP Monitor or Transaction Manager manages the transactions across multiple resources
    • Every Transaction manager has its own API to work with its resources.
    • They also have API to talk between resources in a distributed env
    • They also talk with the application to commit,rollback the transactions

What is JTA ?

  • Java Transaction API  is a part of J2EE API
  • helps in doing distributed transactions across environments such as databases and queues.
  • is an interface between the transaction manager and the other components such as  the application, the application server, and the resource managers.
  • is a java implementation of X/Open XA architecture.
  • Packages : javax.transaction , javax.transaction.xa

Why do we use JTA ?

  • If we use hetrogenous databases and need updation into all of these together,we can use this JTA
  • Though JMS or JDBC itself has transactional abilities,we still use JTA for the following reasons,
    • Both JMS and JDBC have independent trancactions and many not communicate with each other and in case any issue with either of related transactions,there can be contradicting results
    • If we include JTA,it take the role of individually checking the transactions for JDBC or JMS and then provide a single handed commitment
    • JTA provide XAResource interface which the user can custom implement.
  • Nicely explained in this SOF Answer

How does JTA work ?

  • JTA has 3 elements
    • implemented by the javax.transaction.TransactionManager interface
    • helps in efficient schedule and usage of large number of transactions
    • it allows the application server to control transaction boundaries on behalf of the application being managed.
    • transaction manager is responsible for making the final decision either to commit or rollback any distributed transaction
    • implemented by the javax.transaction.UserTransaction interface
    • programatic control of  transaction boundaries
    • starts a global transaction and associates the transaction with the calling thread.
    • implemented by the javax.transaction.xa.XAResource interface
    • is the Java mapping of the X/Open XA implementation
    • we can provide a custom implementation which helps a resource to participate in distributed transactions.
    • TransactionManager
    • UserTransaction
    • XA Resource

Any Limitations ?

    • No support for nested transactions .

References :

  1. https://www.progress.com/jdbc/resources/tutorials/understanding-jta/distributed-transactions-and-the-transaction-manager
  2. http://www.java-tips.org/java-ee-tips-100042/17-enterprise-java-beans/1472-introduction-to-the-java-transaction-api.html
  3. https://www-01.ibm.com/support/knowledgecenter/ssw_i5_54/rzaha/jtacommit.htm?lang=en

RMI

What is RPC ?

  • Remote Procedure Call – creates distributed applications
  • client-server system in which some program execution might happen in different machine
  • request-response protocol
  • Steps :
    1. Client invokes the client stub with parameters for the remote procedure call
    2. The stub creates a message with the params – marshalling and then makes a system call to send the message
    3. The message is send from the client machine to the server machine by the OS of client machine
    4. The server machine passes the incoming messages to the server’s stub.
    5. The server stub generates the parameters from the message – unmarshalling
    6. The server stub calls the server procedure and executes using the parameters passed
    7. The response is again passed from server to client via the stubs

What is RMI ?

  • Remote Method Invocation – allows objects to invokes other objects running in different server machine
  • is a java API
    • performs remote procedure calls in OOP
    • transfers serialized java classes
    • has distributed garbage collection
  • Package -java.rmi

Currently REST / SOAP services have taken over RMI.

References :

http://www.javacoffeebreak.com/articles/javarmi/javarmi.html

Strategy Design Pattern

  •  is a behavioural design pattern
  • used when only one of many available implementations is chosen at runtime of the application
  • Example:

Customer.java

package StrategyPattern;

import java.util.List;

class Customer {

	private List<Product> products;
	private PaymentStrategy strategy;

	public Customer(PaymentStrategy products,List<Product> productList) {
		this.products = productList;
		this.strategy = strategy;
	}


	// Payment of bill
	public int printBill() {
		int sum = 0;
		
		for (Product prod : products) {
			sum += prod.getAmount();
		}
		System.out.println("Total due: " + sum);
		return sum;
	}


}

Online Payment.java

package StrategyPattern;

public class OnlinePayment implements PaymentStrategy {

	@Override
	public int getPaidMoney(int amount) {
		if(amount > 500)
		return (amount - (amount/2));
		
		return amount;
	}
	
	
}

PaymentOnDeleivery.java

package StrategyPattern;

public class PayementOnDeleivery implements PaymentStrategy {

	@Override
	public int getPaidMoney(int amount) {
		if(amount > 500)
		return amount-500;
		
		return amount;
	}
	
	
}

PaymentStrategy.java

package StrategyPattern;

interface PaymentStrategy {
	
	public int getPaidMoney(int amount);

}

Product.java

package StrategyPattern;

public class Product {
	private String name;
	private int amount;

	public Product(String name, int amount) {
		super();
		this.name = name;
		this.amount = amount;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}
}

TestStrategyPattern.java

package StrategyPattern;

import java.util.ArrayList;
import java.util.List;

public class TestStrategyPattern {
	public static void main(String[] args) {
		// Online
		Product p00 = new Product("bed", 1200);
		Product p01 = new Product("paint", 1200);
		Product p02 = new Product("dress", 1200);
		List<Product> prodList1 = new ArrayList<Product>();
		prodList1.add(p00);
		prodList1.add(p01);
		prodList1.add(p02);
		Customer a = new Customer(new OnlinePayment(), prodList1);
		System.out.println(a.printBill());

		// COD
		Product p10 = new Product("cups", 1200);
		Product p11 = new Product("phone", 12000);
		Product p12 = new Product("dress", 1200);
		List<Product> prodList2 = new ArrayList<Product>();
		prodList2.add(p10);
		prodList2.add(p11);
		prodList2.add(p12);

		Customer b = new Customer(new PayementOnDeleivery(), prodList2);

		System.out.println(b.printBill());
	}
}

Output : Total due: 3600
3600
Total due: 14400
14400

Nested Class

What is java nested class ?

  • a class declared or defined inside another class or interface
  • these class are considered as members of the enclosing class
  • can be of public,private,protected or default type
  • has access to all members of the containing class,inclusive of private members
  • these classes can also be inherited by the subclass referring to the outer class.
  • consists of a logical grouping of similar classes or also can be reducing the scope of usage of inner class

How does an nested class work ?

  • java compiler creates two class files as technically there are two classes
    • one for outer class
    • one for inner class with name “Outer$Inner”.
  • To instantiate an inner class from outside of outer class, we need to create the instance of outer class and then call the inner class
  • To call the inner class within the same outer class,we can use them directly as normal class
  • Though a separate class file is generated we cannot access the inner class
  • cannot contain any static members inclusive of main method()
  • access to the inner class can be obtained only at runtime when we have an outer class instance.

Why not to use Nested classes ?

  • affects readability of the code

Types of Nested Class Continue reading