Wednesday, May 10, 2017

Maven as a Build Tool

Have you heard "Maven" before..?? I will explain to you it in a  way you can understand what is and why we have Maven.

When you are creating a project normally you need to add necessary supporting libraries. Have you experienced in having difficulties when you were doing this. Hope most of you will say the answer as "yes". 

Can we avoid those issues by having a tool which add all necessary libraries while you are building your project. The answer is obviously "yes". Because here you have Maven for do everything on behalf of you.



Maven makes Java programming easy..Although it referrers to as a build tool, but it is so much more that that.

We can use Maven to manage the entire lifecycle of our project in a way generating reports, and storing documents with its POM ( Project Object Model) repository. 

And it is not just for Java; C/C++, PHP, and Scala programmers can use Maven, too.

What is Maven..??
  • Maven is a "build management tool".
  •  Defines how our "*.java" files get compiled to "*.class" and  packaged into "*.jar" (or "*.war" or "*.ear") files.
  • Manages our CLASSPATH and all other tasks that are required to build our project. 
  • Similar to "Apache Ant" or "Gradle".
  • It attempts to be completely self-contained.
  • We don't need any additional tools for other common tasks like downloading & installing necessary libraries.
What are the main benefits of using maven ..??
  • We can get our package dependencies easily.
  •  Forces us to have a standard directory structure.
What are the Objectives of Maven..??
  • Making the build process easy.
  • Providing a uniform build system.
  • Providing quality project information.
  • Providing guidelines for best practices development.

Tuesday, May 2, 2017

Single-Page Applications (SPA)

You could find hundreds of reading stuffs about the single page application easily from the internet.But for the beginners  who are looking for get a basic idea about what is single page application, I hope you could get an clear idea from this blog. Here I have simply explained the basics of Single Page Application.
  • Let see simply what is SPA is..?
SPAs are web applications that load a single HTML page.
  • What is mean of "load a single HTML page"..?
That means we dynamically update that page when user interacts with the app.
  • How we can dynamically update the page..??
SPAs heavily use AJAX and HTML5 to create responsive Web apps, without constant page reloads. Simlpy it is the way we communicate with back-end servers without doing a full page refresh and get data loaded into our application. That means much of the work (process of rendering pages)  happenns on the client side.
  • Why we need SPA over regular website..??
Because in regular web app, every time when the application needs to display the data or need to submit data back to server it has to request a new page from the server and then render it in the web browser.

So why we cannot use this approach..??


With this approach nothing wrong if our application is a simple application. But when we need to create a rich user interface then our page might become very complex and we need to be loaded with lot of data.

Hence in this approach we need to ,

  • Generate complex pages on a server.
  • Transfer them to the client over internet.
  • Render them into the browser.
Because of these reasons it takes rime and degrade the user experience. So now have moved to SPAs with AJAX.
SPA allows refreshing only parts of the page when needs instead of reloading the whole page each time. It helps to improve the user experience since it is the way of reduced amount of pages refreshes. .

From the image I have put below, you could get a better picture in your mind about how SPA and regular app work.




Here are some advantages and disadvantages of SPA.

SPA advantages
  • Faster page loading time
  • Improved user experience
  • Decoupling front-end and back-end 
  • No need to write the code to render pages on server.
SPA disadvantages
  • Heavy client frameworks which are required to be loaded to the client

I hope you have got a basic understanding about single page application from here.


Friday, April 21, 2017

Checked and Unchecked exceptions

In java, there are two types of exceptions we can find.
  1.         Checked exceptions
  2.       Unchecked exceptions

Checked exception:

Checked exceptions are checked at the compilation time. If some code contains checked exception, that exception should be handled using try catch block or throw using throws keyword.

Example :

Let’s say in a java program we open a file and try to read it. In there we have to use FileReader(). FileReader() throws an exception called “FileNotFoundException” . and also I there we have to use readLine() and close() methods. Those are also throws checked exception called “IOException”.

Sample code :

            import java.io.*;

class Main {
    public static void main(String[] args) {
        FileReader file = new FileReader("C:\\test\\a.txt");
        BufferedReader fileInput = new BufferedReader(file);
         
        // Print first 3 lines of file "C:\test\a.txt"
        for (int counter = 0; counter < 3; counter++)
            System.out.println(fileInput.readLine());
         
        fileInput.close();
    }
}

Output :

               Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - 
