Sunday, January 21, 2018

What is Factory Design Patten and its Code in java

What is Factory Desigen Pattern.also write the code?
A Factory Pattern  define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. 
In other words, subclasses are responsible to create the instance of the class.

 

Advantage of Factory Design Pattern

1-Factory Method Pattern allows the sub-classes to choose the type of objects to create.
2-It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code. That means the code interacts solely with the resultant interface or abstract class, so that it will work with any classes that implement that interface or that extends that abstract class.

 

Usage of Factory Design Pattern

1-When a class doesn't know what sub-classes will be required to create
2-When a class wants that its sub-classes specify the objects to be created.
3-When the parent classes choose the creation of objects to its sub-classes.



Ex:
Step 1->
Create an interface.
Shape.java

public interface Shape
{
   void draw();
}

Step 2->
Create concrete classes implementing the same interface.
1-Rectangle.java
public class Rectangle implements Shape
{
   @Override
   public void draw()
   {
     System.out.println("Inside Rectangle::draw() method.");
   }
}

2-Square.java
public class Square implements Shape
{
   @Override
   public void draw()
   {
      System.out.println("Inside Square::draw() method.");
   }
}

3-Circle.java
public class Circle implements Shape
{
   @Override
   public void draw()
 {
      System.out.println("Inside Circle::draw() method.");
   }
}

Step 3->
Create a Factory to generate object of concrete class based on given information.
ShapeFactory.java
public class ShapeFactory
{
           
   //use getShape method to get object of type shape
   public Shape getShape(String shapeType)
   {
      if(shapeType == null){
         return null;
      }               
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();
        
      } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();
        
      } else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }
     
      return null;
   }
}

Step 4->
Use the Factory to get object of concrete class by passing an information such as type.
FactoryPatternDemo.java
public class FactoryPatternDemo
{
  public static void main(String[] args)
 {
      ShapeFactory shapeFactory = new ShapeFactory();

      Shape shape1 = shapeFactory.getShape("CIRCLE");
              shape1.draw();

     Shape shape2 = shapeFactory.getShape("RECTANGLE");
                shape2.draw();

      Shape shape3 = shapeFactory.getShape("SQUARE");
             shape3.draw();
   } 
}

If you have any issue in understanding the above article , reach out to us leave us a comment.

Thanks for reading
Noeik

Design Pattern and Implementation of Singleton Design Pattern in java


What is Desigen  Pattern ?
A design pattern is a well-proved solution for solving the specific problem/task.  We must use the design patterns during the analysis and requirement phase of SDLC(Software Development Life Cycle).
Design patterns ease the analysis and requirement phase of SDLC by providing information based on prior hands-on experiences.
 

Categorization of design patterns:

Basically, design patterns are categorized into two parts:
1.     Core java (or JSE) Design Patterns.
2.     JEE Design Patterns.


What is  Singleton Design Pattern . also write the code?
Singleton Pattern says that just"define a class that has only one instance  
and provides a global point of access to it".
 A class must ensure that only single instance should be created and single object can be used by all other classes.

Advantage of Singleton design pattern

Saves memory because object is not created at each request. Only single instance is reused again and again.
This has advantages in memory management, and for Java, in garbage collection. Moreover, restricting the number of instances may be necessary or desirable for technological or business reasons--for example, we may only want a single instance of a pool of database connections.

Usage of Singleton design pattern

Singleton pattern is mostly used in multi-threaded and database applications. It is used in logging, caching, thread pools, configuration settings etc.

How to create Singleton design pattern?
To create the singleton class, we need to have static member of class, private constructor and static factory method.
1-Static member: It gets memory only once because of static, itcontains the instance of the Singleton class.
2-Private constructor: It will prevent to instantiate the Singleton class from outside the class.
3-Static factory method: This provides the global point of access to the Singleton object and returns the instance to the caller.
Ex:
Ex->
class Demo
{
   private static Demo obj=null;
  
   static
   {
               obj = new Demo();
   }
  
