Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, April 24, 2016

Switch Statement

It shows just one of the operations that might depend on the type of employee.

There are several problems with this function.

First, it's large, and when new employee types are added, it will grow.
Second, it very clearly does more than one thing.
Third, it violates the Single Responsibility Principle (SRP) because there is more than one reason for it to change.
Fourth, it violates the Open Closed Principle (OCP) because it must change whenever new types are added.

But possibly the worst problem with this function is that there are an unlimited number of other functions that will have the same structure.
For example we could have:
  • isPayday(Employee e);
  • deliverPay(Employee e, Money pay);

The solution to this problem is to bury the switch statement in the basement of an abstract factory, and never let anyone see it.

The factory will use the switch statement to create appropriate instance of the derivatives of employee, and the various functions, such as calculatePay, isPayday and deliverPay will be dispatched polymorphically through the employee interface.

Tuesday, January 15, 2008

J2SE 5.0 Enhanced for Loop

Here an example of J2SE 5.0 Enhanced for Loop by one of my old colleague:
This code maybe will run faster.

Wednesday, December 19, 2007

If & Else If


These two set of codes seem like having no different. It seem like "return" make "else if" useless.

Monday, November 19, 2007

Microsoft ASP.NET 2.0 Vs Java Apache Struts

Hehe, another Microsoft & Java comparison.

Overall, both of them having a quite complete framework which let developers easy to develop web application.

ASP.NET 2.0 & Apache Struts framework comparison:

1) .NET Master Page = Struts Tiles Definition (Something like layout)
2) .NET Theme & Skin, Struts don't have this feature. (Store all the images, css in one place, make it easy to change from one set to another set)
3) Struts URL remap from .jsp to .do, .NET don't have this feature. (The jsp file for http://www.abc.com/index.do can be store at //server-root-path/somename/main.jsp, outsider won't know where is the exact location of the jsp file)
4) Struts Business Logic & Presentation Layer Separate, .NET don't have this feature. (Business logic centralize at Struts Action, separate from presentation layer which is the jsp, jsp only use to display selected data, all the logic on select what data out program at Struts Action)
5) Both have Resources for localization.
6) Both have Pre-compilation, create a cache copy on the server, let user open a website faster.

Secondly is the software use for web development:

1) Microsoft ASP.NET 2.0 - Microsoft Visual Studio 2005 (NOT FREE) or Microsoft Visual Web Developer (FREE) + Microsoft .NET Framework 2.0

2) Java Apache Struts - Netbeans (FREE) or Eclipse (FREE) + Struts plug in

Thirdly is how easy to pick up these new framework:

1) Microsoft ASP.NET 2.0 - I find it quite easy to learn. There are a lot of online resources available to view or download. I like W3Schools the most, all the basic information can get from there, easy to understand also.

2) Java Apache Struts - I find it hard to learn. There are too many things to handle for create a single page, struts-config, tiles-definition, application-resources, Action, Form & jsp 6 things. I took quite some time to pick up this framework.

But the way, I still haven't fully discover all the .NET features, I might miss out some important features on .NET. Overall, I like .NET, it is easy to learn.

Thursday, November 15, 2007

Java Class Cache Problem

I found this cache problem during my development. Example of how this problem can be happen:

If these two java files compiled into class files previously, then we might need to be careful when changing the "planId" constants value. The problem happen when we change the "planId" value to some other value & only compile the Constants.java file. In this case, Main.class file still refer to previous "planId" value which is "999".

So, the best solution is to remove all the classes & compile all java files again which we called it as Clean Build.

Thursday, October 18, 2007

Programming Trick

Here is some trick to let the code run faster, I face this long time ago, finally I am able to write it down.

For CodeA & CodeB:

The different will be very big when the String Array gone larger. If let say the size of the String Array is 100, CodeA will create 100 Integer object to store it's size, the program will end up like this CodeC.

Obviously, every loop set the String Array size is unnecessary. So, it is alway better to keep CodeB practice.