jump to navigation

API Design: Observer pattern in state change or transactional communication December 27, 2009

Posted by metalickl in Java, Software Insights.
Tags: , , , ,
add a comment

The challenge I’ve personally met can be boiled down to design a simple interfacing method to a web service call, for example authentication. The caveats here include network latency, response handling and possible errors(timeout, 404, IOException’s you name it). In terms of response handling, I remembered in C we had some kind of predetermined response codes (such as those found in HTTP response, 200′s, 300′s indicating success, and the 400′s/500′s indicating failure). With this design, you simply define a list of response codes (with thorough documentation), and then simply return an int that describes the response in the least verbose manner. To visualize, it becomes something like this:

// Don't do this
/** indicating success in authentication  */
public static final int AUTHENTICATION_SUCCESS = 200;
...
/** 
 *  returns response code,
 *  such as {@link AUTHENTICATION_SUCCESS}
 */
public int authenticate();

The advantage of abstraction is also its weakness. The response itself is converted to a number, where much of the information is lost(abstracted away) during the process. The user of this API would first having to read the source/javadoc comments to understand what are each of the response codes, then implement the logic to handle each case respectively. If there’s a bug inside the method, and a number that’s not a valid response is returned, the client code needs to handle that as well. Worst, if there’s declared exception (ie the familiar IOException), and the client needs to call several of these methods, one after the succession of the other, the client code would look something like this.

