Sunday, August 01, 2010
De|Bug|er
Saturday, August 29, 2009
Book Review : Refactoring by Martin Fowler
Friday, May 08, 2009
Twitter Java API
In my opinion, after Apple's iPhone, iPod and Amazon's Kindle, Twitter is greatest innovation of this decade. The simple but powerful innovation allows us to share our thoughts, news, and opinion to 100K plus or even 1 million users within a single click. Amazing doesn't it? How will you share your opinion transparently within 140 chars? The twitter users also has amazing power, people sharing from all over globe by using a single entity is something powerful medium.
I was drawn into Twitter world like a year ago, so far my experience with Twitter is excellent. But every things has it is own drawback, in twitter world there are some a-holes try to deceive us by following us and make us to follow them and then silently unfollow us. I want to develop a small piece of code to find this mischief. But i found an excellent site www.refollow.com to do the same thing efficiently, i recommend all to use the refollow.com to find this rogues. But anyways i did this code to understand various APIs for Twitter. One of the great and fastest Java API for Twitter is from Google but there is no good documentation. Here is step by step guide to integrate with Google java-twitter.
1. Go to http://code.google.com/p/java-twitter/
2. Download Java-Twitter-*-SNAPSHOT.jar. Java-Twitter-*-SNAPSHOT.jar depends on following JARS
commons-beanutils-1.6
commons-codec-1.2
commons-collections-3.1
commons-httpclient
commons-lang-2.4
commons-logging-1.1.1
ezmorph-1.0.6
google-collect-1.0-rc1
httpcore-4.0
httpcore-nio-4.0
joda-time-1.6
json-lib-2.2.3-jdk15
3. Here is the complicated Google's Protocol Buffers. Google uses Protocol Buffers for almost all of its internal RPC protocols and file formats. Our job is to download bunch of C++ code and convert it to Java and jar it.
4. Download protobuf*.zip and protobuf*win32.zip.
5. Unzip protobuf*.zip and proto*win32.zip, and find a proto.exe from protobuf*win32.zip.
6. Copy the file to protobuf/src/ folder.
7. Go to protobuf/java and find README.txt for step by step guide to create protobuf-java-2.0.3.jar file.
8. After adding all the above JAR files, the code development is easy and refer http://java-twitter.googlecode.com/svn/site/apidocs/index.html
9. Make sure to install Java 1.6 and set JAVA_HOME variable correctly.
Here is code to find friendship exists call.
Sunday, June 08, 2008
Stereotypes - Architecture point of view
As per architecture point of view, stereotypes and assumptions are really important to understand/build a new system but with in human systems based on stereotypes and assumptions "forcing" is wrong. My opinion is, a little stereotype, common sense and general knowledge is really required to understand a stranger and to build a good relationship or persuade others. The 3 combination of stereotype, common sense and general knowledge are success weapon to deal with most diversified cultures like india.
For example, couple of weeks ago my wife with couple of other housewives planned to contact a get together. One is from east india, other one is from west india(we haven't meet them earlier). My wife came from prominently beef eating state based on that she suggested a beef dish. She lacked to apply stereotype, common sense and general knowledge here, because i knew western india states banned beef slaughtering , and most of them are sensitive to hindu culture in which eating beef is sin and a simple stereotype that their last name belongs to veg. only community. I was upset with her and i felt some uneasy when we went there for dinner ofcourse with pure veg. dishes.
Tuesday, May 27, 2008
Decrator Pattern
The classic example of decorator pattern is Java I/O package. We used to create a powerful reader by chaining file reader and buffered reader. So file reader got decorated by buffer reader at run time with out extending it.
BufferedReader buff = new BufferedReader(new FileReader("something.txt"));
Again back to our traditional Car example. Assume, Car came out of factory and it is ready for drive but not quite ready for fully operational. Because now Car directly going to a dealer and he/she needs to add their own accessories and some more accessories depends on customer choice. If we think about an object model here, we can clearly understand the decorator.
So Car manufacture has to create an interface ( assume an abstract class ) to satisfy both dealer specific accessories as well as customer specific ones, with out changing or extending car.Now dealer implementing all accessories by extending manufacture interface (abstract). Now dealer decided to create one more abstract class by extending manufacture interface, to give more choice to customers for highly customize the car.
//Manufacture abstract class
public abstract class Accessories {
public void addMoonRoof();
public void fixNumberPlate();
}
//Dealer concrete class
public class DealerAccessories extends Accessories {
public void addMoonRoof() { //add a basic moon roof }
public void fixNumberPlate(); { //fix number plate }
}
//Dealer abstract class for high customization
//allowing customer to choose what moon roof they want, panoramic, 3D etc
public abstract class AccessoriesDecorator extends Accessories {
public abstract void addMoonRoof();
public void fixNumberPlate(); { //fix number plate }
}
//Panoramic view moon roof
public class Panoramic extends AccessoriesDecorator {
public void addMoonRoof() { //add a panoramic moon roof }
public void fixNumberPlate(); { //fix number plate }
}
Now we have all in place, just we want to add it in car. Assume, car has one method called add Accessories(Accessories), here we can add any thing Panoramic or basic Dealer Accessories.
Thursday, May 15, 2008
Factory Pattern (Contd.)
To achieve the above problem statement, first thing we do is convert CarBuilder class to an abstract class, we no longer use simple parametrize create car factory, we need something robust, multiple factories depends on countries.
Lets think about country based factories like India, US, Japan and so on. So definitely we need a IndiaCarFactory, USCarFactory, JapanCarFactory. Now what we have as a common with in these factories, one thing is assemble,sendToPaint,delivery so does it make sense if we implement all these methods in super class and let sub class implement createCar method and let sub class decide which car object to instantiate depends on country. That's our factory model. Here is real implementation.
public abstract class CarBuilder {
public void assemble(Car car) {
//a concrete shared assemble implementation goes here.
}
public void sendToPaint(Car car) {
//a concrete shared send to paint implementation goes here.
}
public void delivery(Car car) {
//a concrete shared delivery implementation goes here.
}
//this is going to differ based on country
abstract Car createCar(String name);
public void buildCar(String name)
{
Car car = this.createCar(name);
assemble(car);
sendToPaint(car);
delivery(car);
}
}
Here is factory implementation.
public class IndiaCarFactory extends CarBuilder {
Car createCar(String name) {
Car car = null;
if(name.equals("ford") {car = new IndiaFord(); }
if(name.equals("honda") { car = new IndiaHonda(); }
if(name.equals("chev") { car = new IndiaChev(); }
if(name.equals("toyota") { car = new IndiaToyota(); }
return car;
}
}
The same can be implement in USCarFactory and JapanCarFactory etc. So when we want to these from caller, all we need to do is
CarBuilder builder = new IndiaCarFactory();
builder.buildCar("honda");
that's it, now we have a car builder from India factory for Honda.
Factory Pattern
Factory Pattern
The definition of factory pattern in simple "encapsulate object creation" and from Gang of 4 "Define an interface for creating an object but let subclass to decide which class to instantiate".
Here is one example, we have a concrete CarBuilder class implementation and a method buildCar as follows, this class has other important methods to build a car also.
public class CarBuilder {
public void buildCar(String name) {
Car car = null;
if(name.equals("ford") { car = new Ford(); }
if(name.equals("honda") { car = new Honda(); }
if(name.equals("chev") { car = new Chev(); }
assemble(car);
sendToPaint(car);
delivery(car);
}
}
Okay, all are fine, our class is ready to support 3 types car creation and supporting methods. After some days, now we want to add Toyota into this mix, so we need to add one more condition in buildCar method, and after some time we need to add 3 more types and adding 3 more if conditions in code, looks bad as well as we are leading to ambiguous class. So now we have to remove all if conditions to some class like utility class and pass name from here to get correct class implementation. Removing all if condition from here to other class is a simple factory pattern, that is, encapsulate object creation.
public class SimpleCarFactory {
public static Car createCar(Sting name) {
Car car = null;
if(name.equals("ford") {car = new Ford(); }
if(name.equals("honda") { car = new Honda(); }
if(name.equals("chev") { car = new Chev(); }
if(name.equals("toyota") { car = new Toyota(); }
return car;
}
}
createCar method declared static so we can call this method without instantiate SimpleCarFactory class. We haven't really into real factory pattern yet. This is just a starting point.
Tuesday, April 01, 2008
Unified Modeling Language - Sequence Diagrams
The simple definition of sequence diagram is, explore the logic of complex function and how many objects required and how they interacts with each other to satisfy an use case. Not all use case required a sequence diagram, we can identify complex or most critical ones to be the candidate for sequence diagram.
Lets take a simple use case "add a product in shopping cart".
1. User enter user name and password in login screen.
2. System validates user and let user in the main page.
3. User select a product from product menu.
4. System adds the product into user's shopping cart.
5. User clicks checkout link.
6. System displays checkout page.
Based on above simple use case, we can draw a simple sequence like this
User -> Login - > main page -> Product catalog -> check out
Now we need to convert this simple sequence into objects
aLogin -> aProductCatalog -> aCheckOut -> aBusinessService
After second iteration, we identified that we need a business service to get products from a data base. After second iteration we have to put some more stress on aBusinessService, how it is going to get data, which pattern we are going to use to get data, how we are going to transfer the data etc.
aLogin - > aProductCatalog -> aCheckOut -> aLookUp -> aProductDTO -> aProductDAO
Ok. Now we have some decent sequence, lookup service going to get DAO reference and return to aProductCatalog and aProductCatalog executes getProducts method on aProductDAO, in return aProductDTO as value object. We have a good explained sequence here at high level.
DAO = Data Access Objects
DTO = Data transfer Objects
So we are moving towards Sun's J2EE design pattern here, hence it is good add a business delegate in front of aLookUp. Like wise, we can improve our design and apply design pattern after each iteration until we satisfy our design.
Here is excellent UML and applying design patterns book by Craig Larmen
Sunday, March 23, 2008
Unified Modeling Language(UML) - Class Diagrams.
UML is not a strict language, in other words, there is no compiler out there to check our code against standards. A rough sketch can do the purpose. UML is useful on both forward and reverse engineering. The main component of UML is class diagrams, there is almost none design document without class diagrams. In class diagrams, the following notations and relationships are really important. This class diagrams are going to be a blue print for long time, hence this notations are important to communicate because we don't know whether designer who developed this diagram going to be with company or not.
1. Associations
In Class diagrams, if a class associate with other class, the a solid line with a arrow, from source to target. A perfect example would be, Customer and Order class. Order class must give a customer id, who created order. Since we can identify an unique customer id from order, Order class has an association with Customer class, that same with customer class also, from customer class we can find orders by the customers.
2. Compositions
A class can have other class's reference in it. This is useful when we want to utilize other class's methods without inheritance. Decorator pattern is mainly based on compositions.
3. Generalization
This is for grouping similar classes, to add these similar methods in a super class and all other classes are derived(inheritance) from it. This is nothing but super class and sub class notation in UML.
4. Aggregation
Aggregation and association are interchangeable. There is no major difference between them. A car has an engine, then car (whole) has a aggregation relationship with engine (part). To make our life simple, we can use association notation instead of aggregation. Most of the UML gurus discourage us to use aggregation notation in class diagram. We can just use association for top level design and composition and aggregation can be used in detailed design.
5. Realization
Realization is relationship between an interface and implements class. The interface specifies behaviours and a class implements that behaviour then the class realize relationship with interface.
6. Dependency
Dependency relationship as name implies, if one class change, other class's state changes then these two classes are dependency relationship. MVC pattern can be good example of dependency relationship.
The other important elements from class diagrams are multiplicity (upper bound..lower bound), Stereotype, visibilities and properties.
From Generative AI to Agentic AI: 2 Weeks of Building MyNewsAI.io
From Generative to Agentic AI: 2 Weeks Building MyNewsAI.io Over the last two weeks, I set out to evolve MyNewsAI...
-
Already Microsoft getting heat from Google, that Google is capturing all critical functionalities from Microsoft office suite and developed ...
-
When I was young, i used to listen old stories from grandmother and a cousin of my grandfather. Most of the stories are from their flashback...
-
When I start thinking about future, my mind exploding without control, particularly current events and state of our economy. There is no d...