Five ‘s Weblog

November 28, 2007

Whose mistake?

Filed under: Five's thought — by powerdream5 @ 5:21 pm
Tags: , ,

        I will graduate in this December, and now looking for a job. To be honest, I really want to be a J2EE programmer.  Actually, I have had several interviews with some companies. It doesn’t matter whether I finally get a job, but I have strong feeling that what we learned in the university are not the same things the company want to get from us. When I studied for my bachelor degree in China, I had already hearded a lot of information about the knowledge we get from the classes are detached from the companies. However, now, when I am studying for the masters’ degree in U.S, I find the same thing happens here as in China.

        For example, Agile Development is so popular in most software companies currently. However, in the university, we almost have no chance of understanding it deeply, perhaps just heard a little bit informaton on it, and it is not necessary to say we cannot apply it to the real projects.

        Whose mistake is this? It is not the professors’ mistake, since they are just responible for giving instructions to students. It is absolutely not the companies’ mistake. They just want to find the right person who can contribute to the companies immediately. It is also not the students’, expecially the good students’, mistake. We spend a large part of our time in all kinds of assignments in that we want a high GPA, so that it is impossible for us to concentrate on the things which are becoming the trend.

        In fact, we cannot find the right person who need to be responsible for this problem. What we need to do is finding the reasons caused it and solving this problem. In my opinion, the most important reason is that the purposes of the unversity and the company are different from each other. The universities aim to help the students build solid foundation and form a good learning way, therefore, the students can pick up new things more quickly and effectively in the future. However, the companis hope the new employees can bring their benefits immediately. I think no company would like to invest time and money to the new employees and wait for them to bring profit, which also explains why experience is the most significant factor when somebody is looking for a job. In fact, few days ago, I read an article that some students complain that why the textbooks they use are the ones published 20 years ago, for example, the theory of operating system, which is usually a required course of the computer science department.

11_26_1_2007.jpg

Advertisements

November 24, 2007

Shift to Dynamic programming language

Filed under: Five's thought — by powerdream5 @ 11:44 pm
Tags: , ,

        In recent years, dynamic programming languages develope very fastly, especially PHP and Ruby. There is no doubt that They have already became the first choice for many programmerers when developing web applications. Furthermore, it seems that more and more people, like thoughtworks, are trying to apply them to enterprise applications dominated by Java and C#.

        I have studied JAVA for almost 5 years. I love it. Everything in JAVA makes me comfortable. Although I have finished several projects by PHP, and in some aspects, PHP is really better than JAVA, I still put JAVA in the first place in my mind.

       In fact, there are already a lot of arguments about PHP or Ruby on Rails are not suitable for complex enterprise appliactions. In my opinion, everything in the world is changing, so is dynamic programming language. Just like Java adopts some good features of dynamic language, for example, reflection and supporting scripting language, I believe that both PHP and Ruby on Rails are developing so that they can be applied in a broader area.

        Now, I have a strong feeling that it is necessary to pay more attention to Ruby and Ruby on Rails. Many Java programers think that studying dynamic languages can imporve their thought about Java. Therefore, it is killing two birds with one stone.

        Fortunately, because I have much experience in Java, it is easy to pick up a kind of dynamic language. However, I suggest the people who want to study programming start from Java or C++, since they are more religious and can help you build solid fundation in programming.

sky.jpg

November 20, 2007

As a Java Programmer, Do you still remember?

Filed under: Five's thought — by powerdream5 @ 10:31 pm
Tags: , ,

        Today, When I was tidying up the disk space of my hard driver, I found something interesting. It is the notes I made when I started learning Java. In fact, I have already used Java for almost 4 years. However, when I am coding, It seems that many things have already became my habits, so that I don’t think about the reason why I do it like that. When reading these notes again, I have a feeling of getting an insight into the problem. Therefore, I suggest all the programmers, not just Java programmer, not always keeping coding, sometimes please stop to think and recall.

        In the following, there are some concepts of Java. They are pretty easy, but do you still remember them?

Default values for primitive members: When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it. Notice that this guarantee doesn’t apply to local variables (for example, you declare it in a method), since that are not fields of a class.

The static keyword: When you say something is static, it means that data or method is not tied to any particular object instance of that class, and it directly belongs to the class. Bear in mind that the static method cannot directly access non-static members or methods. There are two ways to refer to a static variable and method. You can name it via an object or refer to it directly through its class name. Usually, we always prefer to the later way.

