Five ‘s Weblog

November 12, 2007

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

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


      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.


October 5, 2007

The differences between Strategy pattern and State pattern

Filed under: Five's thought — by powerdream5 @ 2:54 am
Tags: , ,


       Recently, I am reading some articles about Refactoring, and I have draw two conclusions. The first one is that one of the benefits of refactoring is to change the procedure design into object-oriented design. The second one is that the design patterns really play an important role in the process of refactoring. For example, one of the refactoring ways is replacing conditions with polymorphism. In this case, strategy pattern and state pattern are the key!
       However, some programmers are very confused with strategy pattern and state pattern, because ther are pretty similar to each other. So today, I want to show the differences between them in my mind!

        Firstly, Let me give the definition of the two pattern according to the classic book – <<Design Patterns: Elements of Reusable Object-Oriented Software>>
        Strategy pattern: Define a family of algorithms, encapsulate each one and make them interchangeable.
        State pattern: Allow an object to alert its behavior when its internal state changes. The object will appear to change its class.

        perhaps you would have a headache just looking at the definitions, but I hope you can have a deep thought of them after reading this article.
        In order to distinguish the two patterns, we have to answer the question that why they look similar to each other. The answer is that the main concept of the two patterns is polymorphism. Both of them define a parent class and a series of children class inherit the parent class. And the client encapsulate the call to the method of the parent class (in fact, the client calls the method of the children class).
        In my opinion, the biggest difference between strategy pattern and state pattern is that we encapsulate each algorithm into a class in strategy pattern, but we encapsulate each state into a class in state pattern. So before deciding to use strategy pattern or state pattern to solve a problem, we must have the idea that what is the key point of the problem. is it the algorithm or is it the state? Now, let’s give an example to show it more clearly. 

Image from naturetrek

        In a zoo, a feeder is in charge of feeding a tiger. the tiger has three states, hungry, medium and full. Now, there are two cases.
        The first one is the feeder feed the tiger different kinds of food according to the state of the tiger. For instance, when the tiger is hungry, the feeder should support it meat; when the tiger is in medium, the feeder should support it cake; when the tiger is full, the feeder should support it water.
        The second one is each time the feeder will provide the tiger meat, cake and water. However, the tiger will eat only one of the three food according to the state of itself. When the tiger is hungry, it eats meat; when the tiger is in medium, it eats cake; when the tiger is full, it drinks water.
        After comparing the two cases, we know we need to realize the first case using strategy pattern and realize the second case using state pattern. So, now you can program by yourself, which will help you make more sense, or you can download the source files I made.

        In the former example, we think the state of the tiger doesn’t change. Now, let’s make something different. If the state of the tiger will change, what is going to happen. For example, at first, the tiger is hungry, and the feeder feed it meat or it eats meat, then the tiger transfer to the second state – medium, so the feeder should feet it cake or it eats cake. Therefore, how can we make changes to the programs we made for the former example. I hope you make a conclusion that it will be easier to change the program realized by state pattern than the one realized by strategy pattern in this case.

       After reading the words above, I hope you have a good understanding of the differences between strategy pattern and state pattern. If you have any different opinions or questions, welcome to leave message to me!

Blog at