Sunday, 17 May 2015

Introduction of Vaadin

Developing application specially web application require skills of other technologies like HTML , CSS, JavaScript, Web Application architecture and  plus many  other tools and technologies ; which directly effect developer productivity ,time frame and project budget / cost.  Vaadin provide the solution all of these obstacle.While traditional web programming is a fun way to spend your time learning new web technologies,you probably want to be productive and concentrate on the application logic. The server-side Vaadin framework takes care of managing the user interface in the browser and the AJAX communications between the browser and the server. With the Vaadin approach, you do not need to learn and deal directly with browser technologies, such as HTML or JavaScript.
Vaadin is a web application development framework that allow you to build web application traditionally similar with desktop application like AWT or SWING.
In Vaadin all user interaction with server by event driven (Observer Design Pattern) using AJAX support in vaadin by creating rich internet application(RIA).

Vaadin support both a server side and client side development model. In server side application architecture consist of  server side framework and client side engine. Engine run in browser as a JavaScript code , rendering the user interface and delivering the user interaction to the server with any need of plugin.The client-side development model allows building new client-side widgets and user interfaces with the GWT toolkit included in Vaadin. The widgets can be integrated with server-side component counterparts to enable using them in server-side applications. You can also make pure client-side

UIs, which can communicate with a back-end service.

Example
@Title(“My Vaadin UI”)
public class HelloWorld extends com.vaadin.UI {
     @Override
     protected void init(VaadinRequest request) {
           // Create the content root layout for the UI
           VerticalLayout content = new VerticalLayout();
           setContent(content);
           // Display the greeting
           content.addComponent(new Label(“Hello World!”));
     }
  }
A Vaadin application has one or more UIs that extend the com.vaadin.ui.UI class. A UI is a part of the web page in which the Vaadin application runs. An application can have multiple UIs in the same page, especially in portals, or in different windows or tabs. A UI is associated with a user session, and a session is created for each user who uses the application. In the context of our Hello World UI, it is sufficient to know that the underlying session is created when the user first accesses the application by opening the page, and the init() method is invoked at that time. 






Saturday, 16 May 2015

What is Design Pattern

Design Pattern are the best practice approach used by experienced Object Oriented Software developer. Design pattern give tested solution's of repeatedly general problem that software engineer faced during development.
This prevent the developer for to changes in code due to client requirement  ; simply design pattern save time and code reusable.  

Simple definition is 
"Design Pattern are generic solutions of all repeatably problem that encounter in object oriented software application".
It's based on the relationship between classes and object with defined responsibilities that behave act like that away us from error prone programming in future.

Advantages of design pattern are

  1. Code Readability 
  2. Provide Robust Architecture
  3. Solutions from experienced Developer
  4. Code re-usability
  5. Overcome Rapid Changes 



There are many different design pattern are  

  1. Factory Design Pattern
  2. Observer Design Pattern
  3. Singleton Design Pattern
  4. Strategy Design Pattern
  5. MVC Design Pattern
  6. Composite Design Pattern
  7. Memento Design Pattern
  8. Decorator Design Pattern
  9. Proxy Design Pattern
  10. Iterator Design Pattern

Sunday, 22 February 2015

What is Spring

Spring is the part of Java Enterprise Edition and was developed by Rod Johnson in 2003 and Spring framework is an open source Java platform and Spring is a "Framework" that contains many framework like Hibernate/ JPA (Data Persist-ency), JSF , JMS , EJB and many other framework  and these all makes the easy development of JavaEE application.


What is Framework?

Framework is a collection of API or structure that help you in technical problem and make your work smooth and faster.Framework take the headache of workload from your self into own self for 60% to 70%.

Overview Of Spring

Basically Spring is a framework of 


Modules In Spring

The Spring Framework consists of features organized into about 20 modules. These modules are grouped into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation, Messaging, and Test, as shown in the following diagram.



http://docs.spring.io/spring/docs/current/spring-framework-reference/html/overview.html


