Monday, December 31, 2012

The idea behind this post to create a minimalist Client/Server application that can be run within a network. There are many valuable posts about the same topic, but most of them tend to be long and it is hard to get the actual meat in a rather shorter time.

Let's first briefly go over some network programming buzzwords:

Port: Type of software constructor which is used as a communication endpoint at Server-Side. Ports are either process-specific or application-specific and are mainly used in Transport Layer protocols such as TCP/IP.

Socket : one end-point of a two-way communication link between two programs running on the network. A socket is bound to a specific port number.

Single-Threaded Server:

In the below code, what you need to do is to simply Create a ServerSocket object with the port number. Afterwards, the server instance listens on a port for a connection request from a client and accepts it right after that. Notice that the reason it is a single-thread server processor is that if two clients request for a connection at the same-time, the second client will not be processed unless the server is done with the first client's request.

As I said earlier, the single-threaded server is not gonna work is almost all cases. With all of computers having dual/triple core servers, a programmer needs to be able to use that opportunity to deliver faster solutions. Java makes concurrent programming really easy by the abstraction layers it has created inside the language libraries.

Let us start with the simplest situation possible. In the multi-threaded case, server will listen to a client request and as soon as it gets the request, starts a Thread and process it. For that, we need to replace the following lines:

while (true)
{
new MultiServerThread(serverSocket.accept()).start();
}

In here, the MultiServerThread class will handle most of the Server's work. This class will inherent Thread as its super class. As you may know, while implementing Thread, you will do most of your work in the run method as below:

As you can see in the code, you can instantiate Bufferedreader to read the Client's request and process that later. Also, you need to instantiate PrintWriter object with the OutputStream of the Socket to write the message back to client. Note that you should close the Streams as it can exploit your program to a Memory leak.

Friday, December 21, 2012

I was searching the other day for this pattern and while there are many websites referring to the pattern, they mostly fall into long discussions of this pattern. What I will do is that I make it as brief as possible.
We all know that our desire for coding is to have full flexibility to change components without re-writing many parts of code. So in any service-oriented scenario, what we mainly trying to do is to keep the client "as dumb as possible".
So let us cut right to it.

The minimum requirement for Abstract Factory Pattern is to have a client class, one interface and (at least) two classes that implement that interface. This means that those two have their methods in common. If that is not the case for you, you are looking at the wrong pattern. Abstract Factory Pattern needs to be applied to help you instantiate a group of similar classes. Like in our example, we have IT consultant and Software Developer classes:

Before going to the last step, I just want to point that sometimes even thinking about this pattern, gives me the idea that some of my classes can really implement a single-interface. So thinking about design patterns make your mind up.
Now, if the client wants to instantiate an instance of, for example ITConsultant class, you can simply do this:

Why bad practice? Because your client code is hard-coded with this class and changes make you to re-write this. Now is the time for Abstract Factory Pattern! This is an example of a factory class:

class EmployeeFactory {
public static EmployeeInterface createEmployee(final String name, final String seed )
{
if (seed.equals("SWD"))
return new SoftwareDeveloper(name);
else if (seed.equals("ITC"))
return new ITConsultant(name);
else
throw new IllegalArgumentException("Your seed is not compatible to any of classes");
}
}

Notice that the method of the factory should be static. Also we pass name and a seed to the factory so that we can decide which type of instance to return. The name parameter will be passed as name for the constructor of SoftwareDeveloper and ITConsultant classes.
Now the last part of the work is to create our dumbo client. Client does not know anything about the details and gets decoupled: