Skip to main content

Posts

Showing posts from 2015

Design Pattern 3: Factory Pattern

Design Pattern 3: Factory Pattern The factory design pattern also falls under the Creational design patterns same as the Singleton pattern. But the scope is different. Ins singleton our scope was the object. Here we focus more on the class. Lets dig deep into this design pattern. What is Factory Pattern? In earlier we talked about the Singleton Pattern where a single object is created and is used throughout the life span of the program. This time its quite different. In Factory pattern what happens is that the user doesn't get to know the actual implementation of the system instead of that all the created objects are referred via an common interface. When I mentioned hiding the implementation you might get some familiar vibe. Yes the concept of polymorphism might have crossed your mind. Actually for the implementation of the Factory design pattern we are using interfaces. Other thing is you might wonder what I ment...

Design Patterns 2 : Singleton

Design Patterns 2 : Singleton  So welcome with the second post on Design patterns and this is gonna be about the Singleton Pattern. In the last post you might remember how I have mentioned the birth of the design patterns and how Developers used it for their convenience and standardization of codes. This will be the first design pattern that will be discussed.  The word Singleton means a Single person. From that itself you can take the Idea about the Singleton. If you remember the previous post (The table with the dimensions Scope and Purpose) you'll see that Singleton belongs to the Creational Design Pattern and to the scope of Objects. Let's discuss this further. The Singleton Design Pattern Singleton is one of the simplest design Patterns out there. The Singleton Design Pattern is used when once instance of an object is used throughout the entire life span of the program. A singleton design associates with a single class and only one single...

Design Patterns 1 : Introduction

Design Patterns : Introduction So its the holiday time and thought of starting with the Design patterns. In this post I'll talk about What are design patterns?, What good to us using them?, Why and when use them? and many more. So why wait? Lets start the journey to Design patterns. What are Design patterns? So over the years when programmers tried to build systems that can solve problems often they encountered problems that were difficult to overcome. So after finding a solution what they did was presenting it as an future guideline where other programmers when encountered the same problem can easily surpass that. These are what we called as Design Patterns. To see how it all started we have to go back in the past. History of Design Patterns? All these patterns buzz started in about 1977/79 when  Christopher Alexander  showed interest in using using pattern in architecture. That's right not in computer, in architectural ...

Network Programming - Part 3 (I)

Here's a quick and fun code that you can try on. We know that the port no spans from 0 - 65535. So lets loop through that range and find the active ports in the system. This is called a Port Scanner .  There are no steps to do this. Just make a loop and try to establish a connection . Implementation 01 import java.net.Socket ; public class MyPortScanner { public static void main ( String [] args ) { for ( int i = 0 ; i < 65535 ; i ++) { try { Socket ClientSoc = new Socket ( "localhost" , i ); System . out . println ( "Port " + i + " is active" ); ClientSoc . close (); } catch ( Exception e ) { System . out . println ( "Nope nothing on " + i + " port" ); } } } } The result I got was interesting. FYI this takes a longer time to finish scanning all 65535 ports. S...

Network Programming - Part 3

Last time we did make a server and connected using Telnet client. This time we are going to create a client using Java and try to connect to our earlier made simple server O key, Lets go straight to the coding part since there is no much to discuss. As creating the simple server this too has 3 steps. Opening the client  socket Create I/O stream (Read and write). Perform Communication and finally Close Socket So few things to remember from the earlier post. The server was created in the localhost and the port was 9000. Here is the code for Simple Client program.  import java.io.BufferedReader ; import java.io.InputStream ; import java.io.InputStreamReader ; import java.net.Socket ; public class MySimpleClient { public static void main ( String [] args ) { try { Socket ClientSoc = new Socket ( "localhost" , 9000 ); //Step1: Opening Client Socket InputStream is = ClientSoc . getIn...

Network Programming - Part 2

Okay, Continuing the  part 01 of this set. Let's get on with the practical side. Here I'll be using Java as the language and lets look at socket Programming. First things first some analogies What is a socket ? Socket is an end point actually. This end point need to be there for some communication to happen. If not the sender will not know where to  connect or not. Socket is the combination of the IP address and the Port no. the IP address determines the computer and the Port determines the Protocol and the application. Then what is a Port ? When an application runs on the computer a specific address is located for it. This specific address or the PORT can ether be TCP or UDP (Two Transport layer protocols). A port can have any number between 0 - 65,535. The port numbers below 1024 are most frequently used so if you are creating a custom application just use something above 1024.  TCP vs UDP TCP is for Transmission Control Prot...

Network Programming - Part 1

Starting off with Network Programming I thought of discussing basics and the environment for the Network Programming. Why Network Programming? With the advancement of Technology and the ground breaking innovations we have come to a point that most of the thing we do are inter connected with each other. This is applicable for programs as well. Most of the programs we use has two modes now a days. Offline and Online. There for we can say that Programs are Network Aware. For example: Playing Dota 2 or LoL online. real time updates in servers, real time stock update etc. Lets talk about few of the simple things that can be done using a Network Program. A Network Program can retrieve data from a remote location as well as vise versa. Other than that if we talk about it further more, it allows peer-to peer interaction which supports us in chats, messaging, video conferencing etc.Another thing is Spiders in web searchin...

This time Queues with Linked Lists

Same as the earlier posts Here's the implementation. Node Class 1 2 3 4 5 6 7 8 9 10 11 12 public class NodeClass { public int info ; public NodeClass next ; public NodeClass ( int i ){ this ( i , null ); } public NodeClass ( int x , NodeClass n ){ info = x ; next = n ; } } The implementation: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 public class QueueWithLL { protected NodeClass head , tail ; public QueueWithLL () { head = tail = null ; } public boolean isEmpty (){ return ( head == null && tail == null ); } public void add ( int x ){ if ( isEmpty ()){ head = tail = new NodeClass ( x ); } else { tail . next = new NodeClass ( x , null ); tail = tail . next ...

Back With Stacks But with Linked Lists

So keeping it all simple this is a post to present the code that implements the Stacks using Linked List.  Like in the Linked list (obviously) made to classes one for nodes and other one for implementation . Node class 1 2 3 4 5 6 7 8 9 10 11 12 public class NodeClass { public int info ; public NodeClass next ; public NodeClass ( int i ){ this ( i , null ); } public NodeClass ( int x , NodeClass n ){ info = x ; next = n ; } } Then the implementation: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class StackWithLL { protected NodeClass head ; public StackWithLL (){ head = null ; } public boolean isEmpty (){ return head == null ; } public void push ( int x ){ head = new NodeClass ( x , head ); } public int pop (){ int temp = head . info ; ...