// Don't do this
// API methods
public int authenticate() throws IOException;
public int fetchBillingInfo() throws IOException;
public int payBill() throws IOException;
...
// Client code
public void run() {
  try {
    if (authenticate() == AUTHENTICATION_SUCCESS) {
	try {
	  if (fetchBillingInfo() == FETCHBILL_SUCCESS) {
	    try {
	      if (payBill() == PAYBILL_SUCCESS) {
		// do something
	      } else { // do somehting }
	    } catch (IOException e) { // do somehting }
           } else { // do something }
	} catch (IOException e) { // do something }
    } else { // do soemthing }
  } catch (IOException e) { // do something }
}

You see where I’m getting? Obviously no.. because the code here is unnecessarily nested, very ugly indeed. In terms of performance, try-catch blocks is also much slower to execute.

The solution to the above concerns I’ve found to be most elegant, which takes advantage of all the goodness of type safety and verboseness is to implement observer pattern. The class that wishes to access the service API first becomes registered as a service state listener. Then the API simply fires appropriate events in place of returning different responses. Look at the example below:

// API
/**
 * an enumeration of service states
 * <p>much clearer to read and understand by client
 */
public Enum ServiceState { 
  AUTHENTICATION_SUCCESS, 
  AUTHENTICATION_BAD_USRNAME_OR_PW, 
  AUTHENTICATION_PASSWORD_EXPIRED, 
  AUTHENTICATION_USER_BANNED, BILL_FETCHED, 
  BILL_UNAVAILABLE, PAYBILL_SUCCESS 
}

/** 
 * client code must implement this interface in order to access 
 * the service interfacing methods and be notified of the responses. 
 */
public interface ServiceStateListener { void serviceStateChanged(ServiceState ss); }

public void Session.authenticate();
public void Session.fetchBillingInfo();
public void Session.payBill();
...

// Client Code
public void run() {
  session.addServiceStateListener(this);
  session.authenticate();
}

public void serviceStateChanged(ServiceState ss) {
  if (ss == AUTHENTICATION_SUCCESS) {
    session.fetchBillingInfo();
  } else if (ss == BILL_FETCHED) {
    session.payBill();
  } else if (ss == PAYBILL_SUCCESS) {
    session.removeServiceStateListener(this);
  } else if (ss == AUTHENTICATION_BAD_USRNAME_OR_PW) {
    ...
  } else {
    ...
  }
}

The API itself defines the different service states or responses, in a case that more service states are needed, they can be easily added to the Enum without breaking the compatibility. The service methods do not declare checked Exceptions, but fires notification to indicate service state changes. To access the API, the client code simply becomes registered as a ServiceStateListener, and then call the first service method, i.e. session.authenticate(). The work is minimum from there on, the state transition and logic is simple and is handled in serviceStateChanged(). This designs takes advantage of the goodness of type safety, the API will never notify of anything other than a Service State. It is also much more readable and organized. This is the solution I have found and implemented in my personal project.

The name you cannot utter December 25, 2009

Posted by metalickl in Software Insights.
Tags: , ,
add a comment

This was an absolute discovery by accident. I created a Java project under Mac OSX with something like com.***.****.con, where con was intended as a package for classes in communication and data connections. I then went ahead and checked the project into SVN.
Later when I tried again to check out the project using Eclipse in Windows XP, I kept on getting errors saying that there’s been errors in creating the folder con. A little researched served me right, and it had turned out that there are some reserved names in windows. Along with con, there are also

CON, PRN, AUX, CLOCK$, NUL
COM0, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9
LPT0, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.

which you simply cannot use for as a filename.

The windows distribution of Eclipse has been thoughtful. If you try to create a package with a reserved name, there will be an error message saying that package name is not supported on the current platform.

窦唯/DouWei complete discography December 5, 2009

Posted by metalickl in Life, Music.
add a comment

窦唯 / Dou Wei / Douwei

Being my favourite musician, I wanted to talk about his music for a while now. Douwei is one of the most inventive, talented musicians from China, producing splendid sound across a wide range of music genres. Douwei is a man of authenticity, making music for a life and not a living. Here, I’ll give a personal categorization to his music from the past 15 years.

  • 1991年 黑豹 专辑 与黑豹乐队合作 (heavy metal, chinese rock)
  • 1994年 黑梦 专辑 与做梦乐队合作 (post-punk, chinese rock, neo-psychedelia) similar to Bauhaus – Bauhaus, Cure
  • 1995年 艳阳天 专辑 (electronica, ambient, electronic, indie pop, synth)
  • 1998年 山河水 专辑 (electronica, ambient, electronic, indie pop, synth)
  • 1999年 幻听 专辑 与译乐队合作 (alternative rock, post-rock, ambient)
  • 2000年 雨吁 专辑 与译乐队合作 (录制) (alternative rock, post-rock) -similar to Bark Psychosis – Hex
  • 2003年 壹举·两得 专辑 与不一定乐队合作 (jazz rock, jazz fusion, post-rock, ambient, electronic)
  • 2003年 暮良文王 专辑 与暮良文王乐队合作 (folk, ambient, classic chinese instrumental)
  • 2004年 镜花缘记 专辑 与FM3乐队合作 (electronic, ambient, idm)
  • 2004年 八段锦 专辑 (electronica, synthpop, ambient, disco, indie)
  • 2004年 三国·四记 专辑 与不一定乐队合作 (jazz rock, jazz fusion, post-rock improvisation, ambient, electronic) -similar to Tortoise – TNT
  • 2004年 五鹊·六雁 专辑 与不一定乐队合作 (classic chinese instrumental, post-rock, ambient)
  • 2004年 相相生 专辑 与暮良文王乐队合作 (classic chinese instrumental, folk, ambient)
  • 2004年 期过圣诞 专辑 与不一定乐队合作 (jazz rock, jazz fusion, post-rock, improvisational music, live recording)
  • 2005年 山豆几石页 专辑 与暮良文王乐队合作 (folk, chinese classic instrumental, ambient)
  • 2005年 祭然品气国 专辑 与暮良文王乐队合作 (folk, chinese classic instrumental, ambient)
  • 2005年 八和 专辑 与不一定乐队合作 (jazz rock, jazz fusion)
  • 2005年 九生 专辑 与不一定乐队合作 (jazz rock, jazz fusion)
  • 2006年 东游记 专辑(上、下)与宁英杰、巫娜、张荐、文智涌合作 (ambient, chinese classic instrumental, folk, lp, improvisational music, live recording)
  • 2006年 水先后古清风乐 专辑 与不一定乐队合作 (ambient, idm, chinese chinese classic instrumental, improvisational music, live recording)
  • 2006年 后观音 专辑 与FM3乐队合作 (ambient, electonic)
  • 2007年 松阿珠阿吉 专辑 (ambient, folk)
  • 2007年 35651 EP (ambient, folk)
  • 2007年 佐罗在中国 专辑 (ambient, idm, electronic, folk, chinese classic instrumental)
  • 2008年 五音环乐 专辑 与不一定, 不一样乐队合作 (ambient, folk, jazz rock, cultural, post-rock, improvisational music, live recording)

My personal favourite is 雨吁, after that are 山河水, 三国, 幻听, 壹举, 艳阳天, 八和 and etc. My favourite track is 山秀谷 (雨吁)

downloads:

  • 1994 – 黑梦 http://www.dmpan.com/file/4Ud7tt1.html
  • 1995 – 艳阳天 http://www.dmpan.com/file/4UgMFmt.html
  • 1998 – 山河水 http://www.dmpan.com/file/4UjV3R5.html
  • 1999 – 幻听 http://www.dmpan.com/file/4Un6lGh.html
  • 2000 – 雨吁 http://www.dmpan.com/file/4Uo4g5t.html
  • 2003 – 暮良文王 http://www.dmpan.com/file/4UpWHmw.html
  • 2003 – 一举两得 http://www.dmpan.com/file/4UrOgVt.html
  • 2004 – 八段锦 http://www.dmpan.com/file/4V0ZCxB.html
  • 2004 – 期过圣诞 http://www.dmpan.com/file/4V20CLK.html
  • 2004 – 镜花缘 http://www.dmpan.com/file/4V2siUI.html
  • 2004 – 相相生 http://www.dmpan.com/file/4V426j1.html
  • 2004 – 三国四记 http://www.dmpan.com/file/4V6OWcS.html
  • 2004 – 五鹊六雁 http://www.dmpan.com/file/4V7gL1o.html
  • 2005 – 八和 http://www.dmpan.com/file/4V9LXhC.html
  • 2005 – 九生 http://www.dmpan.com/file/4VC5HMv.html
  • 2005 – 山豆几石页 http://www.dmpan.com/file/4VChwBh.html
  • 2005 – 祭然品气国 http://www.dmpan.com/file/4VEKzDV.html
  • 2006 – 东游记 http://www.dmpan.com/file/4VFZGOY.html
  • 2006 – 水先后古清风乐 http://www.dmpan.com/file/4VGglU4.html
  • 2006 – 后观音 http://www.dmpan.com/file/4VHInZz.html
  • 2007 – 松阿珠阿吉 http://www.dmpan.com/file/4VHttUE.html
  • 2007 – 佐罗在中国 http://www.dmpan.com/file/4WgM2PA.html
  • 2008 – 798 http://www.dmpan.com/file/4WgzolU.html
  • 2008 – 五音环乐.十三张 http://www.dmpan.com/file/4WoKpwG.html
  • 2008 – 五音环乐.上步南山 http://www.dmpan.com/file/4WqDeJO.html
  • 2008 – 五音环乐.七对儿 http://www.dmpan.com/file/4WseQYJ.html
  • 2008 – 五音环乐.后上海 http://www.dmpan.com/file/4WtyLqU.html
  • 2008 – 五音环乐.不一样(DVD) http://www.dmpan.com/file/4YiklT3.html
Follow

Get every new post delivered to your Inbox.