Feature Of Spring

  1. Dependency Injection: 
    Dependency Injection is a design pattern that promotes loose coupling between java objects. This is an extension of the Factory pattern where-in objects do not resolve their dependencies themselves but let an external container (like Spring) take the responsibility of resolving the dependencies. 
    The benefits of using dependency injection is that any object of appropriate type can be injected just by changing the spring configuration. This makes the objects loosely coupled to other objects and promotes 'coding to interface' rather than hardcoding dependencies. 
    Dependency Injection is the back-bone of Spring framework.
  2. Support for Aspect Oriented Programming (a.k.a. AOP): 
    Aspect Oriented Programming is a style of programming used to factor out cross-cutting concerns across many java objects and keep the code for handing the cross-cutting concern in just one place. Examples of cross-cutting concerns could be logging, method tracing, auditing etc. Using AOP, we need not pollute numerous java classes with the same code for handing cross-cutting concerns thereby promoting high cohesion among java classes.
  3. Integration technology for easier integration with other Java libraries: 
    Spring uses Dependency Injection and AOP to provide integration facility for many java libraries for access database (e.g. JDBC, Hibernate, myBatis, etc.), queues (JMS based queue integration), web access (e.g. Servlets, Struts, etc.) and to virtually all J2EE technologies. 
    Spring has emerged as the de-facto integration technology for the Java/J2EE stack.



Dependency Injection (DI) is a design pattern and Dependency Injection design pattern allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable and maintainable.DI can help make your code architecturally pure.

Sunday, 19 October 2014

What is HashMap in Java

What is Hashmap?
HashMap in Java is one of the most popular class Collection Framework . The HashMap class uses a hashtable to implement the Map interface.

In above I used "Hash Table" terminology, let's take overview  of "What is Hash Table"
Here's an explanation in laymans terms.
Let's assume you want to fill up a library of books, and not just stuff them in there, but you want to be able to easily find them again when you need them.So, you decide that if the person that wants to read a book knows the title of the book, and the exact title to boot, then that's all it should take. With the title, the person, with the aid of the librarian, should be able to go find the book easily and quickly.
So, how can you do that? Well, obviously you can keep some kind of list of where you put each book, but then you have the same problem as searching the library, you need to search the list. Granted, the list would be smallers, and easier to search, but still, you don't want to search sequentially from one end of the library (or list) to the other.
You want something that, with the title of the book, can give you the right spot at once, so all you have to do is just stroll over to the right shelf, and pick up the book.But how can that be done? Well, with a bit of forethought when you fill up the library, and actually, a lot of work when you fill up the library.
Instead of just starting to fill up the library from one end to the other, you devise a clever little method. You take the title of the book, run it through a small computer program, which spits out a shelf number and a slot number on that shelf. This is where you place the book.
The beauty of this program is that later on, when a person comes back in to read the book, you feed the title through the program once more, and get back the same shelf number and slot number that you were originally given, and this is where the book is located.
The program, as others have already mentioned, is called a hash algorithm or hashing, and usually works by taking the data fed into it (the title of the book in this case) and calculates a number from it.For simplicity, let's say that it just converts each letter and symbol into a number, and sums them all up. In reality it's a lot more complicated than that, but let's leave it at that for now.The beauty of such an algorithm is that if you feed the same input into it again and again, it will keep spitting out the same number each time.

What is Hashing?

Hashing in its simplest form, is a way to assigning a unique code for any variable/object after applying any formula/algorithm on its properties. A true Hashing function must follow this rule:
Hash function should return the same hash code each and every time, when function is applied on same or equal objects. In other words, two equal objects must produce same hash code consistently.
Note: All objects in java inherit a default implementation of hashCode() function defined in Object class. This function produce hash code by typically converting the internal address of the object into an integer, thus producing different hash codes for all different objects.

Characteristics of HashMap 

HashMap use 2 data structure:
  • Hash Use hash value to group elements into slots, control by hash() method of HashMap,
  • linked list (singly)
    Each slot is a singly linked list, their key has the same hash value,
    the slot index is control by indexFor() method of HashMap,