Distinguishing overloaded methods: there is a siimple rule. Each overloaded method must take a unique list of argument types. Even differences in the ordering of arguments are sufficient to distinguish two methods. And only the differences of the type of return value cannot separate two methods.

String s = new String(“a String”);
this sentence contains four tasks. The first one is creating a String object in the heap; the second one is initializing this String object as “a String”; the third one is create a reference named s; then make this reference point to the String object.

what is the sequence of initializing the members of a java class?
Let’s assume that there is a class named Dog.

  1. when we create an object of Dog or the first time to access the static members or methods of Dog, JVM need to find Dog.class firstly
  2. all the static memebers of the Dog class will be initialized
  3. arranging space for the object created by new Dog() in the heap. This space will be cleared and all the memebers of the Dog class will get their default value
  4. run the definition initialization
  5. run the consturctor

11_20_1_2007.jpg

November 17, 2007

The differences between Decorator Pattern and Proxy Pattern

Filed under: Five's thought — by powerdream5 @ 9:38 pm
Tags: ,

       Firstly, Let’s look at the following two UML diagrams which describes the basic implementation of Decorator Pattern and Proxy Pattern respectively.

11_17_2_2007.jpg

11_17_3_2007.jpg

        The two UML diagrams may confuse us. The two design patterns looks the same with each other. With Decorator Pattern, the decorator and the decoratee implement the same interfaces. With Proxy PatteRn, the proxy class and the real class delegated implement the same interfaces. Furthermore, no matter which pattern is used, it is easy to add functions before or after the method of the real object.        However, in fact, there are some differences between Decorator Pattern and Proxy Pattern. Decorator Pattern focuses on dynamically adding functions to an object, while Proxy Pattern focuses on controlling access to an object. In other words, with Proxy Pattern, the proxy class can hide the detail information of an object from its client. Therefore, when using Proxy Pattern, we usually create an instance of abject inside the proxy class. And when using Decorator Pattern, we typically pass the original object as a parameter to the constructor of the decorator.

       We can use another sentence to conclude thire differences: with the Proxy pattern, the relationship between a proxy and the real subject is typically set at compile time, whereas decorators can be recursively constructed at runtime.

//Proxy Pattern
public class Proxy implements Subject{

       private Subject subject;
       public Proxy(){
             //the relationship is set at compile time
            subject = new RealSubject();
       }
       public void doAction(){
             ….
             subject.doAction();
             ….
       }
}

//client for Proxy
public class Client{
        public static void main(String[] args){
             //the client doesn’t know the Proxy delegate another object
             Subject subject = new Proxy();
             …
        }
}
//Decorator Pattern
public class Decorator implements Component{
        private Component component;
        public Decorator(Component component){
            this.component = component
        }
       public void operation(){
            ….
            component.operation();
            ….
       }
}

//client for Decorator
public class Client{
        public static void main(String[] args){
            //the client designate which class the decorator decorates
            Component component = new Decorator(new ConcreteComponent());
            …
        }
}

11_17_4_2007.jpg

November 14, 2007

Command Pattern

Filed under: Programming — by powerdream5 @ 10:04 pm
Tags:

       I am not going to introduce the definition of Command Pattern, as well as the advantage and disadvantage of it. You can find many articles about that on the internet. Here, I just wanna give an example and realiz it by command pattern. (command pattern in wiki)

       There are two cleaner. One is responsible for sweeping the floor, and the other one is responsible for rubbing the table.  However, both of them are lazy. They never do their job until the supervisor commands them.

       There is no doubt it is suitable for command pattern to realize this problem. The following is my code:

//the command interface
public interface Command{
       public void executeCommand();
}

//concrete command 1
public class RubTableCommand implements Command{
       private Worker worker;
       public RubTableCommand(Worker worker){
             this.worker = worker;
       }

       public void executeCommand(){
            worker.doJob();
       }
}

//concrete command 2
public class SweepFloorCommand implements Command{
       private Worker worker;
       public SweepFloorCommand(Worker worker){
             this.worker = worker;
       }

       public void executeCommand(){
             worker.doJob();
       }
}

//the receiver interface
public interface Worker{
       public void doJob();

//receiver 1
public class CleanerForRubTable implements Worker{
 
      public void doJob(){
          System.out.println(“Rubbing floor”);
      }
}

//receiver 2
public class CleanerForSweepFloor implements Worker{
 
