Thursday, June 20, 2013

Factory Method Design Pattern

Factory Method Design Pattern: This creational pattern is used to instantiate an object among a set of classed based on some logic. It works on the feature of Encapsulation. Here the object creation code is not on the client side, but in the factory method.
 Suppose say, you have a set of subclasses extending a super class or implementing an interface.  Now if you want another concrete class to make a decision using a method which takes one or more arguments to decide which sub class among these set has to be instantiated, it will be a factory method design pattern.
Sounds lot confusing than it is J I did try to implement it with a problem statement in mind.
Now if you are in charge of building the framework and the developers are going to just implement the logger api which you are going to expose and if you are not sure which logger implementation(log4j or commons logging) the client might approve after couple of days, then this where factory method design patterns might help.
This is how I would design it,
//Interface  Logger
package com.dp.example.factorymethod;
public interface Logger extends Serializable{
public boolean isInfo();
public boolean isDebug();
public Boolean isWarn();
public Boolean isError();
}

// Subclass Log4J Implementation
package com.dp.example.factorymethod;
import org.apache.Log4JLogger
public class Log4JLogger implements com.dp.example.factorymethod.Logger{
private transient Logger logger;
public static Log4JLogger getInstance(String classname){
Log4JLogger log4JInstance = new Log4JLogger();
log4JInstance.logger = Logger.getLogger(classname);
return log4JInstance;

}
public boolean isInfo();
public boolean isDebug(){
System.out.println(“You have called Log4J Logger Debug”);
}
public Boolean isWarn();
public Boolean isError();
}
//Subclass Commons Logging Implementation
package com.dp.example.factorymethod;
public class CommonsLogger implements com.dp.example.factorymethod.Logger{
private transient java.util.logging.Logger logger;

public static CommonsLogger  getInstance(String classname){
CommonsLogger commonsLoggerInstance = new CommonsLogger ();
commonsLoggerInstance.logger = Logger.getLogger(classname);
return commonsLoggerInstance;
}
public boolean isInfo();
public boolean isDebug(){
System.out.println(“You have called Commons Logger Debug”);
}
public Boolean isWarn();
public Boolean isError();
}
//Factory Class – LoggerFactory
public class LoggerFactory{
public static Logger getLoggerImpl(String Classname, String LoggerType){
//you can get the loggertype from propertis file
If(loggerType.equals(“Log4J”)){
return Log4JLogger.getInstance(classname);
}
else {
return CommonsLogger.getInstance(classname);
}
}
//FactoryMethod – LoggerFactoryMethod// Client Code
public class LoggerFactoryMethod{
public static void main(String[] args){
LoggerFactory loggerFactory = new LoggerFactory();
Logger logger = loggerFactory.getLoggerImpl(“className”,”Log4J”);
Logger.isDebug();
}
Apparently, not the best example, but helped me in understanding this whole thing a little better.
Please feel free to comment incase you found this as a wrong implementation of Factory Method Design Pattern.



Monday, June 17, 2013

Why Design Patterns at all??

Okay.. So the whole of software industry preaches everyone to work on reusability. We have always been asked to develop reusable code and use it later. The most famous keys Ctrl+C and Ctrl V is designed for reusability. Now the reason why I am talking about reusability is because,  Design Patterns just do that. Here it’s not the code we resuse, it’s the solution to the most commonly occurring problem we reuse.

One thing to note is, Design Patterns cannot be directly transformed into code.  It just helps you in making a design decision. Because every problem is different in different contexts, the dogma of one solution fits all doesn’t work here. It sort of gives you a manual or a generic solution on what are best ways of approaching the problem.

Finally its all with your experience, you can make the best design decision. So we have three kinds of Design Patterns :

Creational Design Patterns - It is all about Class Instantiation.
As the name goes, it’s about the best ways to create classes and objects.  There are two types in them – Class creation patterns and Object creation patterns. For class creation, these patterns use inheritance for the instantiation while for object creation, they use delegation to get their job done.

Structural Design Patterns - It’s all about Class composition
As self-explanatory it is about structuring the objects and classes. Structural Class Patterns use inheritance to compose interfaces and Structural Object Patterns defines the way to compose objects.

Behavioral Design Patterns - It’s all about Class’s Object Communication
These patterns talk about how objects can communicate with each other.

In next couple of blogs I plan to take one design pattern at a time and make my observations.

Monday, June 10, 2013

Traditional Portal Vs Lean Portal

Like it or not, traditional portals over a period of time become very difficult to manitain and deploy. Some of the features which comes bundled in, are never used by the organizations. Lot of companies think twice before investing because firstly it exceeds their budget and secondly some of them do not deliver the results they want.

Gartner introduced the concept of Lean Portal which is a portal built on Web 2.0 technologies. It has been observed that compaines which opted for Lean Portal deliver 80% of functionalities within months without compromising security or advanced integration.

Some of the  advantages of Lean Portal are

One size doesn't fit all : Lean Portal creates a fit-for-purpose suites and doesnt go by the traditional one-size-fits-all approach.Lean portal is more agile in the sense it doesnt need to be 'finished' to be rolled out to market.

Customer Satisfaction: Since new functionalities can be added rapidly, based on the customers wants, the business can easily adapt to the ever changing requirements.

Less is more : Lean Portal does not deliver less, they deliver only what is needed.


Monday, June 3, 2013

Back Again

After a long hiatus of more than a year, i am glad i am back :-) I had thought my account would have got deactivated by now.. Thankfully not yet..

Wednesday, November 9, 2011

REST...

Finally back to the place where i really get to learn.. So this is what i learnt offlate... REST...

REST stands for Representational State Transfer. REST works on a stateless client server
cacheable communication protocol which is basically the HTTP protocol.
 
If all this while, one was wondering if SOAP or RPC were some of the only ways to connect between machines, infact REST makes simple HTTP calls between machines for communication.
 
Rest is said to be an architectural style for designing networked application.
 
 
A very good example which i read about the difference between REST and SOAP is
the difference between mailing a letter using an envelope and a postcard.
Letter being the message of communication, envelope being SOAP and postcard being REST.
 
Postcards are easier to handle by the  reciever, wastes less paper(takes
less bandwidth) and have a short content. If in between all this, if one
thinks that REST is not as secure as SOAP, then its wrong. REST and SOAP with
proper encryption are equally secure.
 
 
The differnce between SOAP request and REST style of request for getting all the details of an employee is as below.
 
 
 
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:body pb="http://www.acme.com/phonebook">
  <pb:GetUserDetails>
   <pb:UserID>12345</pb:UserID>
  </pb:GetUserDetails>
</soap:Body>
</soap:Envelope>
 
 
With REST, its as simple as this,.This URL is sent to the server using a simpler GET request,
and the HTTP reply is the raw result data -- not embedded inside anything,just the data you need in a way you can directly use
 
Now to make this more interesting, lets see what stateless means and whats the advantage of having a stateless client server protocol
 
Stateless means the server will not keep the state information of the client.
 
Considering the example of FileOpen and FileRead operations,
 
File f = FileOpen("myfile.dat");
byte[] firstK = FileRead(f, 1024);
byte[] secondK = FileRead(f, 1024);
 
So what does this do.. firstK will hold teh first 1024 bytes and secondK will hold the next 1024 bytes.This happens because the
server remembers for each open file, where is the file read head which is infact the state.
 
The same thing can be achieved using the stateless API
 
 
firstK = FileRead("myfile.dat", 0, 1024);
secondK = FileRead("myfile.dat", 1024, 1024);
 
Here the FileRead funtion accepts the filename, from where to read and till what amount it has to read.. This stateless approach looks
better because, firstly it survives with a server restart... Secondly there is no client server binding... So each request can be handled by different
server but still yield the same result..
 
The problem with stateful api clearly is, that the exact same machine has to handle all the requests or
the state must be replicated to all the servers in the cluster

Wednesday, April 27, 2011

Lotus Greenhouse

Now, this is something i came to know after attending the IBM Training..

IBM Lotus and WebSphere Portal Solution Catalog can now be found at Lotus Greenhouse i,e. http://lotus.greenhouse.com.


Users who register to it, get full and free access to all IBM products hosted on it. Users registered in Lotus Greenhouse, have full and free access to use all the products hosted in it. Currently this includes, Lotus Connections for social networking, Lotus Quickr for team based collaboration, Sametime for instant messaging and collaboration and iNotes for Mail.

 For example,members of Lotus Greenhouse can create e-meetings, share information, develop team projects, create demos, communicate through e-mail and instant messaging, and build connections and contacts with others. They can set up their own e-mail accounts and invite colleagues to join them.

Also, members are invited to give feedback on the products so are able to make their ideas known, identifying areas for improvement and requesting feature enhancements. Any reported defects are promptly investigated, and requested enhancements may become new product requirements.


Great going IBM!!

Tuesday, April 26, 2011

20 Do’s and Don’ts of Effective Web Design

Very good write-up on the do's and dont's of web design..

http://webdesignledger.com/tips/20-dos-and-donts-of-effective-web-design