Pages

Tuesday, November 15, 2011

Servant (Design Pattern) in Java - example

The servant design pattern - or better idiom is used to provide the functionality (methods) to some group of objects. This functionality is common for all these object and therefor should not be repeated in every of these classes.
The object, which should be served is passed to the method of servant as a parameter. All the served objects should implement common interface - in this particular example IMovable interface. Also the type of argument passed to the servand method is of type IMovable.
The servant in this example is used to move objects from one position to another. In real life application these methods should change the position of object in small steps so that the final change would look like smooth movement (animation). In my servant method, only some message are printed instead for demonstration.
IMovable interface:
package com.shimon.servant;

import java.awt.Point;

/**
 * Movable interface
 * @author shimon
 *
 */
public interface IMovable {
 
 public void setPosition(Point p);
 
 public Point getPosition();
  
}
Implementation (Triangle.java):
package com.shimon.servant.objects;

import java.awt.Point;

import com.shimon.servant.IMovable;

public class Triangle implements IMovable {

 private int sideA;
 private int sideB;
 private int sideC;
 
 
 Point position = null;
 
 public Triangle(int sideA,int sideB,int sideC,Point p){
  this.sideA = sideA;
  this.sideB = sideB;
  this.sideC = sideC;
  this.position = p;
 }
 
 @Override
 public void setPosition(Point p) {
  this.position = p;
 }

 @Override
 public Point getPosition() {
  return this.position;
 }

 public int getSideA() {
  return sideA;
 }

 public void setSideA(int sideA) {
  this.sideA = sideA;
 }

 public int getSideB() {
  return sideB;
 }

 public void setSideB(int sideB) {
  this.sideB = sideB;
 }

 public int getSideC() {
  return sideC;
 }

 public void setSideC(int sideC) {
  this.sideC = sideC;
 }

}
Servant class (Mover.java)
package com.shimon.servant;

import java.awt.Point;

/**
 * Mover servant - moves the provided IMovable objects to specified position or increases their x an y axis position 
 * using the provided arguments.
 * @author shimon
 *
 */
public class Mover {
 
 /**
  * Private constructor
  */
 private Mover(){};
 
 /**
  * Move movable object to specified position.
  * @param moved - moved object
  * @param position - final position
  */
 public static void moveTo(IMovable moved, Point position){
  Point previousPosition = moved.getPosition();
  System.out.printf("Moving smoothly from position x=%d,y=%d to position x=%d,y=%d \n",
    previousPosition.x,
    previousPosition.y,
    position.x,
    position.y);
  moved.setPosition(position);
 }
 
 /**
  * Move movable object by specified distances.
  * @param moved - object to be moved
  * @param x - difference in x-axis
  * @param y - difference in y-axis
  */
 public static void moveBy(IMovable moved, int x, int y){
  Point previousPosition = moved.getPosition();
  System.out.printf("Moving smoothly from position x=%d,y=%d to position x=%d,y=%d \n",
    previousPosition.x,
    previousPosition.y,
    previousPosition.x + x,
    previousPosition.y + y);
  moved.setPosition(new Point(previousPosition.x + x, previousPosition.y + y));
 }
}
The servant method call is showed in the JUnit test method.
package com.shimon.servant;

import static org.junit.Assert.*;

import java.awt.Point;

import org.junit.Test;

import com.shimon.servant.objects.Rectangle;
import com.shimon.servant.objects.Square;
import com.shimon.servant.objects.Triangle;

public class MoverTest {

 @Test
 public void test() {
  Triangle triangle = new Triangle(1, 3, 2, new Point(-1,0));
  Square square = new Square(2, 4, new Point(2,5));
  Rectangle rec = new Rectangle(4, 3, new Point(-3,6));
  Mover.moveTo(rec, new Point(3,2));
  //assert equals on new position of rectangle object
  assertEquals(new Point(3,2), rec.getPosition());
  Mover.moveBy(square, 2, 4);
  assertEquals(new Point(4,9), square.getPosition());
  Mover.moveTo(triangle, new Point(-4,0));
  assertEquals(new Point(-4,0), triangle.getPosition());
 }

}
Console output:
Moving smoothly from position x=-3,y=6 to position x=3,y=2 
Moving smoothly from position x=2,y=5 to position x=4,y=9 
Moving smoothly from position x=-1,y=0 to position x=-4,y=0 

Java Crate (design pattern/idiom) example