      public void doJob(){
         System.out.println(“Sweeping floor”);
      }
}

//the invoker
public class InvokeCommand{
       private Command command;

       public void setCommand(Command command){
            this.command = command;
       }

       public void sendCommand(){
            command.executeCommand();
       }
}

 //the client
public class Supervisor{
 
       public static void main(String[] args){
             Worker cleanerForSweepFloor = new CleanerForSweepFloor();
             Worker cleanerForRubTable = new CleanerForRubTable();
  
             InvokeCommand ic = new InvokeCommand();
             Command command = new RubTableCommand(cleanerForRubTable);
             ic.setCommand(command);
             ic.sendCommand();

            command = new SweepFloorCommand(cleanerForSweepFloor);
            ic.setCommand(command);
            ic.sendCommand();  
       }
}

11_14_1_2007.jpg

November 12, 2007

Patterns and Refactoring – After reading the article “Patterns & XP”

Filed under: Five's thought — by powerdream5 @ 10:11 pm
Tags: , ,

11_12_1_2007.jpg   

      The article “Pattern & XP” is written by Joshua Kerievsky. The purpose of this article is to figure out how to apply Patterns to the context of Extreme Programming, because he thinks that XP has far focused heavily on Refactoring while remaining all but silent about Patterns, the reason for this is that Patterns encourage over-engineering, while Refactoring keeps things simple and light.

      The prerequisite of understanding the importance of combining patterns and refactoring is realizing the relationship between them. This article has quoted some words from the Design Patterns Book, which can help to explain it well.

     Our design patterns capture many of the structures that result from refactoring. Using these patterns early in the life of a design prevents later refactorings. But even if you don’t see how to apply a pattern until after you’ve built your system, the pattern can still show you how to change it. Design patterns thus provide targets for your refactoring.

     However, there are two problems of applying patterns early in the life of a design. The first one is over-engineering, which means that we make great efforts to produce flexible and robust designs early in the life of a system, but we do not realize that all our work will be meaningless and wasteful if the system never needs such degrees of flexibility and robutness. Sometimes, it is not necessary to inject various desgin patterns to our designs. Secondly, there is no 100% perfect design, in order to keep the system in high quality, refactorying is necessary!

     In the other side, without Patterns, refactoring may make small design improvements, but their overall design will ultimately suffer because it lacks the order, simplicity, and effectiveness that come from intelligently using Patterns.

     Herefore, Joshua Kerievsky suggest us that: start simple, think about Patterns but keep them on the back-burner, make small refactorings, and move these refactorings towards a Pattern or Patterns only when there is a genuine need for them.

11_12_2_2007.jpg

November 11, 2007

The process-level cache in Hibernate

Filed under: ORM — by powerdream5 @ 8:03 pm
Tags: , ,

11_11_1_2007.jpg

        Hibernate has two kinds of cache, which are the first-level cache and the second-level cache. We also call the first-leve cache session cache, and the second-level cache process-level cache. The first-level cache is mandatory and can’t be turned off; However, the second-level cache in Hibernate is optional. Because the process-level cache caches objects across sessions and has process or cluster scope, in some situation, turning on the process-level cache can improve the performance of the application. In fact, before accessing the database to load an object, Hibernate will first look in the Session cache and then in the process-level cache.

       The process-level cache is best used to store objects that change relatively infrequently, and it is set up in two steps. First, you have to decide which concurrency strategy to use. After that, you configure cache expiration and physical cache attribtes using the cache provider. Hibernate supports a variety of caching strategies including:
read-only : which is suitable for objects which never changes. Use if for reference data only.
read-write : which is suitable for objects that are modified by the application.

      To cache these classes in the process-level cache, we must use the <cache> element in the O/R mapping. For example:
<class name=”?” talbe=”?”>
      <cache usage=”read-wirte” />
      …
</class>

      The usage=”read-write” attribute specifies that instances of this class are sometimes updated by the application. One thing needed to remember is that you must enable lazy loading for relationships that are from objects that not cached to objects that are cached to ensure that the applications uses the cache.
<class name=”?” talbe=”?”>
      <cache usage=”read-wirte” />
       …

       <set name=”?” lazy=”true”>
                /*collections require their own <cache> element*/
               <cache usage=”read-only” />
                …
        </set>
</class>

11_11_2_2007.jpg

November 9, 2007

Eager loading in Hibernate

Filed under: ORM — by powerdream5 @ 5:50 pm
Tags: , , ,

