Monday, 9 September 2013

A 5 Minute Overview of All New HTML5 Markup Elements

Introduction 

With the announcement of the recommended specification of HTML5, we are even closer than ever to the announcement of the final specification of HTML5 (which can happen at any moment). HTML5 introduces a bunch of new markup elements as well as support for programmatic APIs which can be used with JavaScript to build web pages which can act as web applications.
         
    In this article, we will do a quick rundown of all the new markup elements new in HTML5.

  • Article - The article element is a new sectional element and is used to represent a self-contained entry in a document, page, application or site. 
  • Aside - The aside element is used to represent content which is tangentially related to the content which is around aside element, sometimes, represented as sidebars. 
  • audio - The audio element is a type of media element used to represent an audio stream. 
  • canvas - The canvas element is used for rendering graphs, game graphics, art images or other visual images using a resolution dependent bitmap canvas. 
  • command - The command element represents a command which can be invoked by a user. It can be part of a menu element (explicitly part of a context menu or toolbar); alternately it can be placed anywhere else on the page to define a keyboard shortcut or to define other commands. 
  • datalist - The datalist element is used to represent a set of option elements which can act as options of other controls. datalist elements are wired to input elements using the list attribute on the input element. 
  • details - The details element is used to represent an area where users can go to obtain additional information. The details element in an interactive element represented as a widget. 
  • embed - The embed element is used to represent an integration point for a non-HTML application or interactive content. 
  • summary - The summary element is an interactive element used to represent a summary or legend for the rest of the content. 
  • Figure - The figure element is a grouping element is used to group content that is self-contained and is typically referenced as a single unit from the main flow of the document. 
  • Figcaption - The figcaption element represents the caption for a figure element. 
  • Footer - The footer element is used to represent a footer of the preceding sectional content. 
  • Header - The header element is used to represent a header for the succeeding sectional content. 
  • keygen - The keygen element is used to represent a key pair generator control which is used to store the private key in the local keystore and send the public key to the server when the form is submitted. The keygen element is a form element. 
  • mark - The mark element is used to highlight a range of text in a document for reference purpose. It is equivalent of using a highlighter to highlight a bunch of text. 
  • meter - The meter element is used to represent a scalar measurement within a known range, for example, how many respondents were male with kids. It should not be used to indicate progress. 
  • nav - The nav element is a section with navigation links and represents a section of page that links to other pages. 
  • output - The output element is used to represent the result of a calculation or user action. It supports representation of explicit relationship between itself and the elements that represent the values that went into the calculation of the output value. 
  • progress - The progress element is used to represent the progress of a task. It supports both indeterminate situations as well as determinate situations. 
  • Ruby - The ruby element is used to support one or more spans of content to be marked with ruby annotations. 
  • rt - The rt element is used to mark the ruby text component for a ruby annotation. 
  • rp - The rp element is used to provide parenthesis around a ruby text component of a ruby annotation. 
  • Section - The section element is used to represent a generic section of a document or application, usually containing a heading. E.g. chapters in a book. 
  • Source - The source element is used to specify to alternate media resources for media elements. It is not supposed to be dynamically modified, as that will have no effect. 
  • Summary- The summary element is an interactive element which is used to represent a summary, or caption for a details element. 
  • Time - The time element represents datetime attribute contents in a machine readable form (limited to various kinds of dates, times, time-zone offsets, and durations). 
  • Video - The video element is a type of media element used to represent a video stream. 
  • wbr - The wbr element is used to represent a line break in a web page. 
Summary 
In this article, we learned about all the new markup elements in HTML5. I hope you have found this information useful. In upcoming articles, we will review these new markup elements in more detail.

What is HTML5?

Introduction
It won’t be incorrect to say that the World Wide Web was invented as a consequence of Tim-Berners Lee drafting a memo that defined a markup language called HyperText MarkupLanguage (HTML) to create documents and reference each other in a consistent manner to enable access via Internet. 

The markup language was quite simple when it was originally drafted in 1991 where it had only 18 HTML tags. As web programming evolved, it was necessary to ramp up the specification. HTML4 specification was announced in December 1997 and later revised in April 1998 and December 1999. 

The changing landscape for web programming resulted in yet another revision of the HTML spec, which was announced as a draft specification in January 2008. HTML5 garnered world attention when the Apple CEO at that time, late Steve Jobs, suggested the future of web hinged on HTML5 and did not see Flash as needed to view web content. This statement brought renewed focus on HTML5 and in December 2012, the HTML5 specification was considered W3C (World Wide Web Consortium) recommendation. Adobe later announced it was discontinuing Flash, leaving HTML5 alone on the race to support media-rich Internet. 

One of the areas that has not been adequately addressed by prior HTML specifications were “Web Applications”. The HTML5 specification addresses these shortcomings; moreover, it also addresses concerns which have been raised against the past version of the HTML specifications over the years. 