Another example, based on example explained in the book "Navrhove vzory" (Design patterns) from Rudolf Pecionvsky. I have re-made this example just to somehow get more familiar with this design pattern (or better idiom). The "crate" is used to store the set/list of object in one place, so that the moving (passing) these objects is easier.

The example from the book is very easy, and helps to understand, how this design pattern could be applied to som very usefull application (e.g. day planner)

Code example:
 
package com.sim.crate.common;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * The Day Plan class demonstrates the usage of crate to create simple day plan, with items that do not collide.
 * @author shimon
 *
 */
public class DayPlan {
 private final List actions = new ArrayList();
 
 /**
  * Tries to add an item to the day plan with entered start, end time and duration. Returns true, if
  * the try was successful, or false if not. The item is added only if no collision with other items in 
  * the day plan appear.
  * @param hour - start hour
  * @param minute - start minute
  * @param duration - duration
  * @param subject - item subject
  * @return - bollean true - if success, else false
  */
 public boolean add(int hour, int minute, int duration, String subject){
  int start = hour * 60 + minute;
  int end = start + duration;
  ListIterator lip = actions.listIterator();
  while(lip.hasNext()){
   Item i = lip.next();
   if (i.end <= start){
    continue;
   }
   if(i.start <= end){
    return false;
   }
  }
  lip.add(new Item(start, end, subject));
  return true;
 }
 
 /**
  * Prints all items available in the dayplan.
  */
 public void print(){
  System.out.println("\nList of actions:");
  for (Item i:actions){
   System.out.printf("%02d:%02d - %02d:%02d %s\n", i.start / 60,i.start % 60,i.end / 60,i.end % 60,i.subject);
  }
  System.out.println("--------------------------------");
 }
 
 /**
  * Inner class used to store the items.
  * @author shimon
  *
  */
 private static class Item{
  int start;
  int end;
  String subject;
  
  Item(int start, int end, String subject){
   this.start = start;
   this.end = end;
   this.subject = subject;
  }
 }
 
 public void testAdd(int h, int m, int d, String txt){
  boolean success;
  System.out.printf("Adding %d minutes from %02d:%02d for %s\n", d, h, m, txt);
  success = add(h, m, d, txt);
  System.out.println("             " + (success?"OK":"FAIL"));
 }
}

Test class:
 
package com.sim.crate.common;


import org.junit.Test;

public class DayPlanTest {

 @Test
 public void test() {
  DayPlan dp = new DayPlan();
  dp.testAdd(8, 0, 30, "Waking up");
  dp.testAdd(10, 30, 90, "Relax");
  dp.testAdd(8, 30, 30, "Breakfast");
  dp.testAdd(9, 30, 90, "Work");
  dp.print();
 }
}


Friday, November 4, 2011

Immutable object example

After reading some chapters of  the book "Design patterns" (Navrhove vzory) from czech author Rudolf Pecinovsky, I decided to test my own examples of usage of the design patterns mentioned in this book. In this first post I will publish here a code that is an example of usage of the immutable objects.

As you maybe already know - the immutable objects cannot be changed after their creation. That can be very useful in some situation. In this particular example (which is kind of a english copy of the czech example mentioned in the book "Navrhove vzory") the object, which represents fraction and its operations is created. The main thing here is, that during every operation (method) new instance of class Fraction is created instead of modifying the existing Fraction instance.

Immutable objects can be very useful in concurrent applications - their state cannot be changed, so there is no way they could be corrupted by other threads.

Code example:

 
package com.shimon.immutable;

import com.shimon.immutable.common.Functions;

/**
 * This class defines fractions and needed operations with fractions - addition, subtraction, multiplication and division.
 * @author shimon
 *
 */
public class Fraction extends Number{
 /**
  * 
  */
 private static final long serialVersionUID = 2522259263216154175L;
 private final int numerator;
 private final int denominator;
 
 /**
  * Creates new instance of class Fraction with numerator and denominator passed as parameters.
  * @param n - numerator of new fraction
  * @param d - denominator of new fraction
  */
 public Fraction (int n, int d){
  if (d == 0){
   throw new IllegalArgumentException("Denominator cannot be zero.");
  }
  int divisor = Functions.gcd(n, d);
  if (d < 0){
   n = -n;
   d = -d;
  }
  
  numerator = n / divisor;
  denominator = d / divisor;
 }
 
 public Fraction(Fraction f){
  numerator = f.numerator;
  denominator = f.denominator;
 }
 
 public Fraction(int number){
  numerator = number;
  denominator = 1;
 }
 