      In default, Hibernate adopts the mechanism of lazy loading to load objects from databases. Lazy loading contributes a lot to improve the performance of an application by limiting the amount of objects that will be needed. In contrast to lazy loading, eager loading loads the full objects tree once. Because eager loads multiple related objects with a single SELECT statement instead of using multiple SELECT statements, therefore in some situations, eager loads can also improve the performance of the application by reducing the times of accessing the database.

     There are two ways to configure eager loading in Hibernate. They are staticly eager loading and dynamically eager loading respectively. If the application always traverses a relationship, you might want to configure it to be always staticly eager loaded. Conversely, you might want to dynamically configure eager loading for relationships that are only traversed by the application when handling particular requests.

     One way to configure static eager loading is specifying a value for the fetch attribute of the relationship’s mapping element. 
     This attribute can have one of two values:
     select: Lazily load the referenced object or collection with a separate SQL SELECT statement. It is the default value
     join: Eagerly load the referenced object or collection using an outer join

<hibernate-mapping>
     <class name=”?” table=”?”>
          <many-to-one name=”?” fetch=”join” column=”?” />
     </class>
</hibernate-mapping>

      To configure dynamical eager loading, you must use queries with fetch joins.
<hibernate-mapping>
      …
      <query name=”queryName”>
            <![CDATA[
                 from student s
                 left outer join fetch s.classID as c
                 left outer join fetch c.className
                 where s.id = ?
            ]]>
       </query>
     ….
</hibernate-mapping>

     Finally, take a note that Hibernate has a rule that a class can only have at most one collection loaded using an outer join. This is to prevent inefficient queries that return the Cartesian product of too large collections.

11_9_2_2007.jpg

November 7, 2007

Transaction Concurrency (2)

Filed under: Five's thought — by powerdream5 @ 11:13 pm
Tags: , ,

       As mentioned in the article Transaction Concurrentcy(1), most application choose read committed as the isolation level. The reason is that no matter whether there is concurrency occuring, serializable and repeatable read need to lock the rows in the database, which cause the reduction in the system performance, especially for these system where concurrency doesn’t happen frequently. However, Read committed cannot avert unrepeatable read and Second lost updates problem, therefore, we need to use optimistic locking or pessimistic locking to make up!

11_7_1_2007.jpg

      Today, I am going to make a brief introduction to optimistic locking. Optimistic locking is based on the assumption that most database transaction don’t conflict with other transactions. In fact, optimistic locking doesn’t actually lock anything. Instead, when a transaction updates a row it verifies that the row has not been changed or deleted by a different transaction since it was read. If it has, the transaction is typically rolled back and retried.

     Usually, there are three methods to realize optimistic locking. The first one is adding a version column to the tables in the database. The second one is adding a timestamp column, and the third one is to compare current values of the columns with their previously read values. Commonly, we prefer the vrsion column to the other two methods, since the third method makes the sql sentences too complicated and the second method is not suitable for any case.

     There is a sql sentence using version column:
     Update ACTIVITIES Set PLACE = ‘?’ WHERE ACTIVITY_ID=? AND VERSION = ?;

     The following is an simple example to show how to configure optimistic locking realized by version column. It is pretty easy!

<hibernate-mapping>
      <class name=”**” table=”**”>
              <id name=”*” column=”*”>
                    <generator class=”*” />
              </id>
              <version name=”version” column=”VERSION” />
      </class>
</hibernate-mapping>

11_7_2_2007.jpg

November 6, 2007

My Personal Website

Filed under: About me — by powerdream5 @ 10:15 pm
Tags: ,

       I am sorry for I haven’t update the blog for several days, because I worked on mypersonal website. The good news is that it is finished today, and you can visit it
through this link: http://www2.potsdam.edu/caow190. I used a lot javascript, and tested it in IE and Firefox. Unfortunately, It seems that some javascript code do not work  well in Safari. Therefore, please visit it using IE or Firefox. I am pretty sorry for that.

      In my personal website, you can see some works I did in recent years. The works include web development projects, Java program, 2D design works, 3D design works and Flash works. After seeing a lot of designing stuff made by me, you may regard me as a web designer. In fact, I have much more experience in programming than designing, and I set my career orientation as a web application programmer. Designing is just a kind of my interest. No matter what, hope you have fun with it. If you have any question or comment, please contact me!

11_3_1_2007.jpg

Next Page »

Blog at WordPress.com.