Pages

Monday, July 9, 2018

How to build angular application using Angular CLI

Following are the steps for creating and buliding application using angular-CLI

Step 1:-

Install nodeJS.

From here: https://nodejs.org/en/download/

Step 2:-

Exexute and install angular-CLI command as mentioned

npm install -g @angular/cli
npm i @angular/cli@1.4.10 -g 

steps 3:-

Install node module wth mentioned command

npm install

After successfully installation we can check angular configuration on our system with follwoing command.

ng -version

It will return following output





















(Note: The version may be different based on when you install this CLI. This command returns CLI version.)

Common problems:-


If you run into any installation issue, try to update Node, NPM, and CLI to the latest version.
If downloading files is taking forever for Windows user, try running the command line as administrator.
Thank you.

Tuesday, June 19, 2018

Structural Patterns - Adapter 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.

Adapter Pattern UML Diagram



Step 1

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

Step 2

Create concrete classes implementing the AdvancedMediaPlayer interface.
VlcPlayer.cs
public class VlcPlayer implements AdvancedMediaPlayer{
   @Override
   public void playVlc(String fileName) {
      Console.Write("Playing vlc file. Name: "+ fileName);  
   }

   @Override
   public void playMp4(String fileName) {
      //do nothing
   }
}
Mp4Player.cs
public class Mp4Player implements AdvancedMediaPlayer{

   @Override
   public void playVlc(String fileName) {
      //do nothing
   }

   @Override
   public void playMp4(String fileName) {
      Console.Write("Playing mp4 file. Name: "+ fileName);  
   }
}

Step 3

Create adapter class implementing the MediaPlayer interface.
MediaAdapter.cs
public class MediaAdapter implements MediaPlayer {

   AdvancedMediaPlayer advancedMusicPlayer;

   public MediaAdapter(String audioType){
   
      if(audioType.contains("vlc") ){
         advancedMusicPlayer = new VlcPlayer();   
         
      }else if (audioType.contains("mp4")){
         advancedMusicPlayer = new Mp4Player();
      } 
   }

   @Override
   public void play(String audioType, String fileName) {
   
      if(audioType.contains("vlc")){
         advancedMusicPlayer.playVlc(fileName);
      }
      else if(audioType.contains("mp4")){
         advancedMusicPlayer.playMp4(fileName);
      }
   }
}

Step 4

Create concrete class implementing the MediaPlayer interface.
AudioPlayer.cs
public class AudioPlayer implements MediaPlayer {
   MediaAdapter mediaAdapter; 

   @Override
   public void play(String audioType, String fileName) {  

      //inbuilt support to play mp3 music files
      if(audioType.contains("mp3")){
         System.out.println("Playing mp3 file. Name: " + fileName);   
      } 
      
      //mediaAdapter is providing support to play other file formats
      else if(audioType.contains("vlc") || audioType.contains("mp4")){
         mediaAdapter = new MediaAdapter(audioType);
         mediaAdapter.play(audioType, fileName);
      }
      
      else{
         Console.Write("Invalid media. " + audioType + " format not supported");
      }
   }   
}

Step 5

Use the AudioPlayer to play different types of audio formats.
Client.cs
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