What is a layman’s definition of HTML5? 
HTML5 can be viewed as a specification with support for new markups as well as support for programmatic APIs (JavaScript) to support web application scenarios. The HTML5 specification intends to provide first class support for multimedia interaction as well as introduce programmatic capabilities into the specification allowing a web page to act as an application. HTML5 was conceived after observing that there were a lot of mixed features in the HTML and XHTML specifications, and it was an attempt to define a single markup language which could be written in either HTML or XHTML syntax. 

How does the HTML5 landscape look today? 
Today, majority of the browsers have declared HTML5 compatibility as their goals. Browser vendors have rushed to build support for HTML5 into their browsers even though the specification has not been formalized. At the time of writing this article, Google’s Chrome led the pack amongst the popular browser in terms of HTML5 support. Note that Maxthon browser had the most HTML5 compatibility, but it is not have significant market share globally. Google Chrome (version 28) scores 463 points on HTML5test.com website out of a possible 500. Opera (version 15) and Firefox (version 22) are close behind with scores of 423 and 410 respectively. Internet Explorer (version 10) is at the bottom of the list of popular browsers with a score of 320. While none of the browsers are fully compliant to the HTML specification, the race to reach full HTML5 compatibility between the browser vendors is on, and only time will tell who will win.

Summary 
In this article, we learned about HTML5 and its history. In upcoming articles in this series, we will explore how to build HTML5 web pages using the new markups as well as the support for JavaScript APIs. I hope you have found this information useful.

Wednesday, 14 August 2013

Design Pattern Overview

Design patterns represent the best practices used by experienced object-oriented software developers.

Design patterns are solutions to general problems that software developers faced during software development. These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time.

Gang of Four (GOF) In 1994, four authors Erich Gamma, Richard Helm; Ralph Johnson und John Vlissides published a book titled Design Patterns - Elements of Reusable Object-Oriented Software which initiated the concept of Design Pattern in Software development. These authors are collectively known as Gang of Four (GOF).
According to these authors design patterns are primarily based on the following principles of object orientated design. Program to an interface not an implementation
 Favor object composition over inheritance

Usage of Design Pattern 

Design Patterns have two main usages in software development.

Common platform for developers 

Design patterns provide a standard terminology and are specific to particular scenario. For example, a singleton design pattern signifies use of single object so all developers familiar with single design pattern will make use of single object and they can tell each other that program is following a singleton pattern.

Best Practices 

Design patterns have been evolved over a long period of time and they provide best solutions to certain problems faced during software development. Learning these patterns helps un-experienced developers to learn software design in an easy and faster way.

Types of Design Pattern
As per the design pattern reference book Design Patterns - Elements of Reusable Object-Oriented Software, there are 23 design patterns.

S.N.       Pattern & Description
1. Creational Patterns
These design patterns provides way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case.
2. Structural Patterns
These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
3. Behavioral Patterns
These design patterns are specifically concerned with communication between objects.
4. J2EE Patterns
These design patterns are specifically concerned with the presentation tier. These patterns are identified by Sun Java Center.

Difference between forward and sendRedirect

forward

Control can be forward to resources available within the server from where the call is made. This transfer of control is done by the container internally and browser / client is not involved. This is the major difference between forward and sendRedirect. When the forward is done, the original request and response objects are transfered along with additional parameters if needed.

redirect

Control can be redirect to resources to different servers or domains. This transfer of control task is delegated to the browser by the container. That is, the redirect sends a header back to the browser / client. This header contains the resource url to be redirected by the browser. Then the browser initiates a new request to the given url. Since it is a new request, the old request and response object is lost.
For example, sendRedirect can transfer control from http://javapapers.com to http://anydomain.com but forward cannot do this.
‘session’ is not lost in both forward and redirect.
To feel the difference between forward and sendRedirect visually see the address bar of your browser,
in forward, you will not see the forwarded address (since the browser is not involved)
in redirect, you can see the redirected address.

When can we use forward and when can we use sendRedirect?

Technical scenario: redirect should be used
  1. If you need to transfer control to different domain
  2. To achieve separation of task.
For example, database update and data display can be separated by redirect. Do the PaymentProcess and then redirect to displayPaymentInfo. If the client refreshes the browser only the displayPaymentInfo will be done again and PyamenProcess will not be repeated. But if you use forward in this scenario, both PaymentProcess and displayPaymentInfo will be re-executed sequentially, which may result in incosistent data.
For other than the above two scenarios, forward is efficient to use since it is faster than sendRedirect.

Example for forward and sendRedirect based on real world

Consider the real world scenario, the milk man comes and asks for monthly payment to you in your house. Here house is the container and you are a resource existing in the container. Milk man is the client or browser.
He asks for the monthly payment to you, this is the request made by the browser to resource A. If you go inside your house and ask your mother (another resource B inside the same container) for the cash and come back and deliver to milkman this is called forward.
If you ask the milkman to speak himself to your mother inside your house or you ask the milkman to speak to your father who is in his office (different domain) then this is called redirect.