Find value:
First find the slot by hash value, then loop each element in the slot until found or end,
Add value:
First find the slot by hash value,
then try find the value:
* if found, then replace the value,
* if not found, then add a new one to begining of the slot,
Capacity
Capacity is slot size, as element count increase, capacity is larger but liner to element count, and finally equals to size (Integer.MAX_VALUE),
Linked list length:
As element count increase, length is liner to a small constant value, and finally equals to 1,
Speed:
put / get, has O(1) speed, because slot is access via index, and linked list length is very small,
Space:
The slot size increase as element count increase,
but it's empty element are null, so not much space is taking,
Resize:
When resize capacity, it also need to do rehash, this might take a while,


Coding Example of HashMap
public class YasirTest {

public static void main(String[] args){
    HashMap hash=new HashMap<>();
    int i;
    for(i=1;i<=5;i++)
        hash.put(i, "Java Programmer "+i);


    for(i=1;i<=5;i++)
        System.out.println(hash.get(i));
}
}



Friday, 17 October 2014

ArrayList in Java

What is Collection?
A collection is a general term that means something like "a bunch of objects stored in a structured manner". or Simply Collection is also called Container.
Collection is a Java Interface.
A collection represents a group of objects, known as its elements. This framework is provided in the java.util package. Objects can be stored, retrieved, and manipulated as elements of collections.
Collections can be used in various scenarios like storing phone number, Employee names database etc. They are basically used to group multiple elements into a single unit. Some collections allow duplicate elements while others do not. Some collections are ordered and others are not.

Why ArrayList? If you don't know how many items are going to be held in your array, you may be better off using something called an ArrayList. 

What is ArrayList?
An ArrayList is a dynamic data structure(Collection), meaning items can be added and removed from the list. A normal array in java is a static data structure, because you stuck with the initial size of your array but Java ArrayList is a dynamic array, an array that can grow after it is created.


Which Package do you need?
ArrayList is in the "java.util" package and if you want to use this then you should import this package in your code like this
   import java.util.ArrayList;


Hierarchical Structure



The ArrayList class extends AbstractList and implements the List interface.


Why we use ArrayList instead Array?
ArrayList supports dynamic arrays that can grow as needed.
In Java, standard arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. But, sometimes, you may not know until run time precisely how large of an array you need. To handle this situation, the collections framework defines ArrayList. In essence, an ArrayList is a variable-length array of object references. That is, an ArrayList can dynamically increase or decrease in size. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.


How to delcare and use ArrayList?
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");


How to show String ArrayList?
for(String temp:list){
  System.out.println(temp);
}
OR
for(int i=0;i<list.size;i++){
  System.out.println(list.get(i));
}

Saturday, 26 July 2014

Usage of "this" operator in Java

The this keyword is a reference to the current object.
Another way to think about it is that the this keyword is like a personal pronoun that you use to reference yourself. Other languages have different words for the same concept. VB uses Me and the Python convention (as Python does not use a keyword, simply an implicit parameter to each method) is to use self.
If you were to reference objects that are intrinsically yours you would say something like this:
My arm or my leg
Think of this as just a way for a type to say "my". So a psuedocode representation would look like this:

class Employee
{
    private int age;

    public Employee(int age)
    {
        my.age = age;
    }
}


First lets take a look on code
public class Employee  {

private int empId;
private String name;

public int getEmpId() {
    return this.empId;
}

public String getName() {
    return this.name;
}

public void setEmpId(int empId) {
    this.empId = empId;
}

public void setName(String name) {
    this.name = name;
}
}
In the above method getName() return instance variable name. Now lets take another look of similar code is
public class Employee  {

private int empId;
private String name;

public int getEmpId() {
    return this.empId;
}

public String getName() {

    String name="Yasir Shabbir";
    return name;
}

public void setEmpId(int empId) {
    this.empId = empId;
}

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


public static void main(String []args){
    Employee e=new Employee();
    e.setName("Programmer of UOS");

    System.out.println(e.getName());

}
}
Output Yasir Shabbir
  • this operator always work with instance variable(Belong to Object) not any class variable(Belong to Class)
  • this always refer to class non static attribute not any other parameter or local variable.
  • this always use in non static method
  • this operator cannot work on static variable(Class variable)
**NOTE:**It’s often a logic error when a method contains a parameter or local variable that has the same name as a field of the class. In this case, use reference this if you wish to access the field of the class—otherwise, the method parameter or local variable will be referenced.