Showing posts with label Behavioral Design Pattern. Show all posts
Showing posts with label Behavioral Design Pattern. Show all posts

Thursday, June 2, 2016

How to check a Linked-List is a Palindrome.

Solution Approach:

First thing is we should know about palindrome.

What is Palindrome?
The list must be the same backwards and forwards such as

0-> 1 -> 2->1->0 This leads us to our first solution.

This problem can solved in many ways,

  1. Reverse and Compare
  2. Iterative Approach
  3. Recursive Approach
Among three i will first solve this problem using Iterative Approach just see the below 

Implementation:

public boolean isPalindrome(LinkedListNode head)
{
LinkedListNode fast =head;
LinkedListNode slow =head;
Stack<Interger> stack=new Stack<Interger>();
while(fast!=null && fast.next !=null)
{
 stack.push(slow.data);
 slow=slow.next;
 fast=fast.next.next;
 
}
if(fast!=null)
{
  slow =slow.next;
}
while(slow!=null)
{
int top=stack.pop().intValue();
if(top!=slow.data)
                {
   return false;
}
slow=slow.next;
}
return true;
}

Now you can try others too......

Friday, May 27, 2016

Strategy Design Pattern tutorial example java

What is Strategy Design Pattern?

Strategy pattern is used when we want different algorithms needs to be applied  on objects.This pattern where algorithms can be selected at runtime.Strategy pattern is also known as the "Policy Pattern" .

For Example:

A mode of transportation to an airport is an example of this pattern because you have multiple options  such as driving your own car,by taxi,b bus etc.

When to use Strategy Pattern:

we can apply Strategy pattern when we need different variants of an algorithm and these related classes differ only in their behavior.


Implementation:

/**
 * @author Abhinaw.Tripathi
 *
 */

interface SortInterface {
public void sort(int[] array);
}

class QuickSort implements SortInterface {

@Override
public void sort(int[] array) {
// TODO Auto-generated method stub
// do quick sort logic here
}

}

class BubbleSort implements SortInterface {

@Override
public void sort(int[] array) {
// TODO Auto-generated method stub
// do Bubble sort logic
}

}

abstract class Sorter {

private SortInterface strategy;

public void setSorter(SortInterface strtegy) {
this.strategy = strtegy;
}

public SortInterface getSorter() {
return this.strategy;
}

abstract void doSort(int[] listToSort);
}

class MySorter extends Sorter {

@Override
void doSort(int[] listToSort) {
// TODO Auto-generated method stub
getSorter().sort(listToSort);
// other things to do
}

}

public class StrategyTest {

/**
*
*/
public StrategyTest() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int[] listToBeSorted = { 18, 26, 26, 12, 127, 47, 62, 82, 3, 236, 84, 5 };
MySorter mySorter = new MySorter();
mySorter.setSorter(new BubbleSort());
mySorter.doSort(listToBeSorted);
mySorter.setSorter(new QuickSort());
mySorter.doSort(listToBeSorted);

}

}

Design Pattern - Behavioral Design Patterns tutorial example java

What is Behavioral Design Pattern?

Behavioral patterns are those pattern which are specifically concerned with communication between the objects.The interactions between the objects should be such that they are talking to each other and still loosely coupled.

Loose Coupling is the key for architecture.This pattern deals with relationships among communications using different objects.

Categories of Behavioral Design Patterns:


  • Object-Behavioral Design Pattern = This pattern uses object composition rather than inheritance.Also to perform some task that no single object can perform alone.
  • Class-Behavioral Design Pattern = This class patterns uses inheritance rather than inheritance to describe algorithms and flow of control.
There are total 11 Behavioral Design Patterns such as

  1. Chain of Responsibility Pattern
  2. Command Pattern
  3. Interpreter Pattern(Class-Behavioral Pattern) 
  4. Iterator Pattern
  5. Mediator Pattern
  6. Momento Pattern
  7. Observer Pattern
  8. State Pattern
  9. Strategy Pattern
  10. Template Method Pattern(Class-behavioral pattern)
  11. Visitor Pattern
Except these two all are Object-Behavioral Design Pattern.