unreported exception java.io.FileNotFoundException; must be caught or declared to be
thrown
         at Main.main(Main.java:5)

To fix this we have to use throws keyword or try-catch block.

Unchecked Exceptions :-

Unchecked exception are the exceptions which are not check in the compilation time. As an example, in C++, all exceptions are unchecked. So it is not forced by the compiler to handle or throw.

In Java, Errors and Run time Exception classes are unchecked. All others are checked.

Example :

                class Main {
   public static void main(String args[]) {
      int x = 0;
      int y = 10;
      int z = y/x;
  }
}

 Output :

        Exception in thread "main" java.lang.ArithmeticException: / by zero
         at Main.main(Main.java:5)
         Java Result: 1


Friday, April 7, 2017

AngularJS Directives

 What is AngularJS..??


  • Open-source web application framework. 
  • Library written in JavaScript.
  • Maintained by Google and an AngularJS community of developers. 
  • Distributed as a JavaScript file, and can be added to a web page with a <script> tag.
  • Assist with creating single-page applications. 
  • Require only HTML, CSS and JavaScript on the client side. 
  • Reads HTML for additional custom tag attributes. 
  • Extends HTML attributes with Directives which are in those custom attributes. 
  • Binds data (input output parts of the page) to HTML with expressions.

Note: 
To add angular to the web page , use below URL with script tag.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>


As i mentioned you earlier AngularJS extends with Directives let's look at what are those Directieves.

AngularJS directives are HTML attributes with an "ng" prefix. Here are some directives.

  • ng-app - defines the root element of an AngularJS application and will automatically initialize the app when a web page is loaded.
  • ng-model - binds the value of HTML controls (input, select, textarea) to application data.
  • ng-bin - binds application data to the HTML view.
  • ng-init - defines initial values for an AngularJS application. 
  • ng-repeat - repeats an HTML element.
   To get an idea about how directives are used i will show you an example.   

    Example 1 :




     
Example 2:
    
Example 3:











 

Note:
Here i have put only the very basic directives.If you want to follow more  AngularJS directives refer this link.

Angular Applications

  • modules - deifne AngularJS applications using ng-app directive.
  • controllers - control AngularJS applications using ng-controller directive..

 Example 1: AngularJS Module

 Note :
       
 Example 2: AngularJS Controller



Example 3: 


Note : 
Normally we put module and the controllers in separate JavaScript  files (myApp.js , myController.js ) and link them using script tag .







Tuesday, March 28, 2017

Microservices Architecture



Microservices Architecture


The term "Microservice Architecture" has come into the picture over the last few years. These days all most all are talking about the microservices. But what is a microservice really..?? why we need it..??and when we need microservices..??

Let see first what is microservice in a simple manner using an example..

what is a Microservice.??

simply, microservices architecture is when you separate your application into smaller applications (we will call them services) that work together.


Asssume that a developing team has developed a large application (ex: online education app) where all the services (ex: login service, registration services,  student service, course service... ) have put inside one data service ( This is what we called as Monolithic Architecture).

In microservices what we do is we breakdown this large application into smaller applications as separate services (login microservice , student microservice , course microservices...). Each microservice act  as a separate end point. In a technical way it describes a particular way of designing software applications as suites of independently deployable services in different servers.

NOTE :
In Monolithic Architecture all services are composed inside one data service rather than containing architecturally separate services.

let see what is the different between monolithic and microservices...























As you see in above picture in Monolithic there is only one service that serves everything and it is hosted on one server and it is hard to scale.

But in Microservies it has different services for each functionallity and we hosted each microservice on different server and it is easy to scale.

If one microservice needs data from some other microservice this communication doing using API which are sent JSON messages.

Why we say that scaling is hard in monolithic and easy in microservices..??

Assume you are using monolithic architecture to develop your application and one service only getting lot of requests from client. Can you scale up only one service or a function in your application then..?? No..since you have put all services into one single process you could not able to scale up one specific function there. But In microservices easily we can scale up one specific service to handle lot of requests coming into it.

I hope now you have the basic idea of microservices.





Monday, March 20, 2017

Version Control Systems (VCS)

Version Controlling

Didn't you ever struggle when you were trying to integrate everyone's files together while working on group projects with your colleagues.Was it easy for you all..??Is it worked smoothly once after  you merged your files..??I guess most of you will say Nooo.. just like me.