 /**
  * Adds a fraction passed as parameter to the Fraction instance.
  * @param f- fraction to add
  * @return
  */
 public Fraction plus(Fraction f){
  return new Fraction(numerator * f.denominator + f.numerator * denominator, denominator * f.denominator);
 }
 
 public Fraction plus (int number){
  return new Fraction(numerator + number * denominator, denominator);
 }
 
 /**
  * Substracts fraction passed as parameter from the Fraction instance
  * @param f - fraction to substract
  * @return
  */
 public Fraction minus(Fraction f){
  return new Fraction(numerator * f.denominator - f.numerator * denominator, denominator * f.denominator);
 }
 
 public Fraction minus(int number){
  return new Fraction(numerator - number * denominator, denominator);
 }
 
 /**
  * Subtracts the fraction from the number, which is passed to the method as a parameter.
  * @param number - number to subtract from
  * @return
  */
 public Fraction subtractFrom(int number){
  return new Fraction(number * denominator - numerator, denominator);
 }
 
 /**
  * Multiplies the fraction with fraction passed as parameter
  * @param f - fraction to multiply with
  * @return
  */
 public Fraction multiply(Fraction f){
  return new Fraction(numerator * f.numerator,denominator * f.denominator);
 }
 
 public Fraction multiply(int number){
  return new Fraction(number * numerator, denominator);
 }
 
 /**
  * Divides the fraction with the fraction passed as a parameter
  * @param f - fraction to divide by
  * @return
  */
 public Fraction devide(Fraction f){
  return new Fraction(numerator * f.denominator, denominator * f.numerator);
 }
 
 public Fraction devide(int number){
  return new Fraction (numerator, denominator * number);
 }
 
 /**
  * Divides the number with a fraction
  * @param number - number to be divided
  * @return
  */
 public Fraction devideNumber(int number){
  return new Fraction(denominator * number, numerator);
 }
 
 @Override
 public int intValue() {
  return numerator / denominator;
 }

 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + denominator;
  result = prime * result + numerator;
  return result;
 }

 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Fraction other = (Fraction) obj;
  if (denominator != other.denominator)
   return false;
  if (numerator != other.numerator)
   return false;
  return true;
 }

 @Override
 public long longValue() {
  return (long) numerator / denominator;
 }

 @Override
 public float floatValue() {
  return (float) numerator / denominator;
 }

 @Override
 public double doubleValue() {
  return (double) numerator / denominator;
 }
 
 public int getNumerator() {
  return numerator;
 }

 public int getDenominator() {
  return denominator;
 }

}

}
}
Functions class:
 
package com.shimon.immutable.common;

/**
 * This class is a library class, its methods solve the calculation of greatest common divisor and least common multiple.
 * @author shimon
 *
 */
public final class Functions {
 
 /**
  * Private constructor.
  */
 private Functions(){}
 
 /**
  * Returns greatest common divisor of two numbers.
  * @param i1 - First number
  * @param i2 - Second number
  * @return - gcd
  */
 public static int gcd(int i1, int i2){
  i1 = Math.abs(i1);
  i2 = Math.abs(i2);
  
  while (i2 > 0){
   int pom = i1 % i2;
   i1 = i2;
   i2 = pom;
  }
  return i1;
 }
 
 /**
  * Least common multiple of two numbers.
  * @param i1 - First number
  * @param i2 - Second number
  * @return lcm
  */
 public static int lcm(int i1, int i2){
  if ((i1 == 0) || (i2 == 0))
   return 0;
  return i2 * Math.abs(i1) / gcd(i1,i2);
 }
}
Test class:
 
package com.shimon.immutable;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class FractionTest {
 
 private Fraction f;

 @Before
 public void setUp() throws Exception {
  f = new Fraction(3, 5);
 }
 
 @Test
 public void testPlus(){
  Fraction result = f.plus(new Fraction(1,2));
  assertEquals(new Fraction(11,10), result);
 }
 
 @Test
 public void testMinus(){
  Fraction result = f.minus(new Fraction(2,10));
  assertEquals(new Fraction(2,5), result);
 }
 
 @Test
 public void testDivision(){
  Fraction result = f.devide(new Fraction(6,4));
  assertEquals(new Fraction(6,15), result);
 }
 
 @Test
 public void testMutliplication(){
  Fraction result = f.multiply(new Fraction(5,6));
  assertEquals(new Fraction(1,2), result);
 }

}