Showing posts with label thoughts. Show all posts
Showing posts with label thoughts. Show all posts

Thursday, November 15, 2007

Custom or Standard?

Modern languages like Java, C#,... provide powerful APIs with many libraries which are ready to use. But sometimes I consider I should use the "standard" class or create my own custom class. Spending more time to think about it, I realize that it depends.

Assumes that I want to have a list of members, if I use Java, I can use List<Member> or create my own MemberList to store all members. If I want to have more features than the standard List<Member> provides, of course, I need to create my own MemberList, which bases on List<Member>. But what will I do if I only need a read-only list? A List<Member> cannot do it. To have a read-only list, I need to use Collections.unmodifiableList(myMemberList) or create MemberList which supports read-only only.

Now consider that I only need to use the read-only list inside my application, which solution can I choose? One of two but maybe one of three. I also can use List<Member> and forget about its "edit" methods.

To have a clearer design, using MemberList will be the best solution but it needs time to implement. To be lazy, using Collections.unmodifiableList(myMemberList) or List<Member> without using its "edit" methods is an acceptable answer.

Monday, June 25, 2007

Are there any places to use contructors of an abstract class with public or default access modifier?

As we already knew, we cannot create instances of an abstract class. So there are only 2 places to access constructors of an abstract class:

+ From the internal of that class (we can use private access modifier for this situation, it's enough), and
+ From its subclasses (we can use protected access modifier for this situation and it also satisfies the above situation)

And for other reasons, if we need to use instances of an abstract class, we will provide factory method(s) to create them (actually, at this time, we will return an instance of a "DEFAULT" subclass). So I don't see any reasons to declare constructors of an abstract class with public or default access modifiers.

Can you give me the reasons why I "need" to do it? Thank you.

Sunday, June 24, 2007

Interface - Where and When?

I understand that if I use interface, my application can a high flexible construction. I can easily extend class to add more function, and also make use of strong point of polymorphism and dynamic binding. But I'm wondering that where and when I should use interface.

At this time I'm developing a small application it contains a Tourist class and I can't imagine that when I need to create some more specific kinds of Tourist. Even I need to extend Tourist, I can re-factor Tourist to be a abstract class (or keep it unchanged) and then create subclasses of Tourist. Where is the place I need to consider to use interface here?

As I told you, I'm developing a small application and I know it still keep in small level, hardly to become bigger. So what is the matter I need to use interface here? That is WHEN question.

What do you think about the WHERE and WHEN?

Saturday, June 23, 2007

Lack of something

I spent a little of time to think about my work, my knowledge and my experience. And I found that I'm lack of knowledge, MUCH.

I'm a techno guy. I love new technologies, really love. And I always try to catch the trend of new technologies by collecting every little amount of information about them. I cannot remember how many times I said "WOW" when I found something interesting. But when I knew about it (a little, again, sadly) I threw it away to catch a new one.

Till now, about 6 years from the time I graduated university. I knew Spring, I knew Hibernate, I knew JSF, Struts, Wicket... and many things but I'd just knew, NOT UNDERSTAND. Even I can use them but I don't know how they works, how they make this or that interesting.

Of course, you cannot UNDERSTAND everything, but you should understand SOMETHING, something that can make you love, are interested in, something can make you fun and of course, it can help you to earn money. And here is my list, something I want to understand:

+ Algorithms
+ OOP (and how to apply it CORRECTLY)
+ Design Patterns
+ Software Development Process (I choose ICONIX to start)
+ Swing
+ Servlet
+ Spring
+ Hibernate
+ Struts
+ Wicket
(the list will be continued...)

Wednesday, May 02, 2007

Cái chết

Vừa rồi Tr thông báo anh N mới mất. Bần thần cả người. Mới vừa hôm nọ anh em còn nhậu...

Cuộc sống chẳng thể lường trước điều gì sẽ xảy ra. Hôm nay còn mà mai đã thành cát bụi. Nhìn lại bản thân thấy mình chưa làm được gì, cũng chẳng có gì. Buồn thật. Để rồi đến lúc mình thành cát bụi, phỏng mình có chi đây?

Xin chia buồn với gia đình anh N :-(

Wednesday, January 10, 2007

2 questions for iBATIS developers

I've just started to play with iBATIS in Action and I have 2 questions for iBATIS developers about configuration files.

- Why doesn't have <property> inside of <properties>?
My concern: Sometimes user wants to keep these properties inside of SqlMapConfig.xml rather than outside

- Why doesn't have 'file' or 'resource' attribute in <select>, <update>, <delete>,... which points to a .SQL file?
My concern: If it exists, DBA only needs to distribute SQL script files to developer and developer just needs to change a little of content rather than copy&paste

Wednesday, November 22, 2006

Which Java web framework you should learn next?

As in my previous post, I showed you an estimation for several Java web frameworks from Matt Raible. Did you take free time to examine it? OK, in despite of you did it or not, let's take a look at my choice.

For me, the choice is Struts. Struts, again Struts, a Gorilla of MVC web framework. But, not Struts, more exactly, it's Struts2 (former name is WebWork)

Why? Just see the new features of Struts2 2.0.2. You can see: code behind, zero configuration, and many more... All of them (new features in Struts2 2.0.x) are really brilliant.

In the past, WebWork is a good framework but IMO it has the lack of documentation (and also marketing). But now it became under the umbrella of ASF, I bet that it will have the success as its ancestor, Struts.

At this time, I'm working with v2.0.1 beta and I will upgrade to v2.0.2 immediately when it comes out.

Sunday, September 24, 2006

What's next?

After releasing Batch Generator, I've just had a new idea. At the first time when I had this idea, it aimed to resolve my own problem. Later I think it can help others too. It can be provided as a service or value-added product.

At this time, it's only the idea and you can only know it with the name "SSRSS".

Tuesday, September 19, 2006

Getting file extension

Java doesn't give you a way to get the extension of a file so you must do it yourself.

Usually you will get the position of the dot then use it to extract the extension. But it is not a correct way. Why? Because it depends. Depends on what? Depends on you and the OS you target.

If you use Windows, it's OK. Files in Windows can have extension. But in *nix, it's a different story. So you target to run your application both in Windows and *nix, you should consider this problem.

If your application target to run on Windows only, it's fine if you use the position of the dot to extract the extension (but remember, you must use lastIndexOf("."), not indexOf(".") because in Windows, extension will be determine by the position of the dot and from right to left of filename, not from left to right).

The below code fragment runs well in Windows and Linux.

public String getExtension(File file) {
String osName = System.getProperty("os.name");

if (osName.startsWith("Windows")) {
String filename = file.getName();
int index = filename.lastIndexOf(".");

if (index > 0 && index < filename.length() - 1) {
// extesion in Windows doesn't include the dot
// so we must use (index + 1) to substring
return filename.substring(index + 1).toLowerCase();
} else if (index == filename.length() - 1) {
// the dot is the last character in filename
// so extension is blank
return "";
}

// otherwise, this file doesn't have extension
// so we should return null
}

return null;
}


Update: Correct the source code

1-to-0..* and 1-to-1..*

This afternoon I think about how can I represent 1-to-0..* and 1-to-1..* relationship between objects.

First, I should describe what are 1-to-0..* and 1-to-1..*. 1-to-0..* means 1 object of this class can have relationship with 0 or more objects of another class. And 1-to-1..* means 1 object of this class can have relationship with 1 or more objects of another class.

A tourist guide can guide zero or more tours. But an order, it must contains at least one order item. In contrary, a tour and an order item cannot exist without tourist guide and order.

Of course, in TouristGuide class and Order class should contains a collection to keep the tours and order items. But how to represent the 1-to-0..* and 1-to-1..* relationships? User can call getTours() or getOrderItems() any time he wants.

I think in 1-to-0..* relationships, when user invokes getTours() on a TouristGuide, he can have a collection contains no element if that TouristGuide doesn't have any Tour to guide. But in 1-to-1..* relationthips, when user invokes getOrderItems() on an Order, he can have a null value if that Order didn't contain any OrderItem yet.

class TouristGuide {

private ArrayList<Tour> tours = new ArrayList<Tour>();

public ArrayList<Tour> getTours() {
return tours;
}

public void addTour(Tour tour) {
tours.add(tour);
}

...

}

class Order {

private ArrayList<OrderItem> orderItems;

public ArrayList<OrderItem> getOrderItems() {
return orderItems;
}

public void addOrderItem(OrderItem orderItem) {
if (orderItems == null) {
orderItems = new ArrayList<OrderItem>();
}

orderItems.add(orderItem);
}

...

}