But there is a way that you can integrate your project in a proper manner without making files integration conflicts.

But How...??

Using a Version Control System easily you can avoid integration issues.

Now What is this Version Control System..??

Simply, version control system records the changes you make to your project's files , documents, programs, and other information stored as computer files over time. so that you can recall specific versions later.

This is what version control is about. It's really as simple as it sounds.

Do you know the amazing part of VCS...??

  • It allows to revert files back to a previous state.
  • Can revert the entire project back to a previous state.
  •  Also can compare the changes over time.
  • Can see who last modified something that might be causing a problem.


Popular examples for VCS

  • Github
  • Bitbucket
  • GitLab
  • Mercurial
  • Bazaar

Following links are good interactive demos for learning git.
The fundamentals are found in [1] and advanced branching demo is in [2].
[1] https://try.github.io

Distributed vs Centralized


There are many version control systems out there.Mainly we are divided version control systems into two groups.

1. Centralize
2. Distributed” (Decentralized)

First let's see what is the difference between Centralized and Distributed (Decentralized) version control systems.

Centralized version control systems are based on the idea that there is only a single “central” copy of the project  on a server and programmers will “commit” their changes to this central copy.

“Committing” a change simply means recording the change in the central system. Other programmers can then see this change. They can also pull down the change, and the version control tool will automatically update the contents of any files that were changed.Programmers no longer have to keep many copies of files on their hard drives manually, because the version control tool can talk to the central copy and retrieve any version they need

Distributed version control systems (DVCS) do not  rely on a central server to store all the versions of a project’s files. Instead, every developer “clones” a copy of a repository and has the full history of the project on their own hard drive. This copy (or “clone”) has all of the metadata of the original.

The most popular DVCS are Github , Bitbucket ,Mercurial and Bazaar.

I think you got a basic idea of what is version controlling and why we use version controlling..There are few terminologies we need to know when using a version control system for our project. From the provided link[3] below you can get familiar with those  terminologies.

Sunday, March 12, 2017

Diamond Problem in Java

Have you ever  heard about Diamond Problem in java...♦♦???

Yes I'm sure you are ,  if you have ever come across with Object Oriented Concepts  applying in Java. Consideration of the Diamond Problem comes with one of the popular concepts of OOP which is called as the "Inheritance" in Java.

Don't worry if you are new to this and never heard about Diamond Problem. Let's start now..

What is Diamond Problem?

Diamond Problem is sometimes referred as the "Deadly Diamond of Death".

Simply it is an ambiguity that arises when two classes (class B , class C) inherit from a class (class A) and another class (class D) inherits from both B and C.

Have a look at this example if you still didn't get it..


Let's assume that Super class A is an abstract class and class B ,C and D are concrete classes.

Note:
Abstract class :  Base class or a Super class where only declared abstract methods and which does not provide implementations for all of its methods.
Concrete class : Derived class that provides the basic implementations for all of the methods that are not already implemented in the super class.

Here the ambiguity arises in class D when it tries to call Close() method. The compiler doesn't know from which class does it inherit as shown in the figure.

This is a serious problem for some other popular OOP languages like C++ that allows multiple inheritance while java doesn't allow multiple inheritance in classes.

Note:
Multiple Inheritance : Where class can inherit from more than one class

Consider the following situation related to above example.

Class_A.java

Class_A.java
Class_B.java




Class_C.java


Now let's assume Class_D implementation happens as below and it's extending both Class_B and Class_C.

Class_D.java




Here Class_D inherits from both Class_B and Class_C and both classes contain Close() method with different implementations.

In Class_D Test() method call to super class Close() method and this is the place where ambiguity occurred. Because compiler doesn't know which super class method to execute. This referred as diamond problem in Java and this is the main reason why java doesn't support multiple inheritance in classes.

How to Overcome Diamond Problem ?

To overcome this issue Java proposed the solution as Multiple Inheritance in Interface where a single interface can extend multiple interfaces.

Interface_A.java


Interface_B.java

Interface_C.java

Interface_D.java



This is perfectly fine because interfaces declare the methods and actual implementation will be done by concrete classes implementing the interfaces.So there is no possibility of having any kind of ambiguity in multiple inheritance in Java Interfaces.

That's why Java class can implements multiple inheritance like below,



Note: 
Here Class_D must implement the Close() method.If not compiler gives an error by saying you must implement the Close() method at Class_D.