   private Demo()
   {
               System.out.println("Demo");
   }
  
   public static Demo getObject()
   {
               return obj;
   }
}

 public class Demo1 {
             public static void main(String[] args) {
                                    //Demo d1 = new Demo();
                                     Demo d2 = Demo.getObject();
                                     Demo d3 = Demo.getObject();
                                     System.out.println(d2);
                                     System.out.println(d3);
                        }
}


How can be create only 5 object of the class

     A:
    public class MSInt
    {
      private static MSInt instance = null;
      private static int count = 0;

    private MSInt()
    {
       System.out.println("MSINT");
    }

   public static MSInt getInstance()
    {
        if(count < 5){
            instance = new MSInt();
             count++;
             return instance;
        }
        else
        {
            return null;
        }
     }
     public static void main(String[] args)
     {
MSInt m1 =  MSInt.getInstance();
MSInt m2 = MSInt.getInstance();
MSInt m3 = MSInt.getInstance();
MSInt m4 = MSInt.getInstance();
MSInt m5 = MSInt.getInstance();
MSInt m6 = MSInt.getInstance();
System.out.println("m1"+m1);
System.out.println("m2"+m2);
System.out.println("m3"+m3);
System.out.println("m4"+m4);
System.out.println("m5"+m5);
System.out.println("m6"+m6);
    }

}

If you have any concern and any query , leave us comment !!

Thanks for reading
Noeik

Friday, January 19, 2018

[Program]Generic Implementation of custom linked list in java

Data structure is one of the most important in computer science . There are so many data structure are there
if you talk about most important from all the important one is Linked List.

LinkedList is a data structure where there are two block of node,one is data block and second one is for address block , the address block store the address of next node.



Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers.(source geekforgeeks )
source : wikipedia
Now let see if we want to write our own linked list using generic.


If you have any problem you can leave us a comment ,
This program is contributed by Rajendra Tighage

Thanks for Reading
Noeik

Introduction To Computer Coding (Using JAVA Programming Language) PART 1



INTRODUCTION TO COMPUTER CODING
(USING JAVA PROGRAMMING LANGUAGE)
PART 1


Welcome to this article which introduce computer coding (using java programming language).

The goal/aim of this article is to explain in details the fundamentals of Computer Coding to everyone (beginners/expertise), by using a popular and well recognized programming language (JAVA).

These articles are split into series (part). Part one which is this current article introduce us to the computer in relation to Software and Programming. The article will end with a little program in java instructing the computer to display a message after performing some task.





COMPUTER
The computer we see today was not the computer that use to be. So when trying to look for a detailed definition of the computer one might get confused with the vast definition we see, and will most time end up not knowing which one to pick as the correct one.

Mainly people often define computer with reference to different perspective.

so one could say a: Computer is an electronic device that accept inputs as data, process those inputs into information using its central processing unit(CPU) and give out the information through its output device or store the information in one of it Storage device.


This definition tries as much as possible to define computer in terms of its various operations (Inputting, Processing, storing, Output).

This is just one perspective, let's look at another definition of computer.

According to Wikipedia: A computer is a device that can be instructed to carryout arbitrary sequence of arithmetic and logical operation automatically.

This definition shows another perspective; which is: defining the computer in terms of "what it can do"