Adapter Design Pattern

Adapter pattern works as a bridge between two incompatible interfaces.
This type of design pattern comes under structural pattern as this pattern combines the capability of two independent interfaces.
This pattern involves a single class which is responsible to join functionality of independent or incompatible interfaces.
A real life example could be a case of card reader which acts as an adapter between memory card and a laptop. You plugins the memory card into card reader and card reader into the laptop so that memory card can be read via laptop.
We are demonstrating use of Adapter pattern via following example in which an audio player device can play mp3 files only and wants to use an advanced audio player capable of playing vlc and mp4 files.
Implementation 
We've an interface MediaPlayer interface and a concrete class AudioPlayer implementing theMediaPlayer interface. AudioPlayer can play mp3 format audio files by default.

We're having another interface AdvancedMediaPlayer and concrete classes implementing theAdvancedMediaPlayer interface.These classes can play vlc and mp4 format files. We want to make AudioPlayer to play other formats as well.
To attain this, we've created an adapter class MediaAdapter which implements the MediaPlayer interface and uses AdvancedMediaPlayerobjects to play the required format. AudioPlayer uses the adapter class MediaAdapter passing it the desired audio type without knowing the actual class which can play the desired format. AdapterPatternDemo, our demo class will useAudioPlayer class to play various formats.

Class Diagram



Steps 
Use the following steps to implement the above mentioned design pattern.

Step 1 
Create interfaces for Media Player and Advanced Media Player.
MediaPlayer.java
public interface MediaPlayer {
           public void play(String audioType, String fileName);
}
AdvancedMediaPlayer.java
public interface AdvancedMediaPlayer {
           public void playVlc(String fileName);
           public void playMp4(String fileName);
}

Step 2
Create concrete classes implementing the AdvancedMediaPlayer interface.
VlcPlayer.java
public class VlcPlayer implements AdvancedMediaPlayer{ 
           @Override 
           public void playVlc(String fileName) { 
                      System.out.println("Playing vlc file. Name: "+ fileName); 
           } 
           @Override 
           public void playMp4(String fileName) { 
                      //do nothing 
           } 

Mp4Player.java 
public class Mp4Player implements AdvancedMediaPlayer{ 
           @Override 
           public void playVlc(String fileName) { 
                      //do nothing 
           } 
           @Override 
           public void playMp4(String fileName) { 
                      System.out.println("Playing mp4 file. Name: "+ fileName); 
           } 
}

Step 3 
Create adapter class implementing the MediaPlayer interface.
MediaAdapter.java 
public class MediaAdapter implements MediaPlayer { 
           AdvancedMediaPlayer advancedMusicPlayer; 
           public MediaAdapter(String audioType){ 
                      if(audioType.equalsIgnoreCase("vlc") ){ 
                                 advancedMusicPlayer = new VlcPlayer(); 
                      } else if (audioType.equalsIgnoreCase("mp4")){ 
                                 advancedMusicPlayer = new Mp4Player(); 
                     
          
           @Override 
           public void play(String audioType, String fileName) { 
                      if(audioType.equalsIgnoreCase("vlc")){ 
                                 advancedMusicPlayer.playVlc(fileName); 
                      }else if(audioType.equalsIgnoreCase("mp4")){
                                  advancedMusicPlayer.playMp4(fileName); 
                     
          
}
 

Step 4 
Create concrete class implementing the MediaPlayer interface. 
AudioPlayer.java 
public class AudioPlayer implements MediaPlayer { 
           MediaAdapter mediaAdapter; 
           @Override 
           public void play(String audioType, String fileName) { 
                      //inbuilt support to play mp3 music files 
                     if(audioType.equalsIgnoreCase("mp3")){ 
                                System.out.println("Playing mp3 file. Name: "+ fileName); 
                     
                      //mediaAdapter is providing support to play other file formats else
                     if(audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")){ 
                                mediaAdapter = new MediaAdapter(audioType); 
                                 mediaAdapter.play(audioType, fileName); 
                     } else{ 
                                System.out.println("Invalid media. "+ audioType + " format not supported"); 
                     
           }
}

Step 5 
Use the AudioPlayer to play different types of audio formats.
AdapterPatternDemo.java  
public class AdapterPatternDemo { 
           public static void main(String[] args) { 
                      AudioPlayer audioPlayer = new AudioPlayer(); 
                      audioPlayer.play("mp3", "beyond the horizon.mp3"); 
                      audioPlayer.play("mp4", "alone.mp4"); 
                      audioPlayer.play("vlc", "far far away.vlc"); 
                      audioPlayer.play("avi", "mind me.avi"); 
          
}

Step 6 
Verify the output. 
Playing mp3 file. Name: beyond the horizon.mp3 
Playing mp4 file. Name: alone.mp4 
Playing vlc file. Name: far far away.vlc 
Invalid media. avi format not supported