Wednesday, January 30, 2013

What else missing in java NIO

Java NIO2 in java 7 add important features to the NIO framework:
Finally we have good framework that handle files: http://docs.oracle.com/javase/tutorial/essential/io/fileio.html
and improvment of asynchronus chammels: http://www.ibm.com/developerworks/java/library/j-nio2-1/index.html

The reason that now the File framework is usefull is that in java NIO you are not have to support all the types of operating systems but only modern standard kind of desktop server OS. (today with the mobile revolution we can have all kind of OS not always with the same I/O features as windows and linux).
so we finally can copy files and handling directories and all kind of nice features.

Since we are no longer obligated to exotic OS i think it's about time to add interprocess communication in java NIO. so two processes in the same computer will be able to communicate without sockets.

why it is so important? today when we are going to the cloud and computer networks is very easy why spend time and add inter process communication? well i think it's important from the following reasons:
1. don't catch port for inter-process communication - let's say i have a multi process application on the same server - since java has GC sometimes for preformance issue you wish to seperate the application to several processes but still it can run on the same server. in this case if i can avoid catching port is for the best, becouse many time you need to handle issue like port is already taken by another application.

Second, we can add support of share memory that can be very useful in java applications. i know today you can use terracotta for that issue but it's much more complicated (you need teracotta server) here you get your service from the OS and it really simplify your application.

Third, security point of view, socket is a security risk if you can avoid it you reduce the security risk in your application. In addition, it can simplify integration with other languages like .NET since if we will get share memory with other languages communication can be easier.

public static void main(String[] args) throws IOException {
  Descriptor d = Descriptor.create("message");
  ProcessChannel proc = ProcessChannel.open(d);
  ByteBuffer buf = ByteBuffer.allocate(48);
  buf.clear();
  proc.receive(buf);
  ShareMemory s = ShareMemory.create(Descriptor.create("memory"),1000);
  OutputStream o = s.createOutputStream();
  ObjectOutputStream obj = new ObjectOutputStream(o);
  obj.writeObject(new Temp());
 }
So i really hope so java NIO will include such feature, what do you think?

Thursday, January 24, 2013

Back to SELA and intersting issue

Hi all,
It's a long time since i posted something in my blog.
 but now after coming back to SELA i decided to renew my blog and make it much better.
I really wish to make my blog a place where programmers can find deep answers and understand better the all issue of programming.

When I browsing the internet looking for good topic to my blog I found the following question in stack overflow: http://stackoverflow.com/questions/2489701/value-object-getter?rq=1

First it looks like non important issue, however, I think this question is much more important than it looks like , I saw a lot of developers using such utilities methods or choosing the second approach and adding more methods. this issue is important since it touches the core of object oriented programming. both of the options are not good object oriented design.
the first approach creates utility methods that are not really belong to any type and it's really a procedural programming.
The second approach let's the amount of money deal also with the interpretation of the amount to specific type of coins.
The issue become more serious if you wish to support different types of coins (euro, yen, yuan...)
than you can see that both of the approaches are not so good.
Here is my object oriented solution for this issue, the UML should look like this:


1. Create abstract parent class called money with amount and factor, each type of coin will be a subclass of class money and only money will have methods of equals, hashCode and compareTo.
each subclass can implement the toString method for coin presentation. than you can write factory for it to create the right coin that also read the appropriate factor of the coin.
Now let's look how the code should look like:
public abstract class Money implement Comparable{
private int amount;
private int factor;

public Money(int a, int f){
   amount = a;
   factor = f;
}

public boolean equals(Money m){
 return (amount * factor == m.amount * m.factor);
}

public int hashCode(){
  return amount * factor;
}

public int CompareTo(Money m){
    return amount * factor - m.amount * m.factor
}

public int getMoney(){
   return amount;
}

public int getCurrency(){
  return factor;
}

}

public class Cent extends Money{

public Cent(int amount){
  super(amount, 1);
}


public String toString(){
  return Integer.toString(amount) + "C";
}

}

public class Dollar extends Money{

    public Dollar (int amount){
        // here you can also calculate the currency 
        super(amount,100);
    }

    public String toString(){
      return Integer.toString(amount) + "$"; 
    }
}