From the last definition (Wikipedia's definition) we could see that the computer is a device that follows instructions in order to do what it is suppose do.

We should Note: something here. "The computer does not just follows any instruction, the instruction that the computer follows are either arithmetical or logical in nature (in the following series of this article we will be working with both kind of instructions). So, that is to say, majority of what the computer can do are based on sequences of arithmetically and logically arranged instructions".

SOFTWARE
The computer is a device that consist of large set of components (unit). Because of the large number of components that makes up the computer, there was a need to categorize those components.  So the computer is basically made up of two components which are: (1) Hardware (2) Software

Those Mechanical and Electronical components that we can see and feel are referred to as the "Hardware components" example of such component include: the monitor, the mouse, the keyboard etc.

But for this article we are not concern about the Computer Hardware components.

From the definition of computer given by Wikipedia we were able to see that the computer is a device that is given an instructions to follow (carryout).

These instructions that the computer follows are the Software Components of the computer. These instructions given to the computer most times are called "Computer Programs".

Most computer programs (if not all) are written instructions. These instructions are written using various languages (programming language) and other tools.

There are hundreds of computer language which can be used to instruct the computer on a particular task at hand. But for this article we will be dealing with a very popular and well recognize language (JAVA). At the end of the first part we should be instructing the computer to perform some simple task for us.

PROGRAMMING OVERVIEW.
Like I mentioned earlier the goal of this article and subsequent once that I will write is to explain in details the fundamentals of computer coding.

We have seen for a fact that the computer receive instructions, and that this instructions received by the computer are written using various programming languages and other tools. These instructions are written by someone called a "Computer Programmer"; and the process of doing this is called "Computer Programming".

Well, technically speaking the process does not just involve writing instructions. There are more to the process than just writing the language.

Before a programmer begins to write instruction(s) for the computer to follow he/she has to design what he/she wants to accomplish in a pattern that the Computer CAN follow. Yes, the computer can be instructed to follow instruction(s) but we should note that the instruction we give to the computer should be formed in such a way that the computer can be able to follow it.

NOTE: One of the fundamental skills you need to learn for computer coding is for you to see a problem, figure out a solution for that problem and thereafter be able to translate that solution to sequential steps that a computer CAN follow in order to solve the problem.

After designing a solution to a problem in terms of sequence of instructions that the computer can follow then coding begins. That is, writing the sequence of instructions in a particular programming language. One thing we should note is that designing solutions to problems in a way that computer can follow is not an easy task. But there are series of approach that we can follow to get it done. I will be talking about some of these approaches in the consecutive series of these articles. And another thing that will certainly bring perfection is REGULAR PRACTICE.

COMPUTER PROGRAM
A computer program is just a sequence of instructions that the computer CAN execute to perform some task. The computer does nothing more than follow instruction.

So as a programmer your task is to make this instructions to be simple and clear as possible. The reason is that you don't want to end up confusing the computer with the instructions you write. Yes some instructions can get the computer confused.

PROGRAMMING LANGUAGE
Just like the computer we used to know (first generation) is no longer the computer we see today (fourth/fifth generation). So are the programming languages we use in giving instructions to the computer.

Computer only response/follow/execute instructions in one particular language which is called "machine language"(low level language). This language is made up of binary digit which are 1s and 0s

1100110100101110011

Giving instructions to the computer in this format is very cumbersome. Due to this fact, developing computer programs was very difficult and time consuming at a certain time. But after a while other languages was developed to boost the efficiency of software development. This languages are generally referred to as “high level language”. This languages are English like languages cause they are constructed with English words like: “do, if, for, while, public, class, static, this” etc. and also consist of punctuation like { ;(semi colon) ,(coma) .(period) } etc.

There are a lot of high level programming languages, Java is an example of one of such high level languages. And it will be my approach of introducing you to computer coding.

High level languages like java are very easy to write and learn (at least compared to machine language) when trying to give instructions to the computer. Unfortunately the computer cannot understand high level language. The computer can only understand and execute machine language.

Luckily for us there are Software called Language Translator that will come handy in regard to this issue.

One of such software is called Compile: A compiler is a software (language translator) that translate/convert a High Level Language in English like statement such as Java to a low level language (machine language in 1s and 0s).
So basically what we will do, is that, we will write instructions in a high level language like Java feed that instructions to a compiler, the compiler then turns the instructions into a machine code (1s and 0s) for the computer to execute.
OUR FIRST JAVA PROGRAM

Enough theory let's take a look at how these instructions actually look like. Note that details explanation of the following code sample will be given in the next Series.
You should not try too hard to understand it yet, this is just to give an insight of the Kind of arbitrary instructions that are given to the computer.